From a2a2f4af59724090d875eb4460d7b7c690d30b54 Mon Sep 17 00:00:00 2001 From: Jordi Boggiano Date: Wed, 18 Jul 2012 18:31:22 +0200 Subject: [PATCH] Add test and some fixes to allow absolute paths, refs #869 --- src/Composer/Autoload/AutoloadGenerator.php | 24 +++++++++++++------ .../Test/Autoload/AutoloadGeneratorTest.php | 6 +++-- .../Autoload/Fixtures/autoload_classmap4.php | 1 + 3 files changed, 22 insertions(+), 9 deletions(-) diff --git a/src/Composer/Autoload/AutoloadGenerator.php b/src/Composer/Autoload/AutoloadGenerator.php index 6bc06aa52..9a52de54c 100644 --- a/src/Composer/Autoload/AutoloadGenerator.php +++ b/src/Composer/Autoload/AutoloadGenerator.php @@ -53,7 +53,7 @@ return array( EOF; $packageMap = $this->buildPackageMap($installationManager, $mainPackage, $localRepo->getPackages()); - $autoloads = $this->parseAutoloads($packageMap); + $autoloads = $this->parseAutoloads($packageMap, $filesystem); foreach ($autoloads['psr-0'] as $namespace => $paths) { $exportedPaths = array(); @@ -118,10 +118,9 @@ EOF; $autoloads['classmap'] = new \RecursiveIteratorIterator(new \RecursiveArrayIterator($autoloads['classmap'])); foreach ($autoloads['classmap'] as $dir) { foreach (ClassMapGenerator::createMap($dir) as $class => $path) { - $classmapFile .= ' '.var_export($class, true) . ' => '; - if (0 === strpos($path, dirname($vendorPath), 0)) { + if (0 === strpos($path, getcwd())) { // this path seems to be located within this application/package $path = '/' . $filesystem->findShortestPath(getcwd(), $path, true); $classmapFile .= '$baseDir . '; @@ -170,11 +169,16 @@ EOF; /** * Compiles an ordered list of namespace => path mappings * - * @param array $packageMap array of array(package, installDir-relative-to-composer.json) - * @return array array('psr-0' => array('Ns\\Foo' => array('installDir'))) + * @param array $packageMap array of array(package, installDir-relative-to-composer.json) + * @param Filesystem $filesystem optional filesystem instance to use + * @return array array('psr-0' => array('Ns\\Foo' => array('installDir'))) */ - public function parseAutoloads(array $packageMap) + public function parseAutoloads(array $packageMap, Filesystem $filesystem = null) { + if (!$filesystem) { + $filesystem = new Filesystem; + } + $autoloads = array('classmap' => array(), 'psr-0' => array(), 'files' => array()); foreach ($packageMap as $item) { list($package, $installPath) = $item; @@ -190,7 +194,13 @@ EOF; } foreach ($mapping as $namespace => $paths) { foreach ((array) $paths as $path) { - $autoloads[$type][$namespace][] = empty($installPath) ? $path : $installPath.'/'.$path; + if ($filesystem->isAbsolutePath($path)) { + // handle absolute paths in raw form + $autoloads[$type][$namespace][] = $path; + } else { + // prepend install path (including target dir) to the relative paths + $autoloads[$type][$namespace][] = empty($installPath) ? $path : $installPath.'/'.$path; + } } } } diff --git a/tests/Composer/Test/Autoload/AutoloadGeneratorTest.php b/tests/Composer/Test/Autoload/AutoloadGeneratorTest.php index e3b91127d..17cbcc1ee 100644 --- a/tests/Composer/Test/Autoload/AutoloadGeneratorTest.php +++ b/tests/Composer/Test/Autoload/AutoloadGeneratorTest.php @@ -190,7 +190,7 @@ class AutoloadGeneratorTest extends TestCase $packages[] = $a = new MemoryPackage('a/a', '1.0', '1.0'); $packages[] = $b = new MemoryPackage('b/b', '1.0', '1.0'); $a->setAutoload(array('classmap' => array('src/'))); - $b->setAutoload(array('classmap' => array('src/', 'lib/'))); + $b->setAutoload(array('classmap' => array('src/', 'lib/', __FILE__))); $this->repository->expects($this->once()) ->method('getPackages') @@ -211,10 +211,12 @@ class AutoloadGeneratorTest extends TestCase 'ClassMapFoo' => $this->workingDir.'/composer-test-autoload/a/a/src/a.php', 'ClassMapBar' => $this->workingDir.'/composer-test-autoload/b/b/src/b.php', 'ClassMapBaz' => $this->workingDir.'/composer-test-autoload/b/b/lib/c.php', + 'Composer\Test\Autoload\AutoloadGeneratorTest' => __FILE__, ), include ($this->vendorDir.'/composer/autoload_classmap.php') ); - $this->assertAutoloadFiles('classmap4', $this->vendorDir.'/composer', 'classmap'); + $expected = str_replace('%path%', var_export(__FILE__, true), file_get_contents(__DIR__.'/Fixtures/autoload_classmap4.php')); + $this->assertEquals($expected, file_get_contents($this->vendorDir.'/composer/autoload_classmap.php')); } public function testClassMapAutoloadingEmptyDirAndExactFile() diff --git a/tests/Composer/Test/Autoload/Fixtures/autoload_classmap4.php b/tests/Composer/Test/Autoload/Fixtures/autoload_classmap4.php index 944a80bed..9e8546050 100644 --- a/tests/Composer/Test/Autoload/Fixtures/autoload_classmap4.php +++ b/tests/Composer/Test/Autoload/Fixtures/autoload_classmap4.php @@ -6,6 +6,7 @@ $vendorDir = dirname(__DIR__); $baseDir = dirname($vendorDir); return array( + 'Composer\\Test\\Autoload\\AutoloadGeneratorTest' => %path%, 'ClassMapBaz' => $baseDir . '/composer-test-autoload/b/b/lib/c.php', 'ClassMapFoo' => $baseDir . '/composer-test-autoload/a/a/src/a.php', 'ClassMapBar' => $baseDir . '/composer-test-autoload/b/b/src/b.php',