CRM-14885 - CRM_Core_CodeGen_Main::getTasks - Return instances instead of class names
authorTim Otten <totten@civicrm.org>
Wed, 27 Jul 2016 05:28:22 +0000 (22:28 -0700)
committerTim Otten <totten@civicrm.org>
Thu, 28 Jul 2016 00:20:16 +0000 (17:20 -0700)
CRM/Core/CodeGen/BaseTask.php
CRM/Core/CodeGen/ITask.php
CRM/Core/CodeGen/Main.php
CRM/Core/CodeGen/Schema.php

index 6a4a006e0809152e1841672b7207f28943b2f68d..5bff34541349e11572b7c34af0fe9130b138aaaa 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);
   }
 
   /**
index f5f4561401c496e0601cce57382ecfd7ff0d0419..4797ca2629a0efd03252324e057d0e6cea9495e1 100644 (file)
@@ -4,13 +4,6 @@
  * Implemented by CG tasks
  */
 interface CRM_Core_CodeGen_ITask {
-  /**
-   * Make configuration object available to the task.
-   *
-   * @param $config
-   *   Is currently the CRM_Core_CodeGen_Main object.
-   */
-  public function setConfig($config);
 
   /**
    * Perform the task.
index 6a5ff43872434aaea74ac1767514fb7573b08681..99ce0e153a199ca3f6b2f9cca85bb591420580ad 100644 (file)
@@ -65,16 +65,6 @@ class CRM_Core_CodeGen_Main {
    * Automatically generate a variety of files.
    */
   public function main() {
-    if (!empty($this->digestPath) && file_exists($this->digestPath) && $this->hasExpectedFiles()) {
-      if ($this->getDigest() === file_get_contents($this->digestPath)) {
-        echo "GenCode has previously executed. To force execution, please (a) omit CIVICRM_GENCODE_DIGEST\n";
-        echo "or (b) remove {$this->digestPath} or (c) call GenCode with new parameters.\n";
-        exit();
-      }
-      // Once we start GenCode, the old build is invalid
-      unlink($this->digestPath);
-    }
-
     echo "\ncivicrm_domain.version := " . $this->db_version . "\n\n";
     if ($this->buildVersion < 1.1) {
       echo "The Database is not compatible for this version";
@@ -97,78 +87,24 @@ Alternatively you can get a version of CiviCRM that matches your PHP version
     $this->database = $specification->database;
     $this->tables = $specification->tables;
 
-    $this->runAllTasks();
-
-    if (!empty($this->digestPath)) {
-      file_put_contents($this->digestPath, $this->getDigest());
-    }
-  }
-
-  public function runAllTasks() {
-    // TODO: This configuration can be manipulated dynamically.
-    $components = $this->getTasks();
-    foreach ($components as $component) {
-      $task = new $component($this);
-
-      if (is_a($task, 'CRM_Core_CodeGen_ITask')) {
-        $task->setConfig($this);
-        $task->run();
-      }
-      else {
-        echo "Bad news: we tried to run a codegen task of an unrecognized type: {$component}\n";
-        exit();
-      }
+    foreach ($this->getTasks() as $task) {
+      $task->run();
     }
   }
 
   /**
    * @return array
-   *   Array of class names; each class implements CRM_Core_CodeGen_ITask
+   *   Array<CRM_Core_CodeGen_ITask>.
+   * @throws \Exception
    */
   public function getTasks() {
-    $components = array(
-      'CRM_Core_CodeGen_Config',
-      'CRM_Core_CodeGen_Reflection',
-      'CRM_Core_CodeGen_Schema',
-      'CRM_Core_CodeGen_DAO',
-      //'CRM_Core_CodeGen_Test',
-      'CRM_Core_CodeGen_I18n',
-    );
-    return $components;
-  }
-
-  /**
-   * Compute a digest based on the inputs to the code-generator (ie the properties
-   * of the codegen and the source files loaded by the codegen).
-   *
-   * @return string
-   */
-  public function getDigest() {
-    if ($this->digest === NULL) {
-      $srcDir = CRM_Core_CodeGen_Util_File::findCoreSourceDir();
-      $files = CRM_Core_CodeGen_Util_File::findManyFiles(array(
-        array("$srcDir/CRM/Core/CodeGen", '*.php'),
-        array("$srcDir/xml", "*.php"),
-        array("$srcDir/xml", "*.tpl"),
-        array("$srcDir/xml", "*.xml"),
-      ));
-
-      $properties = var_export(array(
-        CRM_Core_CodeGen_Util_File::digestAll($files),
-        $this->buildVersion,
-        $this->db_version,
-        $this->cms,
-        $this->CoreDAOCodePath,
-        $this->sqlCodePath,
-        $this->phpCodePath,
-        $this->tplCodePath,
-        $this->schemaPath,
-        $this->getTasks(),
-      ), TRUE);
-
-      $this->digest = md5($properties);
-    }
-    return $this->digest;
+    $tasks = array();
+    $tasks[] = new CRM_Core_CodeGen_Config($this);
+    $tasks[] = new CRM_Core_CodeGen_Reflection($this);
+    $tasks[] = new CRM_Core_CodeGen_Schema($this);
+    $tasks[] = new CRM_Core_CodeGen_DAO($this);
+    $tasks[] = new CRM_Core_CodeGen_I18n($this);
+    return $tasks;
   }
 
   /**
index c8b0a04f9d77da6a9cf1459e79ebfc1266d2c300..51db8af5e84c769cb5837c35f1c0d1bab800ac17 100644 (file)
@@ -6,8 +6,8 @@
 class CRM_Core_CodeGen_Schema extends CRM_Core_CodeGen_BaseTask {
   /**
    */
-  public function __construct() {
-    parent::__construct();
+  public function __construct($config) {
+    parent::__construct($config);
     $this->locales = $this->findLocales();
   }