From 3c07b4338a07bb33281d2c9791d9d06e5137be52 Mon Sep 17 00:00:00 2001 From: Jordi Boggiano Date: Sun, 8 Apr 2012 17:42:57 +0200 Subject: [PATCH] Rename include-paths to include-path, add deprecated warnings, only generate file if it is needed --- doc/04-schema.md | 6 +- res/composer-schema.json | 4 +- src/Composer/Autoload/AutoloadGenerator.php | 103 +++++++++++------- src/Composer/Package/Loader/ArrayLoader.php | 4 +- .../Test/Autoload/AutoloadGeneratorTest.php | 9 +- 5 files changed, 76 insertions(+), 50 deletions(-) diff --git a/doc/04-schema.md b/doc/04-schema.md index 3c179eb63..d61973807 100644 --- a/doc/04-schema.md +++ b/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 diff --git a/res/composer-schema.json b/res/composer-schema.json index 9db68ab07..3a43aba3e 100644 --- a/res/composer-schema.json +++ b/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" } diff --git a/src/Composer/Autoload/AutoloadGenerator.php b/src/Composer/Autoload/AutoloadGenerator.php index d92080ea5..594cc28a8 100644 --- a/src/Composer/Autoload/AutoloadGenerator.php +++ b/src/Composer/Autoload/AutoloadGenerator.php @@ -27,42 +27,6 @@ class AutoloadGenerator { public function dump(RepositoryInterface $localRepo, PackageInterface $mainPackage, InstallationManager $installationManager, $targetDir) { - $autoloadFile = <<<'EOF' - $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( " $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; + } } diff --git a/src/Composer/Package/Loader/ArrayLoader.php b/src/Composer/Package/Loader/ArrayLoader.php index a72a50f81..f3bd0d5b7 100644 --- a/src/Composer/Package/Loader/ArrayLoader.php +++ b/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; diff --git a/tests/Composer/Test/Autoload/AutoloadGeneratorTest.php b/tests/Composer/Test/Autoload/AutoloadGeneratorTest.php index 0d9bdc1a7..dced4ef7f 100644 --- a/tests/Composer/Test/Autoload/AutoloadGeneratorTest.php +++ b/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)