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 = '';
31 private $ext = "'civicrm'";
34 * CRM_Core_CodeGen_DAO constructor.
36 * @param \CRM_Core_CodeGen_Main $config
38 * @param string $tsFunctionName
40 public function __construct($config, $name, $tsFunctionName = 'ts') {
41 parent
::__construct($config);
43 $this->tsFunctionName
= $tsFunctionName;
44 // If this DAO belongs to an extension, add `use` statement and define EXT constant.
45 if (strpos($tsFunctionName, '::ts')) {
46 $this->tsFunctionName
= 'E::ts';
47 $this->useHelper
= 'use \\' . explode('::', $tsFunctionName)[0] . ' as E;';
48 $this->ext
= 'E::LONG_NAME';
54 * TRUE if an update is needed.
56 public function needsUpdate() {
57 if (!file_exists($this->getAbsFileName())) {
61 if ($this->getTableChecksum() !== self
::extractRegex($this->getAbsFileName(), ';\(GenCodeChecksum:([a-zA-Z0-9]+)\);')) {
65 return !$this->isApproxPhpMatch(
66 file_get_contents($this->getAbsFileName()),
73 public function run() {
74 echo "Generating {$this->name} as " . $this->getRelFileName() . "\n";
76 if (empty($this->tables
[$this->name
]['base'])) {
77 echo "No base defined for {$this->name}, skipping output generation\n";
81 $template = $this->getTemplate();
82 $template->assign('genCodeChecksum', $this->getTableChecksum());
83 $template->run('dao.tpl', $this->getAbsFileName());
87 * Generate the raw PHP code for the DAO.
91 public function getRaw() {
93 $template = $this->getTemplate();
94 $template->assign('genCodeChecksum', 'NEW');
95 $this->raw
= $template->fetch('dao.tpl');
101 * @return CRM_Core_CodeGen_Util_Template
103 private function getTemplate() {
104 $template = new CRM_Core_CodeGen_Util_Template('php');
105 $template->assign('table', $this->tables
[$this->name
]);
106 if (empty($this->tables
[$this->name
]['index'])) {
107 $template->assign('indicesPhp', var_export([], 1));
110 $template->assign('indicesPhp', var_export($this->tables
[$this->name
]['index'], 1));
112 $template->assign('tsFunctionName', $this->tsFunctionName
);
113 $template->assign('ext', $this->ext
);
114 $template->assign('useHelper', $this->useHelper
);
119 * Get relative file name.
123 public function getRelFileName() {
124 return $this->tables
[$this->name
]['fileName'];
128 * Get the absolute file name.
132 public function getAbsFileName() {
133 $directory = $this->config
->phpCodePath
. $this->tables
[$this->name
]['base'];
134 CRM_Core_CodeGen_Util_File
::createDir($directory);
135 $absFileName = $directory . $this->getRelFileName();
140 * Get a unique signature for the table/schema.
144 protected function getTableChecksum() {
145 if (!$this->tableChecksum
) {
147 CRM_Utils_Array
::flatten($this->tables
[$this->name
], $flat);
149 $this->tableChecksum
= md5(json_encode($flat));
151 return $this->tableChecksum
;