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 | 36 | |
cdf5d407 TO |
37 | if ($this->getTableChecksum() !== self::extractRegex($this->getAbsFileName(), ';\(GenCodeChecksum:([a-zA-Z0-9]+)\);')) { |
38 | return TRUE; | |
39 | } | |
40 | ||
0f35babf TO |
41 | return !$this->isApproxPhpMatch( |
42 | file_get_contents($this->getAbsFileName()), | |
43 | $this->getRaw()); | |
a8950859 TO |
44 | } |
45 | ||
37254324 | 46 | public function run() { |
a8950859 | 47 | echo "Generating {$this->name} as " . $this->getRelFileName() . "\n"; |
5e434adf | 48 | |
a8950859 TO |
49 | if (empty($this->tables[$this->name]['base'])) { |
50 | echo "No base defined for {$this->name}, skipping output generation\n"; | |
37254324 TO |
51 | return; |
52 | } | |
5e434adf | 53 | |
37254324 | 54 | $template = new CRM_Core_CodeGen_Util_Template('php'); |
a8950859 | 55 | $template->assign('table', $this->tables[$this->name]); |
cdf5d407 | 56 | $template->assign('genCodeChecksum', $this->getTableChecksum()); |
a8950859 TO |
57 | $template->run('dao.tpl', $this->getAbsFileName()); |
58 | } | |
5e434adf | 59 | |
cdf5d407 TO |
60 | /** |
61 | * Generate the raw PHP code for the DAO. | |
62 | * | |
63 | * @return string | |
64 | */ | |
65 | public function getRaw() { | |
66 | if (!$this->raw) { | |
67 | $template = new CRM_Core_CodeGen_Util_Template('php'); | |
68 | $template->assign('table', $this->tables[$this->name]); | |
69 | $template->assign('genCodeChecksum', 'NEW'); | |
70 | $this->raw = $template->fetch('dao.tpl'); | |
71 | } | |
72 | return $this->raw; | |
73 | } | |
74 | ||
a8950859 TO |
75 | public function getRelFileName() { |
76 | return $this->tables[$this->name]['fileName']; | |
77 | } | |
78 | ||
79 | /** | |
80 | * @return string | |
81 | */ | |
82 | public function getAbsFileName() { | |
83 | $directory = $this->config->phpCodePath . $this->tables[$this->name]['base']; | |
37254324 | 84 | CRM_Core_CodeGen_Util_File::createDir($directory); |
a8950859 TO |
85 | $absFileName = $directory . $this->getRelFileName(); |
86 | return $absFileName; | |
87 | } | |
5e434adf | 88 | |
cdf5d407 TO |
89 | /** |
90 | * Get a unique signature for the table/schema. | |
91 | * | |
92 | * @return string | |
93 | */ | |
94 | protected function getTableChecksum() { | |
95 | if (!$this->tableChecksum) { | |
a8950859 TO |
96 | CRM_Utils_Array::flatten($this->tables[$this->name], $flat); |
97 | ksort($flat); | |
cdf5d407 | 98 | $this->tableChecksum = md5(json_encode($flat)); |
a8950859 | 99 | } |
cdf5d407 | 100 | return $this->tableChecksum; |
5e434adf | 101 | } |
37254324 | 102 | |
5e434adf | 103 | } |