فهرست منبع

Rename include-paths to include-path, add deprecated warnings, only generate file if it is needed

Jordi Boggiano 13 سال پیش
والد
کامیت
3c07b4338a

+ 4 - 2
doc/04-schema.md

@@ -225,14 +225,16 @@ Example:
         }
     }
 
-### include-paths
+### include-path
+
+> **DEPRECATED**: This is only present to support legacy projects, and all new code should preferably use autoloading.
 
 A list of paths which should get appended to PHP's `include_path`.
 
 Example:
 
     {
-        "include-paths": ["lib/"]
+        "include-path": ["lib/"]
     }
 
 ### target-dir

+ 2 - 2
res/composer-schema.json

@@ -146,9 +146,9 @@
                 "type": "string"
             }
         },
-        "include-paths": {
+        "include-path": {
             "type": ["array"],
-            "description": "A list of directories which should get added to PHP's include path.",
+            "description": "DEPRECATED: A list of directories which should get added to PHP's include path. This is only present to support legacy projects, and all new code should preferably use autoloading.",
             "items": {
                 "type": "string"
             }

+ 65 - 38
src/Composer/Autoload/AutoloadGenerator.php

@@ -27,42 +27,6 @@ class AutoloadGenerator
 {
     public function dump(RepositoryInterface $localRepo, PackageInterface $mainPackage, InstallationManager $installationManager, $targetDir)
     {
-        $autoloadFile = <<<'EOF'
-<?php
-
-// autoload.php generated by Composer
-if (!class_exists('Composer\\Autoload\\ClassLoader', false)) {
-    require __DIR__.'/ClassLoader.php';
-}
-
-$includePaths = require __DIR__.'/include_paths.php';
-
-if ($includePaths) {
-    array_unshift($includePaths, get_include_path());
-    set_include_path(join(PATH_SEPARATOR, $includePaths));
-}
-
-return call_user_func(function() {
-    $loader = new \Composer\Autoload\ClassLoader();
-
-    $map = require __DIR__.'/autoload_namespaces.php';
-
-    foreach ($map as $namespace => $path) {
-        $loader->add($namespace, $path);
-    }
-
-    $classMap = require __DIR__.'/autoload_classmap.php';
-    if ($classMap) {
-        $loader->addClassMap($classMap);
-    }
-
-    $loader->register();
-
-    return $loader;
-});
-
-EOF;
-
         $filesystem = new Filesystem();
         $vendorPath = strtr(realpath($installationManager->getVendorPath()), '\\', '/');
         $relVendorPath = $filesystem->findShortestPath(getcwd(), $vendorPath, true);
@@ -138,10 +102,12 @@ EOF;
         }
         $classmapFile .= ");\n";
 
-        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));
+        if ($includePathFile = $this->getIncludePathsFile($packageMap)) {
+            file_put_contents($targetDir.'/include_paths.php', $includePathFile);
+        }
+        file_put_contents($targetDir.'/autoload.php', $this->getAutoloadFile(true, true, (Boolean) $includePathFile));
         copy(__DIR__.'/ClassLoader.php', $targetDir.'/ClassLoader.php');
     }
 
@@ -230,8 +196,69 @@ EOF;
             }
         }
 
+        if (!$includePaths) {
+            return;
+        }
+
         return sprintf(
             "<?php\nreturn %s;\n", var_export($includePaths, true)
         );
     }
+
+    protected function getAutoloadFile($usePSR0, $useClassMap, $useIncludePath)
+    {
+        $file = <<<'HEADER'
+<?php
+
+// autoload.php generated by Composer
+if (!class_exists('Composer\\Autoload\\ClassLoader', false)) {
+    require __DIR__.'/ClassLoader.php';
+}
+
+return call_user_func(function() {
+    $loader = new \Composer\Autoload\ClassLoader();
+
+
+HEADER;
+
+        if ($useIncludePath) {
+            $file .= <<<'INCLUDE_PATH'
+    $includePaths = require __DIR__.'/include_paths.php';
+    array_unshift($includePaths, get_include_path());
+    set_include_path(join(PATH_SEPARATOR, $includePaths));
+
+
+INCLUDE_PATH;
+        }
+
+        if ($usePSR0) {
+            $file .= <<<'PSR0'
+    $map = require __DIR__.'/autoload_namespaces.php';
+    foreach ($map as $namespace => $path) {
+        $loader->add($namespace, $path);
+    }
+
+
+PSR0;
+        }
+
+        if ($useClassMap) {
+            $file .= <<<'CLASSMAP'
+    $classMap = require __DIR__.'/autoload_classmap.php';
+    if ($classMap) {
+        $loader->addClassMap($classMap);
+    }
+
+
+CLASSMAP;
+        }
+
+        return $file . <<<'FOOTER'
+    $loader->register();
+
+    return $loader;
+});
+
+FOOTER;
+    }
 }

+ 2 - 2
src/Composer/Package/Loader/ArrayLoader.php

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

+ 3 - 6
tests/Composer/Test/Autoload/AutoloadGeneratorTest.php

@@ -283,7 +283,7 @@ class AutoloadGeneratorTest extends TestCase
             require($this->vendorDir."/.composer/include_paths.php")
         );
     }
-    
+
     public function testIncludePathsAreAppendedInAutoloadFile()
     {
         $package = new MemoryPackage('a', '1.0', '1.0');
@@ -314,7 +314,7 @@ class AutoloadGeneratorTest extends TestCase
         set_include_path($oldIncludePath);
     }
 
-    public function testIncludePathFileGenerationWithoutPaths()
+    public function testIncludePathFileWithoutPathsIsSkipped()
     {
         $package = new MemoryPackage('a', '1.0', '1.0');
         $packages = array();
@@ -330,10 +330,7 @@ class AutoloadGeneratorTest extends TestCase
 
         $this->generator->dump($this->repository, $package, $this->im, $this->vendorDir."/.composer");
 
-        $this->assertEquals(
-            array(),
-            require($this->vendorDir."/.composer/include_paths.php")
-        );
+        $this->assertFalse(file_exists($this->vendorDir."/.composer/include_paths.php"));
     }
 
     private function createClassFile($basedir)