Merge pull request #9651 from fuzionnz/CRM-19844-hook_only_when_success
[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 /**
35 * Run generator.
36 */
37 public function run() {
38 echo "Generating table list\n";
39 $template = new CRM_Core_CodeGen_Util_Template('php');
40 $template->assign('tables', $this->tables);
41 $template->assign('genCodeChecksum', $this->getSchemaChecksum());
42 $template->run('listAll.tpl', $this->getAbsFileName());
43 }
44
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
60 /**
61 * Get absolute file name.
62 *
63 * @return string
64 */
65 protected function getAbsFileName() {
66 return $this->config->CoreDAOCodePath . "AllCoreTables.data.php";
67 }
68
69 /**
70 * Get the checksum for the schema.
71 *
72 * @return string
73 */
74 protected function getSchemaChecksum() {
75 if (!$this->checksum) {
76 CRM_Utils_Array::flatten($this->tables, $flat);
77 ksort($flat);
78 $this->checksum = md5(json_encode($flat));
79 }
80 return $this->checksum;
81 }
82
83 }