CRM-21787: Keep up-to-date without running GenCode on all builds
[civicrm-core.git] / CRM / Core / ClassLoader.php
index d5f8b2113fd98a0dd8375018878ae0c6297af132..3a455777bbcb91d1d87cb82acbaeb8331b198ea9 100644 (file)
@@ -90,6 +90,28 @@ class CRM_Core_ClassLoader {
     );
   }
 
+  /**
+   * Requires the autoload.php generated by composer
+   *
+   * @return void
+   */
+  protected function requireComposerAutoload() {
+    // We are trying to locate 'vendor/autoload.php'. When installing CiviCRM
+    // manually from the built tarball, that will be two directories up in the
+    // civicrm-core directory. However, if civicrm-core was installed via
+    // composer as a library, that'll be 5 directories up where composer was
+    // run (ex. the Drupal root on a Drupal 8 site).
+    $civicrm_base_path = dirname(dirname(__DIR__));
+    $top_path = dirname(dirname(dirname(dirname(dirname(__DIR__)))));
+
+    if (file_exists($civicrm_base_path . '/vendor/autoload.php')) {
+      require_once $civicrm_base_path . '/vendor/autoload.php';
+    }
+    elseif (file_exists($top_path . '/vendor/autoload.php')) {
+      require_once $top_path . '/vendor/autoload.php';
+    }
+  }
+
   /**
    * Registers this instance as an autoloader.
    *
@@ -104,7 +126,7 @@ class CRM_Core_ClassLoader {
     }
     $civicrm_base_path = dirname(dirname(__DIR__));
 
-    require_once dirname(dirname(__DIR__)) . '/vendor/autoload.php';
+    $this->requireComposerAutoload();
 
     // we do this to prevent a autoloader errors with joomla / 3rd party packages
     // use absolute path since we dont know the content of include_path as yet
@@ -123,7 +145,8 @@ class CRM_Core_ClassLoader {
     );
     $include_paths = implode(PATH_SEPARATOR, $include_paths);
     set_include_path($include_paths . PATH_SEPARATOR . get_include_path());
-    require_once "$civicrm_base_path/vendor/autoload.php";
+    // @todo Why do we need to load this again?
+    $this->requireComposerAutoload();
   }
 
   /**
@@ -182,7 +205,7 @@ class CRM_Core_ClassLoader {
     if (
       // Only load classes that clearly belong to CiviCRM.
       // Note: api/v3 does not use classes, but api_v3's test-suite does
-      (0 === strncmp($class, 'CRM_', 4) || 0 === strncmp($class, 'api_v3_', 7) || 0 === strncmp($class, 'WebTest_', 8) || 0 === strncmp($class, 'E2E_', 4)) &&
+      (0 === strncmp($class, 'CRM_', 4) || 0 === strncmp($class, 'CRMTraits', 9) || 0 === strncmp($class, 'api_v3_', 7) || 0 === strncmp($class, 'WebTest_', 8) || 0 === strncmp($class, 'E2E_', 4)) &&
       // Do not load PHP 5.3 namespaced classes.
       // (in a future version, maybe)
       FALSE === strpos($class, '\\')