| 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 | * @var string |
| 25 | * translate function name |
| 26 | */ |
| 27 | private $tsFunctionName; |
| 28 | |
| 29 | /** |
| 30 | * CRM_Core_CodeGen_DAO constructor. |
| 31 | * |
| 32 | * @param \CRM_Core_CodeGen_Main $config |
| 33 | * @param string $name |
| 34 | * @param string $tsFunctionName |
| 35 | */ |
| 36 | public function __construct($config, $name, $tsFunctionName = 'ts') { |
| 37 | parent::__construct($config); |
| 38 | $this->name = $name; |
| 39 | $this->tsFunctionName = $tsFunctionName; |
| 40 | } |
| 41 | |
| 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 | } |
| 50 | |
| 51 | if ($this->getTableChecksum() !== self::extractRegex($this->getAbsFileName(), ';\(GenCodeChecksum:([a-zA-Z0-9]+)\);')) { |
| 52 | return TRUE; |
| 53 | } |
| 54 | |
| 55 | return !$this->isApproxPhpMatch( |
| 56 | file_get_contents($this->getAbsFileName()), |
| 57 | $this->getRaw()); |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Run generator. |
| 62 | */ |
| 63 | public function run() { |
| 64 | echo "Generating {$this->name} as " . $this->getRelFileName() . "\n"; |
| 65 | |
| 66 | if (empty($this->tables[$this->name]['base'])) { |
| 67 | echo "No base defined for {$this->name}, skipping output generation\n"; |
| 68 | return; |
| 69 | } |
| 70 | |
| 71 | $template = new CRM_Core_CodeGen_Util_Template('php'); |
| 72 | $template->assign('table', $this->tables[$this->name]); |
| 73 | if (empty($this->tables[$this->name]['index'])) { |
| 74 | $template->assign('indicesPhp', var_export([], 1)); |
| 75 | } |
| 76 | else { |
| 77 | $template->assign('indicesPhp', var_export($this->tables[$this->name]['index'], 1)); |
| 78 | } |
| 79 | $template->assign('genCodeChecksum', $this->getTableChecksum()); |
| 80 | $template->assign('tsFunctionName', $this->tsFunctionName); |
| 81 | $template->run('dao.tpl', $this->getAbsFileName()); |
| 82 | } |
| 83 | |
| 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]); |
| 93 | if (empty($this->tables[$this->name]['index'])) { |
| 94 | $template->assign('indicesPhp', var_export([], 1)); |
| 95 | } |
| 96 | else { |
| 97 | $template->assign('indicesPhp', var_export($this->tables[$this->name]['index'], 1)); |
| 98 | } |
| 99 | $template->assign('genCodeChecksum', 'NEW'); |
| 100 | $template->assign('tsFunctionName', $this->tsFunctionName); |
| 101 | $this->raw = $template->fetch('dao.tpl'); |
| 102 | } |
| 103 | return $this->raw; |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * Get relative file name. |
| 108 | * |
| 109 | * @return string |
| 110 | */ |
| 111 | public function getRelFileName() { |
| 112 | return $this->tables[$this->name]['fileName']; |
| 113 | } |
| 114 | |
| 115 | /** |
| 116 | * Get the absolute file name. |
| 117 | * |
| 118 | * @return string |
| 119 | */ |
| 120 | public function getAbsFileName() { |
| 121 | $directory = $this->config->phpCodePath . $this->tables[$this->name]['base']; |
| 122 | CRM_Core_CodeGen_Util_File::createDir($directory); |
| 123 | $absFileName = $directory . $this->getRelFileName(); |
| 124 | return $absFileName; |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * Get a unique signature for the table/schema. |
| 129 | * |
| 130 | * @return string |
| 131 | */ |
| 132 | protected function getTableChecksum() { |
| 133 | if (!$this->tableChecksum) { |
| 134 | $flat = []; |
| 135 | CRM_Utils_Array::flatten($this->tables[$this->name], $flat); |
| 136 | ksort($flat); |
| 137 | $this->tableChecksum = md5(json_encode($flat)); |
| 138 | } |
| 139 | return $this->tableChecksum; |
| 140 | } |
| 141 | |
| 142 | } |