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