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