Merge pull request #9783 from mattwire/back_to_single_lang
[civicrm-core.git] / CRM / Core / CodeGen / BaseTask.php
index 6a4a006e0809152e1841672b7207f28943b2f68d..83fcf1a410d0fec65851bcf1fe97add512ebe6c3 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,58 @@ 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) {
+    $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;
+  }
+
 }