Merge pull request #14266 from seamuslee001/dev_core_369
[civicrm-core.git] / CRM / Core / CodeGen / BaseTask.php
index 6a4a006e0809152e1841672b7207f28943b2f68d..c58a32ac15aa06b6fd140bcfd522402b37549c6a 100644 (file)
@@ -4,11 +4,18 @@
  * Class CRM_Core_CodeGen_BaseTask
  */
 abstract class CRM_Core_CodeGen_BaseTask implements CRM_Core_CodeGen_ITask {
+  /**
+   * @var CRM_Core_CodeGen_Main
+   */
   protected $config;
 
+  protected $tables;
+
   /**
+   * @param CRM_Core_CodeGen_Main $config
    */
-  public function __construct() {
+  public function __construct($config) {
+    $this->setConfig($config);
   }
 
   /**
@@ -22,4 +29,56 @@ abstract class CRM_Core_CodeGen_BaseTask implements CRM_Core_CodeGen_ITask {
     $this->tables = $this->config->tables;
   }
 
+  /**
+   * @return bool
+   *   TRUE if an update is needed.
+   */
+  public function needsUpdate() {
+    return TRUE;
+  }
+
+  /**
+   * Extract a single regex from a file.
+   *
+   * @param string $file
+   *   File name
+   * @param string $regex
+   *   A pattern to match. Ex: "foo=([a-z]+)".
+   * @return string|NULL
+   *   The value matched.
+   */
+  protected static function extractRegex($file, $regex) {
+    $content = file_get_contents($file);
+    if (preg_match($regex, $content, $matches)) {
+      return $matches[1];
+    }
+    else {
+      return NULL;
+    }
+  }
+
+  /**
+   * 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) {
+    foreach (['actual', 'expected'] as $var) {
+      $$var = CRM_Core_CodeGen_Util_ArraySyntaxConverter::convert($$var);
+      $$var = preg_replace(';\(GenCodeChecksum:([a-zA-Z0-9]+)\);', '', $$var);
+      $$var = strtolower(preg_replace(';[ \r\n\t];', '', $$var));
+    }
+    return $actual === $expected;
+  }
+
 }