Merge pull request #8796 from eileenmcnaughton/master
[civicrm-core.git] / CRM / Core / CodeGen / BaseTask.php
1 <?php
2
3 /**
4 * Class CRM_Core_CodeGen_BaseTask
5 */
6 abstract class CRM_Core_CodeGen_BaseTask implements CRM_Core_CodeGen_ITask {
7 /**
8 * @var CRM_Core_CodeGen_Main
9 */
10 protected $config;
11
12 protected $tables;
13
14 /**
15 * @param CRM_Core_CodeGen_Main $config
16 */
17 public function __construct($config) {
18 $this->setConfig($config);
19 }
20
21 /**
22 * TODO: this is the most rudimentary possible hack. CG config should
23 * eventually be made into a first-class object.
24 *
25 * @param object $config
26 */
27 public function setConfig($config) {
28 $this->config = $config;
29 $this->tables = $this->config->tables;
30 }
31
32 /**
33 * @return bool
34 * TRUE if an update is needed.
35 */
36 public function needsUpdate() {
37 return TRUE;
38 }
39
40 /**
41 * Extract a single regex from a file.
42 *
43 * @param string $file
44 * File name
45 * @param string $regex
46 * A pattern to match. Ex: "foo=([a-z]+)".
47 * @return string|NULL
48 * The value matched.
49 */
50 protected static function extractRegex($file, $regex) {
51 $content = file_get_contents($file);
52 if (preg_match($regex, $content, $matches)) {
53 return $matches[1];
54 }
55 else {
56 return NULL;
57 }
58 }
59
60 /**
61 * Determine if two snippets of PHP code are approximately equivalent.
62 *
63 * This includes exceptions to equivalence for (a) whitespace and (b)
64 * the token "GenCodeChecksum".
65 *
66 * This is useful for determining if someone has manually mucked with
67 * one the files. However, it's not perfect -- because whitespace changes
68 * are not detected. Hence, it's good to use in combination with another
69 * heuristic.
70 *
71 * @param $actual
72 * @param $expected
73 * @return bool
74 */
75 protected function isApproxPhpMatch($actual, $expected) {
76 $actual = preg_replace(';\(GenCodeChecksum:([a-zA-Z0-9]+)\);', '', $actual);
77 $actual = preg_replace(';[ \r\n\t];', '', $actual);
78
79 $expected = preg_replace(';\(GenCodeChecksum:([a-zA-Z0-9]+)\);', '',
80 $expected);
81 $expected = preg_replace(';[ \r\n\t];', '', $expected);
82
83 return $actual === $expected;
84 }
85
86 }