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 | |
37254324 TO |
23 | public function __construct($config, $name) { |
24 | parent::__construct($config); | |
25 | $this->name = $name; | |
5e434adf ARW |
26 | } |
27 | ||
a8950859 TO |
28 | /** |
29 | * @return bool | |
30 | * TRUE if an update is needed. | |
31 | */ | |
32 | public function needsUpdate() { | |
33 | if (!file_exists($this->getAbsFileName())) { | |
34 | return TRUE; | |
35 | } | |
cdf5d407 TO |
36 | |
37 | // Has the table metadata changed since the DAO was generated? | |
38 | if ($this->getTableChecksum() !== self::extractRegex($this->getAbsFileName(), ';\(GenCodeChecksum:([a-zA-Z0-9]+)\);')) { | |
39 | return TRUE; | |
40 | } | |
41 | ||
42 | // Has someone messed with the logic of the DAO? | |
43 | // Compare suggested+actual code (modulo whitespace). | |
44 | $stripped['actual'] = file_get_contents($this->getAbsFileName()); | |
45 | $stripped['expect'] = $this->getRaw(); | |
46 | ||
47 | foreach (array('actual', 'expect') as $key) { | |
48 | $stripped[$key] = preg_replace(';\(GenCodeChecksum:([a-zA-Z0-9]+)\);', '', $stripped[$key]); | |
49 | $stripped[$key] = preg_replace(';[ \r\n\t];', '', $stripped[$key]); | |
50 | } | |
51 | return $stripped['actual'] !== $stripped['expect']; | |
a8950859 TO |
52 | } |
53 | ||
37254324 | 54 | public function run() { |
a8950859 | 55 | echo "Generating {$this->name} as " . $this->getRelFileName() . "\n"; |
5e434adf | 56 | |
a8950859 TO |
57 | if (empty($this->tables[$this->name]['base'])) { |
58 | echo "No base defined for {$this->name}, skipping output generation\n"; | |
37254324 TO |
59 | return; |
60 | } | |
5e434adf | 61 | |
37254324 | 62 | $template = new CRM_Core_CodeGen_Util_Template('php'); |
a8950859 | 63 | $template->assign('table', $this->tables[$this->name]); |
cdf5d407 | 64 | $template->assign('genCodeChecksum', $this->getTableChecksum()); |
a8950859 TO |
65 | $template->run('dao.tpl', $this->getAbsFileName()); |
66 | } | |
5e434adf | 67 | |
cdf5d407 TO |
68 | /** |
69 | * Generate the raw PHP code for the DAO. | |
70 | * | |
71 | * @return string | |
72 | */ | |
73 | public function getRaw() { | |
74 | if (!$this->raw) { | |
75 | $template = new CRM_Core_CodeGen_Util_Template('php'); | |
76 | $template->assign('table', $this->tables[$this->name]); | |
77 | $template->assign('genCodeChecksum', 'NEW'); | |
78 | $this->raw = $template->fetch('dao.tpl'); | |
79 | } | |
80 | return $this->raw; | |
81 | } | |
82 | ||
a8950859 TO |
83 | public function getRelFileName() { |
84 | return $this->tables[$this->name]['fileName']; | |
85 | } | |
86 | ||
87 | /** | |
88 | * @return string | |
89 | */ | |
90 | public function getAbsFileName() { | |
91 | $directory = $this->config->phpCodePath . $this->tables[$this->name]['base']; | |
37254324 | 92 | CRM_Core_CodeGen_Util_File::createDir($directory); |
a8950859 TO |
93 | $absFileName = $directory . $this->getRelFileName(); |
94 | return $absFileName; | |
95 | } | |
5e434adf | 96 | |
cdf5d407 TO |
97 | /** |
98 | * Get a unique signature for the table/schema. | |
99 | * | |
100 | * @return string | |
101 | */ | |
102 | protected function getTableChecksum() { | |
103 | if (!$this->tableChecksum) { | |
a8950859 TO |
104 | CRM_Utils_Array::flatten($this->tables[$this->name], $flat); |
105 | ksort($flat); | |
cdf5d407 | 106 | $this->tableChecksum = md5(json_encode($flat)); |
a8950859 | 107 | } |
cdf5d407 | 108 | return $this->tableChecksum; |
5e434adf | 109 | } |
37254324 | 110 | |
5e434adf | 111 | } |