From 487e66d0a9c0f67917bce19a06cb509e6c885b88 Mon Sep 17 00:00:00 2001 From: Jordi Boggiano Date: Sat, 10 Nov 2012 21:54:23 +0100 Subject: [PATCH] Allow loading of multiple composer autoloaders concurrently, fixes #1248 --- src/Composer/Autoload/AutoloadGenerator.php | 22 ++++-- tests/Composer/Test/AllFunctionalTest.php | 8 +++ .../Test/Autoload/AutoloadGeneratorTest.php | 68 +++++++++++-------- .../Autoload/Fixtures/autoload_classmap4.php | 6 +- .../Autoload/Fixtures/autoload_classmap5.php | 6 +- .../Autoload/Fixtures/autoload_functions.php | 2 +- .../autoload_functions_by_dependency.php | 2 +- .../autoload_real_files_by_dependency.php | 14 +++- .../Fixtures/autoload_real_functions.php | 14 +++- .../Fixtures/autoload_real_target_dir.php | 14 +++- .../Autoload/Fixtures/autoload_target_dir.php | 2 +- .../Test/Repository/Vcs/GitHubDriverTest.php | 6 ++ 12 files changed, 112 insertions(+), 52 deletions(-) diff --git a/src/Composer/Autoload/AutoloadGenerator.php b/src/Composer/Autoload/AutoloadGenerator.php index 489a90d23..7ae1cc803 100644 --- a/src/Composer/Autoload/AutoloadGenerator.php +++ b/src/Composer/Autoload/AutoloadGenerator.php @@ -158,13 +158,17 @@ EOF; $filesCode .= ' require '.$this->getPathCode($filesystem, $relVendorPath, $vendorPath, $functionFile).";\n"; } + if (!$suffix) { + $suffix = md5(uniqid('', true)); + } + file_put_contents($targetDir.'/autoload_namespaces.php', $namespacesFile); file_put_contents($targetDir.'/autoload_classmap.php', $classmapFile); if ($includePathFile = $this->getIncludePathsFile($packageMap, $filesystem, $relVendorPath, $vendorPath, $vendorPathCode, $appBaseDirCode)) { file_put_contents($targetDir.'/include_paths.php', $includePathFile); } file_put_contents($vendorPath.'/autoload.php', $this->getAutoloadFile($vendorPathToTargetDirCode, $suffix)); - file_put_contents($targetDir.'/autoload_real'.$suffix.'.php', $this->getAutoloadRealFile(true, true, (bool) $includePathFile, $targetDirLoader, $filesCode, $vendorPathCode, $appBaseDirCode, $suffix)); + file_put_contents($targetDir.'/autoload_real.php', $this->getAutoloadRealFile(true, true, (bool) $includePathFile, $targetDirLoader, $filesCode, $vendorPathCode, $appBaseDirCode, $suffix)); copy(__DIR__.'/ClassLoader.php', $targetDir.'/ClassLoader.php'); } @@ -313,7 +317,7 @@ EOF; // autoload.php generated by Composer -require_once $vendorPathToTargetDirCode . '/autoload_real$suffix.php'; +require_once $vendorPathToTargetDirCode . '/autoload_real.php'; return ComposerAutoloaderInit$suffix::getLoader(); @@ -336,21 +340,29 @@ AUTOLOAD; $file = <<
oldcwd = getcwd(); @@ -21,6 +23,11 @@ class AllFunctionalTest extends \PHPUnit_Framework_TestCase public function tearDown() { chdir($this->oldcwd); + if ($this->testDir) { + $fs = new Filesystem; + $fs->removeDirectory($this->testDir); + $this->testDir = null; + } } /** @@ -74,6 +81,7 @@ class AllFunctionalTest extends \PHPUnit_Framework_TestCase $section = null; $testDir = sys_get_temp_dir().'/composer_functional_test'.uniqid(mt_rand(), true); + $this->testDir = $testDir; $varRegex = '#%([a-zA-Z_-]+)%#'; $variableReplacer = function($match) use (&$data, $testDir) { list(, $var) = $match; diff --git a/tests/Composer/Test/Autoload/AutoloadGeneratorTest.php b/tests/Composer/Test/Autoload/AutoloadGeneratorTest.php index 626c2db07..b94137db6 100644 --- a/tests/Composer/Test/Autoload/AutoloadGeneratorTest.php +++ b/tests/Composer/Test/Autoload/AutoloadGeneratorTest.php @@ -36,7 +36,7 @@ class AutoloadGeneratorTest extends TestCase $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->vendorDir = $this->workingDir.DIRECTORY_SEPARATOR.'composer-test-autoload-'.md5(uniqid('', true)); $this->ensureDirectoryExistsAndClear($this->vendorDir); $this->config = $this->getMock('Composer\Config'); @@ -162,7 +162,7 @@ class AutoloadGeneratorTest extends TestCase $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', false, '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'); + $this->assertFileEquals(__DIR__.'/Fixtures/autoload_real_target_dir.php', $this->vendorDir.'/composer/autoload_real.php'); } public function testMainPackageAutoloadingWithTargetDirAndNoPsr() @@ -237,12 +237,12 @@ class AutoloadGeneratorTest extends TestCase $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', false, '_6'); $this->assertTrue(file_exists($this->vendorDir.'/composer/autoload_classmap.php'), "ClassMap file needs to be generated."); $this->assertEquals( - array( - 'ClassMapBar' => $this->workingDir.'/composer-test-autoload/b/b/src/b.php', - 'ClassMapBaz' => $this->workingDir.'/composer-test-autoload/b/b/lib/c.php', - 'ClassMapFoo' => $this->workingDir.'/composer-test-autoload/a/a/src/a.php', - ), - include ($this->vendorDir.'/composer/autoload_classmap.php') + $this->normalizePaths(array( + 'ClassMapBar' => $this->vendorDir.'/b/b/src/b.php', + 'ClassMapBaz' => $this->vendorDir.'/b/b/lib/c.php', + 'ClassMapFoo' => $this->vendorDir.'/a/a/src/a.php', + )), + $this->normalizePaths(include $this->vendorDir.'/composer/autoload_classmap.php') ); $this->assertAutoloadFiles('classmap4', $this->vendorDir.'/composer', 'classmap'); } @@ -274,12 +274,12 @@ class AutoloadGeneratorTest extends TestCase $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', false, '_7'); $this->assertTrue(file_exists($this->vendorDir.'/composer/autoload_classmap.php'), "ClassMap file needs to be generated."); $this->assertEquals( - array( - 'ClassMapBar' => $this->workingDir.'/composer-test-autoload/b/b/test.php', - 'ClassMapBaz' => $this->workingDir.'/composer-test-autoload/c/c/foo/test.php', - 'ClassMapFoo' => $this->workingDir.'/composer-test-autoload/a/a/src/a.php', - ), - include ($this->vendorDir.'/composer/autoload_classmap.php') + $this->normalizePaths(array( + 'ClassMapBar' => $this->vendorDir.'/b/b/test.php', + 'ClassMapBaz' => $this->vendorDir.'/c/c/foo/test.php', + 'ClassMapFoo' => $this->vendorDir.'/a/a/src/a.php', + )), + $this->normalizePaths(include $this->vendorDir.'/composer/autoload_classmap.php') ); $this->assertAutoloadFiles('classmap5', $this->vendorDir.'/composer', 'classmap'); } @@ -307,10 +307,7 @@ class AutoloadGeneratorTest extends TestCase $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', false, 'FilesAutoload'); $this->assertFileEquals(__DIR__.'/Fixtures/autoload_functions.php', $this->vendorDir.'/autoload.php'); - $this->assertFileEquals(__DIR__.'/Fixtures/autoload_real_functions.php', $this->vendorDir.'/composer/autoload_realFilesAutoload.php'); - - // suppress the class loader to avoid fatals if the class is redefined - file_put_contents($this->vendorDir.'/composer/ClassLoader.php', ''); + $this->assertFileEquals(__DIR__.'/Fixtures/autoload_real_functions.php', $this->vendorDir.'/composer/autoload_real.php'); include $this->vendorDir . '/autoload.php'; $this->assertTrue(function_exists('testFilesAutoloadGeneration1')); @@ -362,12 +359,9 @@ class AutoloadGeneratorTest extends TestCase $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', false, 'FilesAutoloadOrder'); $this->assertFileEquals(__DIR__ . '/Fixtures/autoload_functions_by_dependency.php', $this->vendorDir . '/autoload.php'); - $this->assertFileEquals(__DIR__ . '/Fixtures/autoload_real_files_by_dependency.php', $this->vendorDir . '/composer/autoload_realFilesAutoloadOrder.php'); + $this->assertFileEquals(__DIR__ . '/Fixtures/autoload_real_files_by_dependency.php', $this->vendorDir . '/composer/autoload_real.php'); - // suppress the class loader to avoid fatals if the class is redefined - file_put_contents($this->vendorDir . '/composer/ClassLoader.php', ''); - - include $this->vendorDir . '/autoload.php'; + require $this->vendorDir . '/autoload.php'; $this->assertTrue(function_exists('testFilesAutoloadOrderByDependency1')); $this->assertTrue(function_exists('testFilesAutoloadOrderByDependency2')); @@ -475,7 +469,7 @@ EOF; $this->vendorDir."/b/b/library", $this->vendorDir."/c/library", ), - require($this->vendorDir."/composer/include_paths.php") + require $this->vendorDir."/composer/include_paths.php" ); } @@ -499,10 +493,7 @@ EOF; $oldIncludePath = get_include_path(); - // suppress the class loader to avoid fatals if the class is redefined - file_put_contents($this->vendorDir.'/composer/ClassLoader.php', ''); - - require($this->vendorDir."/autoload.php"); + require $this->vendorDir."/autoload.php"; $this->assertEquals( $this->vendorDir."/a/a/lib".PATH_SEPARATOR.$oldIncludePath, @@ -542,6 +533,25 @@ EOF; private function assertAutoloadFiles($name, $dir, $type = 'namespaces') { - $this->assertFileEquals(__DIR__.'/Fixtures/autoload_'.$name.'.php', $dir.'/autoload_'.$type.'.php'); + $a = __DIR__.'/Fixtures/autoload_'.$name.'.php'; + $b = $dir.'/autoload_'.$type.'.php'; + $this->assertEquals( + str_replace('%vendorDir%', basename($this->vendorDir), file_get_contents($a)), + file_get_contents($b), + $a .' does not equal '. $b + ); + } + + private function normalizePaths($paths) + { + if (!is_array($paths)) { + return strtr($paths, '\\', '/'); + } + + foreach ($paths as $key => $path) { + $paths[$key] = strtr($path, '\\', '/'); + } + + return $paths; } } diff --git a/tests/Composer/Test/Autoload/Fixtures/autoload_classmap4.php b/tests/Composer/Test/Autoload/Fixtures/autoload_classmap4.php index 7ea7c42c7..8896cdfb8 100644 --- a/tests/Composer/Test/Autoload/Fixtures/autoload_classmap4.php +++ b/tests/Composer/Test/Autoload/Fixtures/autoload_classmap4.php @@ -6,7 +6,7 @@ $vendorDir = dirname(__DIR__); $baseDir = dirname($vendorDir); return array( - 'ClassMapBar' => $baseDir . '/composer-test-autoload/b/b/src/b.php', - 'ClassMapBaz' => $baseDir . '/composer-test-autoload/b/b/lib/c.php', - 'ClassMapFoo' => $baseDir . '/composer-test-autoload/a/a/src/a.php', + 'ClassMapBar' => $baseDir . '/%vendorDir%/b/b/src/b.php', + 'ClassMapBaz' => $baseDir . '/%vendorDir%/b/b/lib/c.php', + 'ClassMapFoo' => $baseDir . '/%vendorDir%/a/a/src/a.php', ); diff --git a/tests/Composer/Test/Autoload/Fixtures/autoload_classmap5.php b/tests/Composer/Test/Autoload/Fixtures/autoload_classmap5.php index 41c1738bc..0a1f5def8 100644 --- a/tests/Composer/Test/Autoload/Fixtures/autoload_classmap5.php +++ b/tests/Composer/Test/Autoload/Fixtures/autoload_classmap5.php @@ -6,7 +6,7 @@ $vendorDir = dirname(__DIR__); $baseDir = dirname($vendorDir); return array( - 'ClassMapBar' => $baseDir . '/composer-test-autoload/b/b/test.php', - 'ClassMapBaz' => $baseDir . '/composer-test-autoload/c/c/foo/test.php', - 'ClassMapFoo' => $baseDir . '/composer-test-autoload/a/a/src/a.php', + 'ClassMapBar' => $baseDir . '/%vendorDir%/b/b/test.php', + 'ClassMapBaz' => $baseDir . '/%vendorDir%/c/c/foo/test.php', + 'ClassMapFoo' => $baseDir . '/%vendorDir%/a/a/src/a.php', ); diff --git a/tests/Composer/Test/Autoload/Fixtures/autoload_functions.php b/tests/Composer/Test/Autoload/Fixtures/autoload_functions.php index 3fd5eebed..06a243350 100644 --- a/tests/Composer/Test/Autoload/Fixtures/autoload_functions.php +++ b/tests/Composer/Test/Autoload/Fixtures/autoload_functions.php @@ -2,6 +2,6 @@ // autoload.php generated by Composer -require_once __DIR__ . '/composer' . '/autoload_realFilesAutoload.php'; +require_once __DIR__ . '/composer' . '/autoload_real.php'; return ComposerAutoloaderInitFilesAutoload::getLoader(); diff --git a/tests/Composer/Test/Autoload/Fixtures/autoload_functions_by_dependency.php b/tests/Composer/Test/Autoload/Fixtures/autoload_functions_by_dependency.php index c643a7462..4cdd98237 100644 --- a/tests/Composer/Test/Autoload/Fixtures/autoload_functions_by_dependency.php +++ b/tests/Composer/Test/Autoload/Fixtures/autoload_functions_by_dependency.php @@ -2,6 +2,6 @@ // autoload.php generated by Composer -require_once __DIR__ . '/composer' . '/autoload_realFilesAutoloadOrder.php'; +require_once __DIR__ . '/composer' . '/autoload_real.php'; return ComposerAutoloaderInitFilesAutoloadOrder::getLoader(); diff --git a/tests/Composer/Test/Autoload/Fixtures/autoload_real_files_by_dependency.php b/tests/Composer/Test/Autoload/Fixtures/autoload_real_files_by_dependency.php index ac610bade..990e3dfd2 100644 --- a/tests/Composer/Test/Autoload/Fixtures/autoload_real_files_by_dependency.php +++ b/tests/Composer/Test/Autoload/Fixtures/autoload_real_files_by_dependency.php @@ -1,20 +1,28 @@ removeDirectory(sys_get_temp_dir() . '/composer-test'); + } + public function testPrivateRepository() { $repoUrl = 'http://github.com/composer/packagist';