فهرست منبع

Packages can define include paths.

Christoph Hochstrasser 13 سال پیش
والد
کامیت
f369104bb1

+ 7 - 0
res/composer-schema.json

@@ -146,6 +146,13 @@
                 "type": "string"
             }
         },
+        "include-paths": {
+            "type": ["array"],
+            "description": "A list of directories which should get added to PHP's include path.",
+            "items": {
+                "type": "string"
+            }
+        },
         "scripts": {
             "type": ["object"],
             "description": "Scripts listeners that will be executed before/after some events.",

+ 27 - 0
src/Composer/Autoload/AutoloadGenerator.php

@@ -35,6 +35,11 @@ if (!class_exists('Composer\\Autoload\\ClassLoader', false)) {
     require __DIR__.'/ClassLoader.php';
 }
 
+$includePaths = require __DIR__.'/include_paths.php';
+array_unshift($includePaths, get_include_path());
+
+set_include_path(join(PATH_SEPARATOR, $includePaths));
+
 return call_user_func(function() {
     $loader = new \Composer\Autoload\ClassLoader();
 
@@ -134,6 +139,7 @@ EOF;
         file_put_contents($targetDir.'/autoload.php', $autoloadFile);
         file_put_contents($targetDir.'/autoload_namespaces.php', $namespacesFile);
         file_put_contents($targetDir.'/autoload_classmap.php', $classmapFile);
+        file_put_contents($targetDir.'/include_paths.php', $this->getIncludePathsFile($packageMap));
         copy(__DIR__.'/ClassLoader.php', $targetDir.'/ClassLoader.php');
     }
 
@@ -205,4 +211,25 @@ EOF;
 
         return $loader;
     }
+
+    protected function getIncludePathsFile(array $packageMap)
+    {
+        $includePaths = array();
+
+        foreach ($packageMap as $item) {
+            list($package, $installPath) = $item;
+
+            if (null !== $package->getTargetDir()) {
+                $installPath = substr($installPath, 0, -strlen('/'.$package->getTargetDir()));
+            }
+
+            foreach ($package->getIncludePaths() as $includePath) {
+                $includePaths[] = empty($installPath) ? $includePath : $installPath.'/'.$includePath;
+            }
+        }
+
+        return sprintf(
+            "<?php\nreturn %s;\n", var_export($includePaths, true)
+        );
+    }
 }

+ 4 - 0
src/Composer/Package/AliasPackage.php

@@ -238,6 +238,10 @@ class AliasPackage extends BasePackage
     {
         return $this->aliasOf->getAutoload();
     }
+    public function getIncludePaths()
+    {
+        return $this->aliasOf->getIncludePaths();
+    }
     public function getRepositories()
     {
         return $this->aliasOf->getRepositories();

+ 4 - 0
src/Composer/Package/Loader/ArrayLoader.php

@@ -169,6 +169,10 @@ class ArrayLoader
             $package->setAutoload($config['autoload']);
         }
 
+        if (isset($config['include-paths'])) {
+            $package->setIncludePaths($config['include-paths']);
+        }
+
         return $package;
     }
 

+ 19 - 0
src/Composer/Package/MemoryPackage.php

@@ -56,6 +56,7 @@ class MemoryPackage extends BasePackage
     protected $recommends = array();
     protected $suggests = array();
     protected $autoload = array();
+    protected $includePaths = array();
 
     /**
      * Creates a new in memory package.
@@ -623,4 +624,22 @@ class MemoryPackage extends BasePackage
     {
         return $this->autoload;
     }
+
+    /**
+     * Sets the list of paths added to PHP's include path.
+     *
+     * @param array $includePaths List of directories.
+     */
+    public function setIncludePaths(array $includePaths)
+    {
+        $this->includePaths = $includePaths;
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public function getIncludePaths()
+    {
+        return $this->includePaths;
+    }
 }

+ 8 - 0
src/Composer/Package/PackageInterface.php

@@ -249,6 +249,14 @@ interface PackageInterface
      */
     function getAutoload();
 
+    /**
+     * Returns a list of directories which should get added to PHP's
+     * include path.
+     *
+     * @return array
+     */
+    function getIncludePaths();
+
     /**
      * Returns an array of repositories
      *