Merge pull request #9783 from mattwire/back_to_single_lang
[civicrm-core.git] / CRM / Core / CodeGen / DAO.php
1 <?php
2
3 /**
4 * Create DAO ORM classes.
5 */
6 class CRM_Core_CodeGen_DAO extends CRM_Core_CodeGen_BaseTask {
7
8 /**
9 * @var string
10 */
11 public $name;
12
13 /**
14 * @var string
15 */
16 private $tableChecksum;
17
18 /**
19 * @var string
20 */
21 private $raw;
22
23 /**
24 * CRM_Core_CodeGen_DAO constructor.
25 *
26 * @param \CRM_Core_CodeGen_Main $config
27 * @param string $name
28 */
29 public function __construct($config, $name) {
30 parent::__construct($config);
31 $this->name = $name;
32 }
33
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 }
42
43 if ($this->getTableChecksum() !== self::extractRegex($this->getAbsFileName(), ';\(GenCodeChecksum:([a-zA-Z0-9]+)\);')) {
44 return TRUE;
45 }
46
47 return !$this->isApproxPhpMatch(
48 file_get_contents($this->getAbsFileName()),
49 $this->getRaw());
50 }
51
52 /**
53 * Run generator.
54 */
55 public function run() {
56 echo "Generating {$this->name} as " . $this->getRelFileName() . "\n";
57
58 if (empty($this->tables[$this->name]['base'])) {
59 echo "No base defined for {$this->name}, skipping output generation\n";
60 return;
61 }
62
63 $template = new CRM_Core_CodeGen_Util_Template('php');
64 $template->assign('table', $this->tables[$this->name]);
65 if (empty($this->tables[$this->name]['index'])) {
66 $template->assign('indicesPhp', var_export(array(), 1));
67 }
68 else {
69 $template->assign('indicesPhp', var_export($this->tables[$this->name]['index'], 1));
70 }
71 $template->assign('genCodeChecksum', $this->getTableChecksum());
72 $template->run('dao.tpl', $this->getAbsFileName());
73 }
74
75 /**
76 * Generate the raw PHP code for the DAO.
77 *
78 * @return string
79 */
80 public function getRaw() {
81 if (!$this->raw) {
82 $template = new CRM_Core_CodeGen_Util_Template('php');
83 $template->assign('table', $this->tables[$this->name]);
84 if (empty($this->tables[$this->name]['index'])) {
85 $template->assign('indicesPhp', var_export(array(), 1));
86 }
87 else {
88 $template->assign('indicesPhp', var_export($this->tables[$this->name]['index'], 1));
89 }
90 $template->assign('genCodeChecksum', 'NEW');
91 $this->raw = $template->fetch('dao.tpl');
92 }
93 return $this->raw;
94 }
95
96 /**
97 * Get relative file name.
98 *
99 * @return string
100 */
101 public function getRelFileName() {
102 return $this->tables[$this->name]['fileName'];
103 }
104
105 /**
106 * Get the absolute file name.
107 *
108 * @return string
109 */
110 public function getAbsFileName() {
111 $directory = $this->config->phpCodePath . $this->tables[$this->name]['base'];
112 CRM_Core_CodeGen_Util_File::createDir($directory);
113 $absFileName = $directory . $this->getRelFileName();
114 return $absFileName;
115 }
116
117 /**
118 * Get a unique signature for the table/schema.
119 *
120 * @return string
121 */
122 protected function getTableChecksum() {
123 if (!$this->tableChecksum) {
124 $flat = array();
125 CRM_Utils_Array::flatten($this->tables[$this->name], $flat);
126 ksort($flat);
127 $this->tableChecksum = md5(json_encode($flat));
128 }
129 return $this->tableChecksum;
130 }
131
132 }