diff --git a/src/Composer/Autoload/AutoloadGenerator.php b/src/Composer/Autoload/AutoloadGenerator.php index e9fc5208b..a38378b27 100644 --- a/src/Composer/Autoload/AutoloadGenerator.php +++ b/src/Composer/Autoload/AutoloadGenerator.php @@ -116,13 +116,30 @@ EOF; } // flatten array + $classMap = array(); $autoloads['classmap'] = new \RecursiveIteratorIterator(new \RecursiveArrayIterator($autoloads['classmap'])); + foreach ($autoloads['psr-0'] as $namespace => $paths) { + foreach ($paths as $dir) { + $dir = $this->getPath($filesystem, $relVendorPath, $vendorPath, $dir); + foreach (ClassMapGenerator::createMap($dir) as $class => $path) { + if (0 === strpos($class, $namespace)) { + $path = '/'.$filesystem->findShortestPath(getcwd(), $path, true); + if (!isset($classMap[$class])) { + $classMap[$class] = '$baseDir . '.var_export($path, true).",\n"; + } + } + } + } + } foreach ($autoloads['classmap'] as $dir) { foreach (ClassMapGenerator::createMap($dir) as $class => $path) { $path = '/'.$filesystem->findShortestPath(getcwd(), $path, true); - $classmapFile .= ' '.var_export($class, true).' => $baseDir . '.var_export($path, true).",\n"; + $classMap[$class] = '$baseDir . '.var_export($path, true).",\n"; } } + foreach ($classMap as $class => $code) { + $classmapFile .= ' '.var_export($class, true).' => '.$code; + } $classmapFile .= ");\n"; $filesCode = ""; @@ -279,6 +296,20 @@ EOF; return $baseDir.var_export($path, true); } + protected function getPath(Filesystem $filesystem, $relVendorPath, $vendorPath, $path) + { + $path = strtr($path, '\\', '/'); + if (!$filesystem->isAbsolutePath($path)) { + if (strpos($path, $relVendorPath) === 0) { + // path starts with vendor dir + return $vendorPath . substr($path, strlen($relVendorPath)); + } + return getcwd().'/'.$path; + } + + return $path; + } + protected function getAutoloadFile($vendorPathToTargetDirCode, $suffix) { return <<fs = new Filesystem; $that = $this; - $this->workingDir = realpath(sys_get_temp_dir()); + $this->workingDir = realpath(sys_get_temp_dir()).DIRECTORY_SEPARATOR.'cmptest'; + $this->fs->ensureDirectoryExists($this->workingDir); $this->vendorDir = $this->workingDir.DIRECTORY_SEPARATOR.'composer-test-autoload'; $this->ensureDirectoryExistsAndClear($this->vendorDir); @@ -63,18 +64,14 @@ class AutoloadGeneratorTest extends TestCase protected function tearDown() { - if ($this->vendorDir === $this->workingDir) { - if (is_dir($this->workingDir.'/composer')) { - $this->fs->removeDirectory($this->workingDir.'/composer'); - } - } elseif (is_dir($this->vendorDir)) { + chdir($this->dir); + + if (is_dir($this->workingDir)) { + $this->fs->removeDirectory($this->workingDir); + } + if (is_dir($this->vendorDir)) { $this->fs->removeDirectory($this->vendorDir); } - if (is_dir($this->workingDir.'/composersrc')) { - $this->fs->removeDirectory($this->workingDir.'/composersrc'); - } - - chdir($this->dir); } public function testMainPackageAutoloading() @@ -89,9 +86,9 @@ class AutoloadGeneratorTest extends TestCase ->method('getPackages') ->will($this->returnValue(array())); - if (!is_dir($this->vendorDir.'/composer')) { - mkdir($this->vendorDir.'/composer'); - } + $this->fs->ensureDirectoryExists($this->workingDir.'/composer'); + $this->fs->ensureDirectoryExists($this->workingDir.'/src'); + $this->fs->ensureDirectoryExists($this->workingDir.'/lib'); $this->createClassFile($this->workingDir); @@ -114,9 +111,9 @@ class AutoloadGeneratorTest extends TestCase ->method('getPackages') ->will($this->returnValue(array())); - if (!is_dir($this->vendorDir.'/composer')) { - mkdir($this->vendorDir.'/composer', 0777, true); - } + $this->fs->ensureDirectoryExists($this->vendorDir.'/composer'); + $this->fs->ensureDirectoryExists($this->vendorDir.'/src/Main'); + file_put_contents($this->vendorDir.'/src/Main/Foo.php', 'createClassFile($this->vendorDir); @@ -138,7 +135,10 @@ class AutoloadGeneratorTest extends TestCase ->will($this->returnValue(array())); $this->vendorDir .= '/subdir'; - mkdir($this->vendorDir.'/composer', 0777, true); + + $this->fs->ensureDirectoryExists($this->vendorDir.'/composer'); + $this->fs->ensureDirectoryExists($this->workingDir.'/src'); + $this->createClassFile($this->workingDir); $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', '_3'); $this->assertAutoloadFiles('main2', $this->vendorDir.'/composer'); @@ -157,6 +157,8 @@ class AutoloadGeneratorTest extends TestCase ->method('getPackages') ->will($this->returnValue(array())); + $this->fs->ensureDirectoryExists($this->vendorDir.'/a'); + $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', 'TargetDir'); $this->assertFileEquals(__DIR__.'/Fixtures/autoload_target_dir.php', $this->vendorDir.'/autoload.php'); $this->assertFileEquals(__DIR__.'/Fixtures/autoload_real_target_dir.php', $this->vendorDir.'/composer/autoload_realTargetDir.php'); @@ -177,7 +179,11 @@ class AutoloadGeneratorTest extends TestCase ->method('getPackages') ->will($this->returnValue($packages)); - mkdir($this->vendorDir.'/composer', 0777, true); + $this->fs->ensureDirectoryExists($this->vendorDir.'/composer'); + $this->fs->ensureDirectoryExists($this->vendorDir.'/a/a/src'); + $this->fs->ensureDirectoryExists($this->vendorDir.'/a/a/lib'); + $this->fs->ensureDirectoryExists($this->vendorDir.'/b/b/src'); + $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', '_5'); $this->assertAutoloadFiles('vendors', $this->vendorDir.'/composer'); $this->assertTrue(file_exists($this->vendorDir.'/composer/autoload_classmap.php'), "ClassMap file needs to be generated, even if empty."); @@ -197,10 +203,10 @@ class AutoloadGeneratorTest extends TestCase ->method('getPackages') ->will($this->returnValue($packages)); - @mkdir($this->vendorDir.'/composer', 0777, true); - mkdir($this->vendorDir.'/a/a/src', 0777, true); - mkdir($this->vendorDir.'/b/b/src', 0777, true); - mkdir($this->vendorDir.'/b/b/lib', 0777, true); + $this->fs->ensureDirectoryExists($this->vendorDir.'/composer'); + $this->fs->ensureDirectoryExists($this->vendorDir.'/a/a/src'); + $this->fs->ensureDirectoryExists($this->vendorDir.'/b/b/src'); + $this->fs->ensureDirectoryExists($this->vendorDir.'/b/b/lib'); file_put_contents($this->vendorDir.'/a/a/src/a.php', 'vendorDir.'/b/b/src/b.php', 'vendorDir.'/b/b/lib/c.php', 'method('getPackages') ->will($this->returnValue($packages)); - @mkdir($this->vendorDir.'/composer', 0777, true); - mkdir($this->vendorDir.'/a/a/src', 0777, true); - mkdir($this->vendorDir.'/b/b', 0777, true); - mkdir($this->vendorDir.'/c/c/foo', 0777, true); + $this->fs->ensureDirectoryExists($this->vendorDir.'/composer'); + $this->fs->ensureDirectoryExists($this->vendorDir.'/a/a/src'); + $this->fs->ensureDirectoryExists($this->vendorDir.'/b/b'); + $this->fs->ensureDirectoryExists($this->vendorDir.'/c/c/foo'); file_put_contents($this->vendorDir.'/a/a/src/a.php', 'vendorDir.'/b/b/test.php', 'vendorDir.'/c/c/foo/test.php', 'method('getPackages') ->will($this->returnValue($packages)); - mkdir($this->vendorDir.'/a/a', 0777, true); - mkdir($this->vendorDir.'/b/b', 0777, true); + $this->fs->ensureDirectoryExists($this->vendorDir.'/a/a'); + $this->fs->ensureDirectoryExists($this->vendorDir.'/b/b'); file_put_contents($this->vendorDir.'/a/a/test.php', 'vendorDir.'/b/b/test2.php', 'setAutoload(array('psr-0' => array('A\\B' => '/home/deveuser/local-packages/a-a/lib'))); + $package->setAutoload(array('psr-0' => array('A\\B' => $this->workingDir.'/lib'))); $packages = array(); $packages[] = $a = new MemoryPackage('a/a', '1.0', '1.0'); @@ -301,9 +307,48 @@ class AutoloadGeneratorTest extends TestCase ->method('getPackages') ->will($this->returnValue($packages)); - mkdir($this->vendorDir.'/composer', 0777, true); + $this->fs->ensureDirectoryExists($this->workingDir.'/lib/A/B'); + $this->fs->ensureDirectoryExists($this->vendorDir.'/composer'); + $this->fs->ensureDirectoryExists($this->vendorDir.'/a/a/src'); + $this->fs->ensureDirectoryExists($this->vendorDir.'/a/a/lib/A/B'); + $this->fs->ensureDirectoryExists($this->vendorDir.'/b/b/src'); + file_put_contents($this->workingDir.'/lib/A/B/C.php', 'vendorDir.'/a/a/lib/A/B/C.php', 'workingDir, '\\', '/'); + $expectedNamespace = << \$vendorDir . '/b/b/src/', + 'A\\\\B' => array('$workDir/lib', \$vendorDir . '/a/a/lib/'), + 'A' => \$vendorDir . '/a/a/src/', +); + +EOF; + + $expectedClassmap = << \$baseDir . '/lib/A/B/C.php', +); + +EOF; + $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', '_9'); - $this->assertAutoloadFiles('override_vendors', $this->vendorDir.'/composer'); + $this->assertEquals($expectedNamespace, file_get_contents($this->vendorDir.'/composer/autoload_namespaces.php')); + $this->assertEquals($expectedClassmap, file_get_contents($this->vendorDir.'/composer/autoload_classmap.php')); } public function testIncludePathFileGeneration() @@ -328,7 +373,7 @@ class AutoloadGeneratorTest extends TestCase ->method("getPackages") ->will($this->returnValue($packages)); - mkdir($this->vendorDir."/composer", 0777, true); + $this->fs->ensureDirectoryExists($this->vendorDir.'/composer'); $this->generator->dump($this->config, $this->repository, $package, $this->im, "composer", '_10'); diff --git a/tests/Composer/Test/Autoload/Fixtures/autoload_classmap3.php b/tests/Composer/Test/Autoload/Fixtures/autoload_classmap3.php index 35f1159c1..75c7cd741 100644 --- a/tests/Composer/Test/Autoload/Fixtures/autoload_classmap3.php +++ b/tests/Composer/Test/Autoload/Fixtures/autoload_classmap3.php @@ -6,5 +6,6 @@ $vendorDir = dirname(__DIR__); $baseDir = $vendorDir; return array( + 'Main\\Foo' => $baseDir . '/src/Main/Foo.php', 'ClassMapFoo' => $baseDir . '/composersrc/foo.php', ); diff --git a/tests/Composer/Test/Autoload/Fixtures/autoload_override_vendors.php b/tests/Composer/Test/Autoload/Fixtures/autoload_override_vendors.php deleted file mode 100644 index 934424b5d..000000000 --- a/tests/Composer/Test/Autoload/Fixtures/autoload_override_vendors.php +++ /dev/null @@ -1,12 +0,0 @@ - $vendorDir . '/b/b/src/', - 'A\\B' => array('/home/deveuser/local-packages/a-a/lib', $vendorDir . '/a/a/lib/'), - 'A' => $vendorDir . '/a/a/src/', -);