Merge pull request #22380 from braders/core-483-show-customised-preferences-on-validation
[civicrm-core.git] / CRM / Core / CodeGen / BaseTask.php
CommitLineData
5e434adf
ARW
1<?php
2
b5c2afd0
EM
3/**
4 * Class CRM_Core_CodeGen_BaseTask
5 */
5e434adf 6abstract class CRM_Core_CodeGen_BaseTask implements CRM_Core_CodeGen_ITask {
bc02b97b
TO
7 /**
8 * @var CRM_Core_CodeGen_Main
9 */
5e434adf
ARW
10 protected $config;
11
bc02b97b
TO
12 protected $tables;
13
2558c2b0 14 /**
bc02b97b 15 * @param CRM_Core_CodeGen_Main $config
2558c2b0 16 */
bc02b97b
TO
17 public function __construct($config) {
18 $this->setConfig($config);
5e434adf
ARW
19 }
20
2558c2b0 21 /**
4f1f1f2a
CW
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
2558c2b0 26 */
00be9182 27 public function setConfig($config) {
5e434adf
ARW
28 $this->config = $config;
29 $this->tables = $this->config->tables;
30 }
96025800 31
ce22c206
TO
32 /**
33 * @return bool
34 * TRUE if an update is needed.
35 */
36 public function needsUpdate() {
37 return TRUE;
38 }
39
64b070bf
TO
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
0f35babf
TO
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) {
c3fc2621
CW
76 foreach (['actual', 'expected'] as $var) {
77 $$var = CRM_Core_CodeGen_Util_ArraySyntaxConverter::convert($$var);
1713a0ec 78 $$var = preg_replace("# '\\d+' => #", " ", $$var);
c3fc2621
CW
79 $$var = preg_replace(';\(GenCodeChecksum:([a-zA-Z0-9]+)\);', '', $$var);
80 $$var = strtolower(preg_replace(';[ \r\n\t];', '', $$var));
81 }
0f35babf
TO
82 return $actual === $expected;
83 }
84
5e434adf 85}