4 * Create DAO ORM classes.
6 class CRM_Core_CodeGen_DAO
extends CRM_Core_CodeGen_BaseTask
{
16 private $tableChecksum;
25 * translate function name
27 private $tsFunctionName;
29 private $useHelper = '';
32 * CRM_Core_CodeGen_DAO constructor.
34 * @param \CRM_Core_CodeGen_Main $config
36 * @param string $tsFunctionName
38 public function __construct($config, $name, $tsFunctionName = 'ts') {
39 parent
::__construct($config);
41 $this->tsFunctionName
= $tsFunctionName;
42 // Cleanup helper class with a use statement
43 if (strpos($tsFunctionName, '::ts')) {
44 $this->tsFunctionName
= 'E::ts';
45 $this->useHelper
= 'use \\' . explode('::', $tsFunctionName)[0] . ' as E;';
51 * TRUE if an update is needed.
53 public function needsUpdate() {
54 if (!file_exists($this->getAbsFileName())) {
58 if ($this->getTableChecksum() !== self
::extractRegex($this->getAbsFileName(), ';\(GenCodeChecksum:([a-zA-Z0-9]+)\);')) {
62 return !$this->isApproxPhpMatch(
63 file_get_contents($this->getAbsFileName()),
70 public function run() {
71 echo "Generating {$this->name} as " . $this->getRelFileName() . "\n";
73 if (empty($this->tables
[$this->name
]['base'])) {
74 echo "No base defined for {$this->name}, skipping output generation\n";
78 $template = $this->getTemplate();
79 $template->assign('genCodeChecksum', $this->getTableChecksum());
80 $template->run('dao.tpl', $this->getAbsFileName());
84 * Generate the raw PHP code for the DAO.
88 public function getRaw() {
90 $template = $this->getTemplate();
91 $template->assign('genCodeChecksum', 'NEW');
92 $this->raw
= $template->fetch('dao.tpl');
98 * @return CRM_Core_CodeGen_Util_Template
100 private function getTemplate() {
101 $template = new CRM_Core_CodeGen_Util_Template('php');
102 $template->assign('table', $this->tables
[$this->name
]);
103 if (empty($this->tables
[$this->name
]['index'])) {
104 $template->assign('indicesPhp', var_export([], 1));
107 $template->assign('indicesPhp', var_export($this->tables
[$this->name
]['index'], 1));
109 $template->assign('tsFunctionName', $this->tsFunctionName
);
110 $template->assign('useHelper', $this->useHelper
);
115 * Get relative file name.
119 public function getRelFileName() {
120 return $this->tables
[$this->name
]['fileName'];
124 * Get the absolute file name.
128 public function getAbsFileName() {
129 $directory = $this->config
->phpCodePath
. $this->tables
[$this->name
]['base'];
130 CRM_Core_CodeGen_Util_File
::createDir($directory);
131 $absFileName = $directory . $this->getRelFileName();
136 * Get a unique signature for the table/schema.
140 protected function getTableChecksum() {
141 if (!$this->tableChecksum
) {
143 CRM_Utils_Array
::flatten($this->tables
[$this->name
], $flat);
145 $this->tableChecksum
= md5(json_encode($flat));
147 return $this->tableChecksum
;