Merge pull request #8769 from totten/master-gencode-test
[civicrm-core.git] / CRM / Core / CodeGen / Reflection.php
1 <?php
2
3 /**
4 * Create classes which are used for schema introspection.
5 */
6 class CRM_Core_CodeGen_Reflection extends CRM_Core_CodeGen_BaseTask {
7
8 protected $checksum;
9
10 /**
11 * @var string
12 */
13 private $raw;
14
15 /**
16 * @return bool
17 * TRUE if an update is needed.
18 */
19 public function needsUpdate() {
20 if (!file_exists($this->getAbsFileName())) {
21 return TRUE;
22 }
23
24 if ($this->getSchemaChecksum() !== self::extractRegex($this->getAbsFileName(), ';\(GenCodeChecksum:([a-zA-Z0-9]+)\);')) {
25 return TRUE;
26 }
27
28 return !$this->isApproxPhpMatch(
29 file_get_contents($this->getAbsFileName()),
30 $this->getRaw());
31 }
32
33
34 public function run() {
35 echo "Generating table list\n";
36 $template = new CRM_Core_CodeGen_Util_Template('php');
37 $template->assign('tables', $this->tables);
38 $template->assign('genCodeChecksum', $this->getSchemaChecksum());
39 $template->run('listAll.tpl', $this->getAbsFileName());
40 }
41
42 /**
43 * Generate the raw PHP code for the data file.
44 *
45 * @return string
46 */
47 public function getRaw() {
48 if (!$this->raw) {
49 $template = new CRM_Core_CodeGen_Util_Template('php');
50 $template->assign('tables', $this->tables);
51 $template->assign('genCodeChecksum', 'NEW');
52 $this->raw = $template->fetch('listAll.tpl');
53 }
54 return $this->raw;
55 }
56
57 /**
58 * @return string
59 */
60 protected function getAbsFileName() {
61 return $this->config->CoreDAOCodePath . "AllCoreTables.data.php";
62 }
63
64 protected function getSchemaChecksum() {
65 if (!$this->checksum) {
66 CRM_Utils_Array::flatten($this->tables, $flat);
67 ksort($flat);
68 $this->checksum = md5(json_encode($flat));
69 }
70 return $this->checksum;
71 }
72
73 }