}
}
+ /**
+ * Determine if two snippets of PHP code are approximately equivalent.
+ *
+ * This includes exceptions to equivalence for (a) whitespace and (b)
+ * the token "GenCodeChecksum".
+ *
+ * This is useful for determining if someone has manually mucked with
+ * one the files. However, it's not perfect -- because whitespace changes
+ * are not detected. Hence, it's good to use in combination with another
+ * heuristic.
+ *
+ * @param $actual
+ * @param $expected
+ * @return bool
+ */
+ protected function isApproxPhpMatch($actual, $expected) {
+ $actual = preg_replace(';\(GenCodeChecksum:([a-zA-Z0-9]+)\);', '', $actual);
+ $actual = preg_replace(';[ \r\n\t];', '', $actual);
+
+ $expected = preg_replace(';\(GenCodeChecksum:([a-zA-Z0-9]+)\);', '',
+ $expected);
+ $expected = preg_replace(';[ \r\n\t];', '', $expected);
+
+ return $actual === $expected;
+ }
+
}
return TRUE;
}
- // Has the table metadata changed since the DAO was generated?
if ($this->getTableChecksum() !== self::extractRegex($this->getAbsFileName(), ';\(GenCodeChecksum:([a-zA-Z0-9]+)\);')) {
return TRUE;
}
- // Has someone messed with the logic of the DAO?
- // Compare suggested+actual code (modulo whitespace).
- $stripped['actual'] = file_get_contents($this->getAbsFileName());
- $stripped['expect'] = $this->getRaw();
-
- foreach (array('actual', 'expect') as $key) {
- $stripped[$key] = preg_replace(';\(GenCodeChecksum:([a-zA-Z0-9]+)\);', '', $stripped[$key]);
- $stripped[$key] = preg_replace(';[ \r\n\t];', '', $stripped[$key]);
- }
- return $stripped['actual'] !== $stripped['expect'];
+ return !$this->isApproxPhpMatch(
+ file_get_contents($this->getAbsFileName()),
+ $this->getRaw());
}
public function run() {
protected $checksum;
+ /**
+ * @var string
+ */
+ private $raw;
+
/**
* @return bool
* TRUE if an update is needed.
if (!file_exists($this->getAbsFileName())) {
return TRUE;
}
- return $this->getChecksum() !== self::extractRegex($this->getAbsFileName(), ';\(GenCodeChecksum:([a-z0-9]+)\);');
+
+ if ($this->getSchemaChecksum() !== self::extractRegex($this->getAbsFileName(), ';\(GenCodeChecksum:([a-zA-Z0-9]+)\);')) {
+ return TRUE;
+ }
+
+ return !$this->isApproxPhpMatch(
+ file_get_contents($this->getAbsFileName()),
+ $this->getRaw());
}
+
public function run() {
echo "Generating table list\n";
$template = new CRM_Core_CodeGen_Util_Template('php');
$template->assign('tables', $this->tables);
- $template->assign('genCodeChecksum', $this->getChecksum());
+ $template->assign('genCodeChecksum', $this->getSchemaChecksum());
$template->run('listAll.tpl', $this->getAbsFileName());
}
+ /**
+ * Generate the raw PHP code for the data file.
+ *
+ * @return string
+ */
+ public function getRaw() {
+ if (!$this->raw) {
+ $template = new CRM_Core_CodeGen_Util_Template('php');
+ $template->assign('tables', $this->tables);
+ $template->assign('genCodeChecksum', 'NEW');
+ $this->raw = $template->fetch('listAll.tpl');
+ }
+ return $this->raw;
+ }
+
/**
* @return string
*/
return $this->config->CoreDAOCodePath . "AllCoreTables.data.php";
}
- protected function getChecksum() {
+ protected function getSchemaChecksum() {
if (!$this->checksum) {
CRM_Utils_Array::flatten($this->tables, $flat);
ksort($flat);
- $this->checksum = md5($this->config->getSourceDigest() . json_encode($flat));
+ $this->checksum = md5(json_encode($flat));
}
return $this->checksum;
}