瀏覽代碼

Add .composer/autoload.php in namespace loader

François Pluchino 13 年之前
父節點
當前提交
0bcf3c26d9
共有 1 個文件被更改,包括 18 次插入17 次删除
  1. 18 17
      src/Composer/Trigger/TriggerDispatcher.php

+ 18 - 17
src/Composer/Trigger/TriggerDispatcher.php

@@ -103,11 +103,12 @@ class TriggerDispatcher
     protected function getListeners(TriggerEvent $event)
     {
         $package = $this->composer->getPackage();
+        $vendorDir = $this->composer->getInstallationManager()->getVendorPath(true);
+        $autoloadFile = $vendorDir . '/.composer/autoload.php';
         $ex = $package->getExtra();
         $al = $package->getAutoload();
         $searchListeners = array();
-        $searchNamespaces = array();
-        $listeners = array();
+        $listeners = array();
         $namespaces = array();
 
         // get classes
@@ -117,34 +118,34 @@ class TriggerDispatcher
             }
         }
 
-        // get namespaces
+        // get autoload namespaces
+        if (file_exists($autoloadFile)) {
+            $this->loader = require $autoloadFile;
+        }
+
+        $namespaces = $this->loader->getPrefixes();
+
+        // get namespaces in composer.json project
         if (isset($al['psr-0'])) {
             foreach ($al['psr-0'] as $ns => $path) {
-                $path = trim(realpath('.') . '/' . $path, '/');
-                $searchNamespaces[$ns] = $path;
+                if (!isset($namespaces[str_replace('\\', '\\\\', $ns)])) {
+                    $this->loader->add($ns, trim(realpath('.').'/'.$path, '/'));
+                }
             }
+
+            $this->loader->register();
+            $namespaces = $this->loader->getPrefixes();
         }
 
         // filter class::method have not a namespace registered
-        foreach ($searchNamespaces as $ns => $path) {
+        foreach ($namespaces as $ns => $path) {
             foreach ($searchListeners as $method) {
                 if (0 === strpos($method, $ns)) {
                     $listeners[] = $method;
-
-                    if (!in_array($ns, array_keys($namespaces))) {
-                        $namespaces[$ns] = $path;
-                    }
                 }
             }
         }
 
-        // register namespaces in class loader
-        foreach ($namespaces as $ns => $path) {
-            $this->loader->add($ns, $path);
-        }
-
-        $this->loader->register();
-
         return $listeners;
     }
 }