-
-
Notifications
You must be signed in to change notification settings - Fork 828
/
Copy pathDAO.php
119 lines (103 loc) · 2.63 KB
/
DAO.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
<?php
/**
* Create DAO ORM classes.
*/
class CRM_Core_CodeGen_DAO extends CRM_Core_CodeGen_BaseTask {
/**
* @var string
*/
public $name;
/**
* @var string
*/
private $tableChecksum;
/**
* @var string
*/
private $raw;
/**
* CRM_Core_CodeGen_DAO constructor.
*
* @param \CRM_Core_CodeGen_Main $config
* @param string $name
*/
public function __construct($config, $name) {
parent::__construct($config);
$this->name = $name;
}
/**
* @return bool
* TRUE if an update is needed.
*/
public function needsUpdate() {
if (!file_exists($this->getAbsFileName())) {
return TRUE;
}
if ($this->getTableChecksum() !== self::extractRegex($this->getAbsFileName(), ';\(GenCodeChecksum:([a-zA-Z0-9]+)\);')) {
return TRUE;
}
return !$this->isApproxPhpMatch(
file_get_contents($this->getAbsFileName()),
$this->getRaw());
}
/**
* Run generator.
*/
public function run() {
echo "Generating {$this->name} as " . $this->getRelFileName() . "\n";
if (empty($this->tables[$this->name]['base'])) {
echo "No base defined for {$this->name}, skipping output generation\n";
return;
}
$template = new CRM_Core_CodeGen_Util_Template('php');
$template->assign('table', $this->tables[$this->name]);
$template->assign('genCodeChecksum', $this->getTableChecksum());
$template->run('dao.tpl', $this->getAbsFileName());
}
/**
* Generate the raw PHP code for the DAO.
*
* @return string
*/
public function getRaw() {
if (!$this->raw) {
$template = new CRM_Core_CodeGen_Util_Template('php');
$template->assign('table', $this->tables[$this->name]);
$template->assign('genCodeChecksum', 'NEW');
$this->raw = $template->fetch('dao.tpl');
}
return $this->raw;
}
/**
* Get relative file name.
*
* @return string
*/
public function getRelFileName() {
return $this->tables[$this->name]['fileName'];
}
/**
* Get the absolute file name.
*
* @return string
*/
public function getAbsFileName() {
$directory = $this->config->phpCodePath . $this->tables[$this->name]['base'];
CRM_Core_CodeGen_Util_File::createDir($directory);
$absFileName = $directory . $this->getRelFileName();
return $absFileName;
}
/**
* Get a unique signature for the table/schema.
*
* @return string
*/
protected function getTableChecksum() {
if (!$this->tableChecksum) {
CRM_Utils_Array::flatten($this->tables[$this->name], $flat);
ksort($flat);
$this->tableChecksum = md5(json_encode($flat));
}
return $this->tableChecksum;
}
}