Merge pull request #10541 from JMAConsulting/CRM-20411
[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 // Generating this file is fairly cheap, and we don't have robust heuristic
25 // for the checksum.
26 // if ($this->getSchemaChecksum() !== self::extractRegex($this->getAbsFileName(), ';\(GenCodeChecksum:([a-zA-Z0-9]+)\);')) {
27 // return TRUE;
28 // }
29
30 return !$this->isApproxPhpMatch(
31 file_get_contents($this->getAbsFileName()),
32 $this->getRaw());
33 }
34
35
36 /**
37 * Run generator.
38 */
39 public function run() {
40 echo "Generating table list\n";
41 $template = new CRM_Core_CodeGen_Util_Template('php');
42 $template->assign('tables', $this->tables);
43 $template->assign('genCodeChecksum', 'IGNORE');
44 $template->run('listAll.tpl', $this->getAbsFileName());
45 }
46
47 /**
48 * Generate the raw PHP code for the data file.
49 *
50 * @return string
51 */
52 public function getRaw() {
53 if (!$this->raw) {
54 $template = new CRM_Core_CodeGen_Util_Template('php');
55 $template->assign('tables', $this->tables);
56 $template->assign('genCodeChecksum', 'NEW');
57 $this->raw = $template->fetch('listAll.tpl');
58 }
59 return $this->raw;
60 }
61
62 /**
63 * Get absolute file name.
64 *
65 * @return string
66 */
67 protected function getAbsFileName() {
68 return $this->config->CoreDAOCodePath . "AllCoreTables.data.php";
69 }
70
71 // /**
72 // * Get the checksum for the schema.
73 // *
74 // * @return string
75 // */
76 // protected function getSchemaChecksum() {
77 // if (!$this->checksum) {
78 // CRM_Utils_Array::flatten($this->tables, $flat);
79 // ksort($flat);
80 // $this->checksum = md5(json_encode($flat));
81 // }
82 // return $this->checksum;
83 // }
84
85 }