From 84b34b70e06dbec4f44b1faf4b053c6fc4ad6e2d Mon Sep 17 00:00:00 2001 From: Pierre du Plessis Date: Mon, 14 Jan 2013 10:32:26 +0200 Subject: [PATCH 1/3] Add config option to set if the PHP include path should automatically be used when generating the autoloader --- src/Composer/Autoload/AutoloadGenerator.php | 12 ++++++++++-- src/Composer/Command/ConfigCommand.php | 4 ++++ src/Composer/Config.php | 1 + .../Test/Autoload/AutoloadGeneratorTest.php | 15 ++++++++++++++- 4 files changed, 29 insertions(+), 3 deletions(-) diff --git a/src/Composer/Autoload/AutoloadGenerator.php b/src/Composer/Autoload/AutoloadGenerator.php index 5991083f7..bac2c9852 100644 --- a/src/Composer/Autoload/AutoloadGenerator.php +++ b/src/Composer/Autoload/AutoloadGenerator.php @@ -30,6 +30,7 @@ class AutoloadGenerator $filesystem = new Filesystem(); $filesystem->ensureDirectoryExists($config->get('vendor-dir')); $vendorPath = strtr(realpath($config->get('vendor-dir')), '\\', '/'); + $useGlobalIncludePath = (bool) $config->get('use-include-path'); $targetDir = $vendorPath.'/'.$targetDir; $filesystem->ensureDirectoryExists($targetDir); @@ -171,7 +172,7 @@ EOF; file_put_contents($targetDir.'/include_paths.php', $includePathFile); } file_put_contents($vendorPath.'/autoload.php', $this->getAutoloadFile($vendorPathToTargetDirCode, $suffix)); - file_put_contents($targetDir.'/autoload_real.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, $useGlobalIncludePath)); copy(__DIR__.'/ClassLoader.php', $targetDir.'/ClassLoader.php'); } @@ -326,7 +327,7 @@ return ComposerAutoloaderInit$suffix::getLoader(); AUTOLOAD; } - protected function getAutoloadRealFile($usePSR0, $useClassMap, $useIncludePath, $targetDirLoader, $filesCode, $vendorPathCode, $appBaseDirCode, $suffix) + protected function getAutoloadRealFile($usePSR0, $useClassMap, $useIncludePath, $targetDirLoader, $filesCode, $vendorPathCode, $appBaseDirCode, $suffix, $useGlobalIncludePath) { // TODO the class ComposerAutoloaderInit should be revert to a closure // when APC has been fixed: @@ -403,6 +404,13 @@ PSR0; CLASSMAP; } + if ($useGlobalIncludePath) { + $file .= <<<'INCLUDEPATH' + $loader->setUseIncludePath(true); + +INCLUDEPATH; + } + if ($targetDirLoader) { $file .= << array( + function ($val) { return true; }, + function ($val) { return $val !== 'false' && (bool) $val; } + ), ); $multiConfigValues = array( 'github-protocols' => array( diff --git a/src/Composer/Config.php b/src/Composer/Config.php index f4207fa5a..43aaec078 100644 --- a/src/Composer/Config.php +++ b/src/Composer/Config.php @@ -31,6 +31,7 @@ class Config 'cache-files-dir' => '{$cache-dir}/files', 'cache-repo-dir' => '{$cache-dir}/repo', 'cache-vcs-dir' => '{$cache-dir}/vcs', + 'use-include-path' => false, ); public static $defaultRepositories = array( diff --git a/tests/Composer/Test/Autoload/AutoloadGeneratorTest.php b/tests/Composer/Test/Autoload/AutoloadGeneratorTest.php index 9693d8826..e763d2742 100644 --- a/tests/Composer/Test/Autoload/AutoloadGeneratorTest.php +++ b/tests/Composer/Test/Autoload/AutoloadGeneratorTest.php @@ -40,13 +40,26 @@ class AutoloadGeneratorTest extends TestCase $this->ensureDirectoryExistsAndClear($this->vendorDir); $this->config = $this->getMock('Composer\Config'); - $this->config->expects($this->any()) + + $this->config->expects($this->at(0)) ->method('get') ->with($this->equalTo('vendor-dir')) ->will($this->returnCallback(function () use ($that) { return $that->vendorDir; })); + $this->config->expects($this->at(1)) + ->method('get') + ->with($this->equalTo('vendor-dir')) + ->will($this->returnCallback(function () use ($that) { + return $that->vendorDir; + })); + + $this->config->expects($this->at(2)) + ->method('get') + ->with($this->equalTo('use-include-path')) + ->will($this->returnValue(false)); + $this->dir = getcwd(); chdir($this->workingDir); From 91ca7d74939174e436a73c539ab8434ce76a4256 Mon Sep 17 00:00:00 2001 From: Pierre du Plessis Date: Mon, 14 Jan 2013 13:09:51 +0200 Subject: [PATCH 2/3] Make use-include-path default to false --- src/Composer/Command/ConfigCommand.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Composer/Command/ConfigCommand.php b/src/Composer/Command/ConfigCommand.php index 02202cfec..a68dd498e 100644 --- a/src/Composer/Command/ConfigCommand.php +++ b/src/Composer/Command/ConfigCommand.php @@ -247,7 +247,7 @@ EOT function ($val) { return $val !== 'false' && (bool) $val; } ), 'use-include-path' => array( - function ($val) { return true; }, + function ($val) { return false; }, function ($val) { return $val !== 'false' && (bool) $val; } ), ); From 255c0be7fc1e3a324b19ccfa75807900597b7292 Mon Sep 17 00:00:00 2001 From: Pierre du Plessis Date: Mon, 4 Feb 2013 10:12:41 +0200 Subject: [PATCH 3/3] Added tests for include path flag --- .../Test/Autoload/AutoloadGeneratorTest.php | 29 +++++++-- .../Fixtures/autoload_real_include_path.php | 64 +++++++++++++++++++ 2 files changed, 88 insertions(+), 5 deletions(-) create mode 100644 tests/Composer/Test/Autoload/Fixtures/autoload_real_include_path.php diff --git a/tests/Composer/Test/Autoload/AutoloadGeneratorTest.php b/tests/Composer/Test/Autoload/AutoloadGeneratorTest.php index e763d2742..233b399af 100644 --- a/tests/Composer/Test/Autoload/AutoloadGeneratorTest.php +++ b/tests/Composer/Test/Autoload/AutoloadGeneratorTest.php @@ -55,11 +55,6 @@ class AutoloadGeneratorTest extends TestCase return $that->vendorDir; })); - $this->config->expects($this->at(2)) - ->method('get') - ->with($this->equalTo('use-include-path')) - ->will($this->returnValue(false)); - $this->dir = getcwd(); chdir($this->workingDir); @@ -583,6 +578,30 @@ EOF; $this->assertFalse(file_exists($this->vendorDir."/composer/include_paths.php")); } + + + public function testUseGlobalIncludePath() + { + $package = new Package('a', '1.0', '1.0'); + $package->setAutoload(array( + 'psr-0' => array('Main\\Foo' => '', 'Main\\Bar' => ''), + )); + $package->setTargetDir('Main/Foo/'); + + $this->repository->expects($this->once()) + ->method('getPackages') + ->will($this->returnValue(array())); + + $this->config->expects($this->at(2)) + ->method('get') + ->with($this->equalTo('use-include-path')) + ->will($this->returnValue(true)); + + $this->fs->ensureDirectoryExists($this->vendorDir.'/a'); + + $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', false, 'IncludePath'); + $this->assertFileEquals(__DIR__.'/Fixtures/autoload_real_include_path.php', $this->vendorDir.'/composer/autoload_real.php'); + } private function createClassFile($basedir) { diff --git a/tests/Composer/Test/Autoload/Fixtures/autoload_real_include_path.php b/tests/Composer/Test/Autoload/Fixtures/autoload_real_include_path.php new file mode 100644 index 000000000..a410ff688 --- /dev/null +++ b/tests/Composer/Test/Autoload/Fixtures/autoload_real_include_path.php @@ -0,0 +1,64 @@ + $path) { + $loader->add($namespace, $path); + } + + $classMap = require __DIR__ . '/autoload_classmap.php'; + if ($classMap) { + $loader->addClassMap($classMap); + } + + $loader->setUseIncludePath(true); + spl_autoload_register(array('ComposerAutoloaderInitIncludePath', 'autoload'), true, true); + + $loader->register(true); + + return $loader; + } + + public static function autoload($class) + { + $dir = dirname(dirname(__DIR__)) . '/'; + $prefixes = array('Main\\Foo', 'Main\\Bar'); + foreach ($prefixes as $prefix) { + if (0 !== strpos($class, $prefix)) { + continue; + } + $path = $dir . implode('/', array_slice(explode('\\', $class), 2)).'.php'; + if (!$path = stream_resolve_include_path($path)) { + return false; + } + require $path; + + return true; + } + } +}