From a8950859d73a0ecadf455621d5af16579d519b8f Mon Sep 17 00:00:00 2001 From: Tim Otten Date: Wed, 27 Jul 2016 00:26:59 -0700 Subject: [PATCH] CRM-14885 - CRM_Core_CodeGen_DAO - Use checksum to avoid recomputation --- CRM/Core/CodeGen/DAO.php | 59 +++++++++++++++++++++++++++++++++++----- xml/templates/dao.tpl | 1 + 2 files changed, 53 insertions(+), 7 deletions(-) diff --git a/CRM/Core/CodeGen/DAO.php b/CRM/Core/CodeGen/DAO.php index 13cffd5250..d182ceeb4f 100644 --- a/CRM/Core/CodeGen/DAO.php +++ b/CRM/Core/CodeGen/DAO.php @@ -10,27 +10,72 @@ class CRM_Core_CodeGen_DAO extends CRM_Core_CodeGen_BaseTask { */ public $name; + /** + * @var string + */ + private $checksum; + public function __construct($config, $name) { parent::__construct($config); $this->name = $name; } + /** + * @return bool + * TRUE if an update is needed. + */ + public function needsUpdate() { + if (!file_exists($this->getAbsFileName())) { + return TRUE; + } + return $this->getChecksum() !== self::extractChecksum($this->getAbsFileName(), ';\(GenCodeChecksum:([a-z0-9]+)\);'); + } + public function run() { - $name = $this->name; - echo "Generating $name as " . $this->tables[$name]['fileName'] . "\n"; + echo "Generating {$this->name} as " . $this->getRelFileName() . "\n"; - if (empty($this->tables[$name]['base'])) { - echo "No base defined for $name, skipping output generation\n"; + if (empty($this->tables[$this->name]['base'])) { + echo "No base defined for {$this->name}, skipping output generation\n"; return; } $template = new CRM_Core_CodeGen_Util_Template('php'); - $template->assign('table', $this->tables[$name]); + $template->assign('table', $this->tables[$this->name]); + $template->assign('genCodeChecksum', $this->getChecksum()); + $template->run('dao.tpl', $this->getAbsFileName()); + } - $directory = $this->config->phpCodePath . $this->tables[$name]['base']; + public function getRelFileName() { + return $this->tables[$this->name]['fileName']; + } + + /** + * @return string + */ + public function getAbsFileName() { + $directory = $this->config->phpCodePath . $this->tables[$this->name]['base']; CRM_Core_CodeGen_Util_File::createDir($directory); + $absFileName = $directory . $this->getRelFileName(); + return $absFileName; + } - $template->run('dao.tpl', $directory . $this->tables[$name]['fileName']); + protected static function extractChecksum($file, $regex) { + $content = file_get_contents($file); + if (preg_match($regex, $content, $matches)) { + return $matches[1]; + } + else { + return NULL; + } + } + + protected function getChecksum() { + if (!$this->checksum) { + CRM_Utils_Array::flatten($this->tables[$this->name], $flat); + ksort($flat); + $this->checksum = md5($this->config->getSourceDigest() . json_encode($flat)); + } + return $this->checksum; } } diff --git a/xml/templates/dao.tpl b/xml/templates/dao.tpl index 78f653c090..96c1718627 100644 --- a/xml/templates/dao.tpl +++ b/xml/templates/dao.tpl @@ -31,6 +31,7 @@ * * Generated from {$table.sourceFile} * {$generated} + * (GenCodeChecksum:{$genCodeChecksum}) */ require_once 'CRM/Core/DAO.php'; -- 2.25.1