Merge pull request #18094 from colemanw/ets
[civicrm-core.git] / CRM / Core / CodeGen / DAO.php
1 <?php
2
3 /**
4 * Create DAO ORM classes.
5 */
6 class CRM_Core_CodeGen_DAO extends CRM_Core_CodeGen_BaseTask {
7
8 /**
9 * @var string
10 */
11 public $name;
12
13 /**
14 * @var string
15 */
16 private $tableChecksum;
17
18 /**
19 * @var string
20 */
21 private $raw;
22
23 /**
24 * @var string
25 * translate function name
26 */
27 private $tsFunctionName;
28
29 private $useHelper = '';
30
31 /**
32 * CRM_Core_CodeGen_DAO constructor.
33 *
34 * @param \CRM_Core_CodeGen_Main $config
35 * @param string $name
36 * @param string $tsFunctionName
37 */
38 public function __construct($config, $name, $tsFunctionName = 'ts') {
39 parent::__construct($config);
40 $this->name = $name;
41 $this->tsFunctionName = $tsFunctionName;
42 // Cleanup helper class with a use statement
43 if (strpos($tsFunctionName, '::ts')) {
44 $this->tsFunctionName = 'E::ts';
45 $this->useHelper = 'use \\' . explode('::', $tsFunctionName)[0] . ' as E;';
46 }
47 }
48
49 /**
50 * @return bool
51 * TRUE if an update is needed.
52 */
53 public function needsUpdate() {
54 if (!file_exists($this->getAbsFileName())) {
55 return TRUE;
56 }
57
58 if ($this->getTableChecksum() !== self::extractRegex($this->getAbsFileName(), ';\(GenCodeChecksum:([a-zA-Z0-9]+)\);')) {
59 return TRUE;
60 }
61
62 return !$this->isApproxPhpMatch(
63 file_get_contents($this->getAbsFileName()),
64 $this->getRaw());
65 }
66
67 /**
68 * Run generator.
69 */
70 public function run() {
71 echo "Generating {$this->name} as " . $this->getRelFileName() . "\n";
72
73 if (empty($this->tables[$this->name]['base'])) {
74 echo "No base defined for {$this->name}, skipping output generation\n";
75 return;
76 }
77
78 $template = $this->getTemplate();
79 $template->assign('genCodeChecksum', $this->getTableChecksum());
80 $template->run('dao.tpl', $this->getAbsFileName());
81 }
82
83 /**
84 * Generate the raw PHP code for the DAO.
85 *
86 * @return string
87 */
88 public function getRaw() {
89 if (!$this->raw) {
90 $template = $this->getTemplate();
91 $template->assign('genCodeChecksum', 'NEW');
92 $this->raw = $template->fetch('dao.tpl');
93 }
94 return $this->raw;
95 }
96
97 /**
98 * @return CRM_Core_CodeGen_Util_Template
99 */
100 private function getTemplate() {
101 $template = new CRM_Core_CodeGen_Util_Template('php');
102 $template->assign('table', $this->tables[$this->name]);
103 if (empty($this->tables[$this->name]['index'])) {
104 $template->assign('indicesPhp', var_export([], 1));
105 }
106 else {
107 $template->assign('indicesPhp', var_export($this->tables[$this->name]['index'], 1));
108 }
109 $template->assign('tsFunctionName', $this->tsFunctionName);
110 $template->assign('useHelper', $this->useHelper);
111 return $template;
112 }
113
114 /**
115 * Get relative file name.
116 *
117 * @return string
118 */
119 public function getRelFileName() {
120 return $this->tables[$this->name]['fileName'];
121 }
122
123 /**
124 * Get the absolute file name.
125 *
126 * @return string
127 */
128 public function getAbsFileName() {
129 $directory = $this->config->phpCodePath . $this->tables[$this->name]['base'];
130 CRM_Core_CodeGen_Util_File::createDir($directory);
131 $absFileName = $directory . $this->getRelFileName();
132 return $absFileName;
133 }
134
135 /**
136 * Get a unique signature for the table/schema.
137 *
138 * @return string
139 */
140 protected function getTableChecksum() {
141 if (!$this->tableChecksum) {
142 $flat = [];
143 CRM_Utils_Array::flatten($this->tables[$this->name], $flat);
144 ksort($flat);
145 $this->tableChecksum = md5(json_encode($flat));
146 }
147 return $this->tableChecksum;
148 }
149
150 }