From 66135538c1295c2076262373785a11d81a8f4272 Mon Sep 17 00:00:00 2001 From: Jordi Boggiano Date: Thu, 19 Apr 2012 21:26:01 +0200 Subject: [PATCH 1/3] Allow root packages packages with target-dir to be autoloaded, fixes #139 --- src/Composer/Autoload/AutoloadGenerator.php | 36 ++++++++++++++++- .../Test/Autoload/AutoloadGeneratorTest.php | 16 ++++++++ .../Autoload/Fixtures/autoload_target_dir.php | 39 +++++++++++++++++++ 3 files changed, 89 insertions(+), 2 deletions(-) create mode 100644 tests/Composer/Test/Autoload/Fixtures/autoload_target_dir.php diff --git a/src/Composer/Autoload/AutoloadGenerator.php b/src/Composer/Autoload/AutoloadGenerator.php index 2e8fc07eb..8b32cc181 100644 --- a/src/Composer/Autoload/AutoloadGenerator.php +++ b/src/Composer/Autoload/AutoloadGenerator.php @@ -79,6 +79,36 @@ return array( EOF; + // add custom psr-0 autoloading if the root package has a target dir + $targetDirLoader = null; + $mainAutoload = $mainPackage->getAutoload(); + if ($mainPackage->getTargetDir() && $mainAutoload['psr-0']) { + $levels = count(explode('/', trim(strtr($mainPackage->getTargetDir(), '\\', '/'), '/'))); + $prefixes = implode("', '", array_map(function ($prefix) { + return var_export($prefix, true); + }, array_keys($mainAutoload['psr-0']))); + $baseDirFromTargetDirCode = $filesystem->findShortestPathCode(realpath($targetDir), getcwd(), true); + + $targetDirLoader = <<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)); + file_put_contents($targetDir.'/autoload.php', $this->getAutoloadFile(true, true, (Boolean) $includePathFile, $targetDirLoader)); copy(__DIR__.'/ClassLoader.php', $targetDir.'/ClassLoader.php'); // TODO BC feature, add E_DEPRECATED in autoload.php on April 30th, remove after May 30th @@ -242,7 +272,7 @@ EOF; return $baseDir.var_export($path, true); } - protected function getAutoloadFile($usePSR0, $useClassMap, $useIncludePath) + protected function getAutoloadFile($usePSR0, $useClassMap, $useIncludePath, $targetDirLoader) { $file = <<<'HEADER' register(); diff --git a/tests/Composer/Test/Autoload/AutoloadGeneratorTest.php b/tests/Composer/Test/Autoload/AutoloadGeneratorTest.php index f4feef203..089b40adb 100644 --- a/tests/Composer/Test/Autoload/AutoloadGeneratorTest.php +++ b/tests/Composer/Test/Autoload/AutoloadGeneratorTest.php @@ -141,6 +141,22 @@ class AutoloadGeneratorTest extends TestCase $this->assertAutoloadFiles('classmap2', $this->vendorDir.'/.composer', 'classmap'); } + public function testMainPackageAutoloadingWithTargetDir() + { + $package = new MemoryPackage('a', '1.0', '1.0'); + $package->setAutoload(array( + 'psr-0' => array('Main\\Foo' => ''), + )); + $package->setTargetDir('Main/Foo'); + + $this->repository->expects($this->once()) + ->method('getPackages') + ->will($this->returnValue(array())); + + $this->generator->dump($this->repository, $package, $this->im, $this->vendorDir.'/.composer'); + $this->assertFileEquals(__DIR__.'/Fixtures/autoload_target_dir.php', $this->vendorDir.'/.composer/autoload.php'); + } + public function testVendorsAutoloading() { $package = new MemoryPackage('a', '1.0', '1.0'); diff --git a/tests/Composer/Test/Autoload/Fixtures/autoload_target_dir.php b/tests/Composer/Test/Autoload/Fixtures/autoload_target_dir.php new file mode 100644 index 000000000..8e70900f0 --- /dev/null +++ b/tests/Composer/Test/Autoload/Fixtures/autoload_target_dir.php @@ -0,0 +1,39 @@ + $path) { + $loader->add($namespace, $path); + } + + $classMap = require __DIR__.'/autoload_classmap.php'; + if ($classMap) { + $loader->addClassMap($classMap); + } + + spl_autoload_register(function($class) { + $prefixes = array('Main\\Foo'); + foreach ($prefixes as $prefix) { + if (0 !== strpos($class, $prefix)) { + continue; + } + $path = dirname(dirname(__DIR__)) . '/' . implode('/', array_slice(explode('\\', $class), 2)).'.php'; + if (!stream_resolve_include_path($path)) { + return false; + } + require_once $path; + return true; + } + }); + + $loader->register(); + + return $loader; +}); From a9176aceccb6d7c887f6ac920414c67cf3ef23c5 Mon Sep 17 00:00:00 2001 From: Jordi Boggiano Date: Thu, 19 Apr 2012 21:27:33 +0200 Subject: [PATCH 2/3] Update CHANGELOG --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index e11ed2552..306521056 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ * Added caching of repository metadata (faster startup times & failover if packagist is down) * Added include_path support for legacy projects that are full of require_once statements * Added installation notifications API to allow better statistics on Composer repositories + * Added autoloading support for root packages that use target-dir * Improved repository protocol to have large cacheable parts * 1.0.0-alpha2 (2012-04-03) From 51711c2f73e0382e0394049356d0d8ca4bb77abb Mon Sep 17 00:00:00 2001 From: Jordi Boggiano Date: Thu, 19 Apr 2012 22:04:49 +0200 Subject: [PATCH 3/3] Fix multi-prefix handling --- src/Composer/Autoload/AutoloadGenerator.php | 2 +- tests/Composer/Test/Autoload/AutoloadGeneratorTest.php | 4 ++-- tests/Composer/Test/Autoload/Fixtures/autoload_target_dir.php | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Composer/Autoload/AutoloadGenerator.php b/src/Composer/Autoload/AutoloadGenerator.php index 8b32cc181..38421b375 100644 --- a/src/Composer/Autoload/AutoloadGenerator.php +++ b/src/Composer/Autoload/AutoloadGenerator.php @@ -84,7 +84,7 @@ EOF; $mainAutoload = $mainPackage->getAutoload(); if ($mainPackage->getTargetDir() && $mainAutoload['psr-0']) { $levels = count(explode('/', trim(strtr($mainPackage->getTargetDir(), '\\', '/'), '/'))); - $prefixes = implode("', '", array_map(function ($prefix) { + $prefixes = implode(', ', array_map(function ($prefix) { return var_export($prefix, true); }, array_keys($mainAutoload['psr-0']))); $baseDirFromTargetDirCode = $filesystem->findShortestPathCode(realpath($targetDir), getcwd(), true); diff --git a/tests/Composer/Test/Autoload/AutoloadGeneratorTest.php b/tests/Composer/Test/Autoload/AutoloadGeneratorTest.php index 089b40adb..5cabfd2df 100644 --- a/tests/Composer/Test/Autoload/AutoloadGeneratorTest.php +++ b/tests/Composer/Test/Autoload/AutoloadGeneratorTest.php @@ -145,9 +145,9 @@ class AutoloadGeneratorTest extends TestCase { $package = new MemoryPackage('a', '1.0', '1.0'); $package->setAutoload(array( - 'psr-0' => array('Main\\Foo' => ''), + 'psr-0' => array('Main\\Foo' => '', 'Main\\Bar' => ''), )); - $package->setTargetDir('Main/Foo'); + $package->setTargetDir('Main/Foo/'); $this->repository->expects($this->once()) ->method('getPackages') diff --git a/tests/Composer/Test/Autoload/Fixtures/autoload_target_dir.php b/tests/Composer/Test/Autoload/Fixtures/autoload_target_dir.php index 8e70900f0..f2acfcc3b 100644 --- a/tests/Composer/Test/Autoload/Fixtures/autoload_target_dir.php +++ b/tests/Composer/Test/Autoload/Fixtures/autoload_target_dir.php @@ -19,7 +19,7 @@ return call_user_func(function() { } spl_autoload_register(function($class) { - $prefixes = array('Main\\Foo'); + $prefixes = array('Main\\Foo', 'Main\\Bar'); foreach ($prefixes as $prefix) { if (0 !== strpos($class, $prefix)) { continue;