Merge pull request #9815 from WeMoveEU/CRM-19962
[civicrm-core.git] / CRM / Core / CodeGen / Reflection.php
CommitLineData
5e434adf
ARW
1<?php
2
3/**
4 * Create classes which are used for schema introspection.
5 */
6class CRM_Core_CodeGen_Reflection extends CRM_Core_CodeGen_BaseTask {
4ef04170
TO
7
8 protected $checksum;
9
0f35babf
TO
10 /**
11 * @var string
12 */
13 private $raw;
14
4ef04170
TO
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 }
0f35babf
TO
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());
5e434adf
ARW
31 }
32
0f35babf 33
8246bca4 34 /**
35 * Run generator.
36 */
4ef04170
TO
37 public function run() {
38 echo "Generating table list\n";
5e434adf
ARW
39 $template = new CRM_Core_CodeGen_Util_Template('php');
40 $template->assign('tables', $this->tables);
0f35babf 41 $template->assign('genCodeChecksum', $this->getSchemaChecksum());
4ef04170
TO
42 $template->run('listAll.tpl', $this->getAbsFileName());
43 }
44
0f35babf
TO
45 /**
46 * Generate the raw PHP code for the data file.
47 *
48 * @return string
49 */
50 public function getRaw() {
51 if (!$this->raw) {
52 $template = new CRM_Core_CodeGen_Util_Template('php');
53 $template->assign('tables', $this->tables);
54 $template->assign('genCodeChecksum', 'NEW');
55 $this->raw = $template->fetch('listAll.tpl');
56 }
57 return $this->raw;
58 }
59
4ef04170 60 /**
8246bca4 61 * Get absolute file name.
62 *
4ef04170
TO
63 * @return string
64 */
65 protected function getAbsFileName() {
66 return $this->config->CoreDAOCodePath . "AllCoreTables.data.php";
67 }
5e434adf 68
8246bca4 69 /**
70 * Get the checksum for the schema.
71 *
72 * @return string
73 */
0f35babf 74 protected function getSchemaChecksum() {
4ef04170
TO
75 if (!$this->checksum) {
76 CRM_Utils_Array::flatten($this->tables, $flat);
77 ksort($flat);
0f35babf 78 $this->checksum = md5(json_encode($flat));
4ef04170
TO
79 }
80 return $this->checksum;
5e434adf 81 }
96025800 82
5e434adf 83}