Merge pull request #14734 from yashodha/dev-1104
[civicrm-core.git] / CRM / Core / CodeGen / DAO.php
CommitLineData
5e434adf
ARW
1<?php
2
3/**
4 * Create DAO ORM classes.
5 */
6class 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
8af97d90
SL
23 /**
24 * @var string
25 * translate function name
26 */
27 private $tsFunctionName;
28
bc854509 29 /**
30 * CRM_Core_CodeGen_DAO constructor.
31 *
32 * @param \CRM_Core_CodeGen_Main $config
33 * @param string $name
8af97d90 34 * @param string $tsFunctionName
bc854509 35 */
8af97d90 36 public function __construct($config, $name, $tsFunctionName = 'ts') {
37254324
TO
37 parent::__construct($config);
38 $this->name = $name;
8af97d90 39 $this->tsFunctionName = $tsFunctionName;
5e434adf
ARW
40 }
41
a8950859
TO
42 /**
43 * @return bool
44 * TRUE if an update is needed.
45 */
46 public function needsUpdate() {
47 if (!file_exists($this->getAbsFileName())) {
48 return TRUE;
49 }
cdf5d407 50
cdf5d407
TO
51 if ($this->getTableChecksum() !== self::extractRegex($this->getAbsFileName(), ';\(GenCodeChecksum:([a-zA-Z0-9]+)\);')) {
52 return TRUE;
53 }
54
0f35babf
TO
55 return !$this->isApproxPhpMatch(
56 file_get_contents($this->getAbsFileName()),
57 $this->getRaw());
a8950859
TO
58 }
59
f2ac86d1 60 /**
61 * Run generator.
62 */
37254324 63 public function run() {
a8950859 64 echo "Generating {$this->name} as " . $this->getRelFileName() . "\n";
5e434adf 65
a8950859
TO
66 if (empty($this->tables[$this->name]['base'])) {
67 echo "No base defined for {$this->name}, skipping output generation\n";
37254324
TO
68 return;
69 }
5e434adf 70
37254324 71 $template = new CRM_Core_CodeGen_Util_Template('php');
a8950859 72 $template->assign('table', $this->tables[$this->name]);
6b86d84f 73 if (empty($this->tables[$this->name]['index'])) {
be2fb01f 74 $template->assign('indicesPhp', var_export([], 1));
6b86d84f
AS
75 }
76 else {
77 $template->assign('indicesPhp', var_export($this->tables[$this->name]['index'], 1));
78 }
cdf5d407 79 $template->assign('genCodeChecksum', $this->getTableChecksum());
8af97d90 80 $template->assign('tsFunctionName', $this->tsFunctionName);
a8950859
TO
81 $template->run('dao.tpl', $this->getAbsFileName());
82 }
5e434adf 83
cdf5d407
TO
84 /**
85 * Generate the raw PHP code for the DAO.
86 *
87 * @return string
88 */
89 public function getRaw() {
90 if (!$this->raw) {
91 $template = new CRM_Core_CodeGen_Util_Template('php');
92 $template->assign('table', $this->tables[$this->name]);
6b86d84f 93 if (empty($this->tables[$this->name]['index'])) {
be2fb01f 94 $template->assign('indicesPhp', var_export([], 1));
6b86d84f
AS
95 }
96 else {
97 $template->assign('indicesPhp', var_export($this->tables[$this->name]['index'], 1));
98 }
cdf5d407 99 $template->assign('genCodeChecksum', 'NEW');
8af97d90 100 $template->assign('tsFunctionName', $this->tsFunctionName);
cdf5d407
TO
101 $this->raw = $template->fetch('dao.tpl');
102 }
103 return $this->raw;
104 }
105
f2ac86d1 106 /**
107 * Get relative file name.
108 *
109 * @return string
110 */
a8950859
TO
111 public function getRelFileName() {
112 return $this->tables[$this->name]['fileName'];
113 }
114
115 /**
f2ac86d1 116 * Get the absolute file name.
117 *
a8950859
TO
118 * @return string
119 */
120 public function getAbsFileName() {
121 $directory = $this->config->phpCodePath . $this->tables[$this->name]['base'];
37254324 122 CRM_Core_CodeGen_Util_File::createDir($directory);
a8950859
TO
123 $absFileName = $directory . $this->getRelFileName();
124 return $absFileName;
125 }
5e434adf 126
cdf5d407
TO
127 /**
128 * Get a unique signature for the table/schema.
129 *
130 * @return string
131 */
132 protected function getTableChecksum() {
133 if (!$this->tableChecksum) {
be2fb01f 134 $flat = [];
a8950859
TO
135 CRM_Utils_Array::flatten($this->tables[$this->name], $flat);
136 ksort($flat);
cdf5d407 137 $this->tableChecksum = md5(json_encode($flat));
a8950859 138 }
cdf5d407 139 return $this->tableChecksum;
5e434adf 140 }
37254324 141
5e434adf 142}