diff --git a/src/Composer/Autoload/AutoloadGenerator.php b/src/Composer/Autoload/AutoloadGenerator.php index daf2827a7..b9806320c 100644 --- a/src/Composer/Autoload/AutoloadGenerator.php +++ b/src/Composer/Autoload/AutoloadGenerator.php @@ -34,8 +34,8 @@ class AutoloadGenerator $relVendorPath = $filesystem->findShortestPath(getcwd(), $vendorPath, true); $vendorDirCode = $filesystem->findShortestPathCode(realpath($targetDir), $vendorPath, true); - $appBaseDir = $filesystem->findShortestPathCode($vendorPath, getcwd(), true); - $appBaseDir = str_replace('__DIR__', '$vendorDir', $appBaseDir); + $appBaseDirCode = $filesystem->findShortestPathCode($vendorPath, getcwd(), true); + $appBaseDirCode = str_replace('__DIR__', '$vendorDir', $appBaseDirCode); $namespacesFile = << $paths) { $exportedPaths = array(); foreach ($paths as $path) { - $path = strtr($path, '\\', '/'); - $baseDir = ''; - if (!$filesystem->isAbsolutePath($path)) { - if (strpos($path, $relVendorPath) === 0) { - // path starts with vendor dir - $path = substr($path, strlen($relVendorPath)); - $baseDir = '$vendorDir . '; - } else { - $path = '/'.$path; - $baseDir = '$baseDir . '; - } - } elseif (strpos($path, $vendorPath) === 0) { - $path = substr($path, strlen($vendorPath)); - $baseDir = '$vendorDir . '; - } - $exportedPaths[] = $baseDir.var_export($path, true); + $exportedPaths[] = $this->getPathCode($filesystem, $relVendorPath, $vendorPath, $path); } $exportedPrefix = var_export($namespace, true); $namespacesFile .= " $exportedPrefix => "; @@ -88,7 +73,7 @@ EOF; // autoload_classmap.php generated by Composer \$vendorDir = $vendorDirCode; -\$baseDir = $appBaseDir; +\$baseDir = $appBaseDirCode; return array( @@ -106,7 +91,7 @@ EOF; file_put_contents($targetDir.'/autoload_namespaces.php', $namespacesFile); file_put_contents($targetDir.'/autoload_classmap.php', $classmapFile); - if ($includePathFile = $this->getIncludePathsFile($packageMap)) { + if ($includePathFile = $this->getIncludePathsFile($packageMap, $filesystem, $relVendorPath, $vendorPath, $vendorDirCode, $appBaseDirCode)) { file_put_contents($targetDir.'/include_paths.php', $includePathFile); } file_put_contents($targetDir.'/autoload.php', $this->getAutoloadFile(true, true, (Boolean) $includePathFile)); @@ -186,7 +171,7 @@ EOF; return $loader; } - protected function getIncludePathsFile(array $packageMap) + protected function getIncludePathsFile(array $packageMap, Filesystem $filesystem, $relVendorPath, $vendorPath, $vendorDirCode, $appBaseDirCode) { $includePaths = array(); @@ -198,6 +183,7 @@ EOF; } foreach ($package->getIncludePaths() as $includePath) { + $includePath = trim($includePath, '/'); $includePaths[] = empty($installPath) ? $includePath : $installPath.'/'.$includePath; } } @@ -206,9 +192,43 @@ EOF; return; } - return sprintf( - "getPathCode($filesystem, $relVendorPath, $vendorPath, $path) . ",\n"; + } + + return $includePathsFile . ");\n"; + } + + protected function getPathCode(Filesystem $filesystem, $relVendorPath, $vendorPath, $path) + { + $path = strtr($path, '\\', '/'); + $baseDir = ''; + if (!$filesystem->isAbsolutePath($path)) { + if (strpos($path, $relVendorPath) === 0) { + // path starts with vendor dir + $path = substr($path, strlen($relVendorPath)); + $baseDir = '$vendorDir . '; + } else { + $path = '/'.$path; + $baseDir = '$baseDir . '; + } + } elseif (strpos($path, $vendorPath) === 0) { + $path = substr($path, strlen($vendorPath)); + $baseDir = '$vendorDir . '; + } + return $baseDir.var_export($path, true); } protected function getAutoloadFile($usePSR0, $useClassMap, $useIncludePath) diff --git a/tests/Composer/Test/Autoload/AutoloadGeneratorTest.php b/tests/Composer/Test/Autoload/AutoloadGeneratorTest.php index dced4ef7f..f4feef203 100644 --- a/tests/Composer/Test/Autoload/AutoloadGeneratorTest.php +++ b/tests/Composer/Test/Autoload/AutoloadGeneratorTest.php @@ -264,8 +264,12 @@ class AutoloadGeneratorTest extends TestCase $b = new MemoryPackage("b/b", "1.0", "1.0"); $b->setIncludePaths(array("library")); + $c = new MemoryPackage("c", "1.0", "1.0"); + $c->setIncludePaths(array("library")); + $packages[] = $a; $packages[] = $b; + $packages[] = $c; $this->repository->expects($this->once()) ->method("getPackages") @@ -275,10 +279,12 @@ class AutoloadGeneratorTest extends TestCase $this->generator->dump($this->repository, $package, $this->im, $this->vendorDir."/.composer"); + $this->assertFileEquals(__DIR__.'/Fixtures/include_paths.php', $this->vendorDir.'/.composer/include_paths.php'); $this->assertEquals( array( - $this->vendorDir."/a/a/lib/", - $this->vendorDir."/b/b/library" + $this->vendorDir."/a/a/lib", + $this->vendorDir."/b/b/library", + $this->vendorDir."/c/library", ), require($this->vendorDir."/.composer/include_paths.php") ); @@ -307,7 +313,7 @@ class AutoloadGeneratorTest extends TestCase require($this->vendorDir."/.composer/autoload.php"); $this->assertEquals( - $oldIncludePath.PATH_SEPARATOR.$this->vendorDir."/a/a/lib/", + $oldIncludePath.PATH_SEPARATOR.$this->vendorDir."/a/a/lib", get_include_path() );