From 1df9c625837966eb009de3166ba9a8ac1469f956 Mon Sep 17 00:00:00 2001 From: Jordi Boggiano Date: Mon, 11 Jun 2012 14:15:08 +0200 Subject: [PATCH] Cosmetic fixes to autoload generation --- src/Composer/Autoload/AutoloadGenerator.php | 15 +++++----- src/Composer/Autoload/ClassLoader.php | 2 +- .../Test/Autoload/AutoloadGeneratorTest.php | 6 +--- .../Autoload/Fixtures/autoload_functions.php | 28 +++++++++++++++++++ .../Autoload/Fixtures/autoload_target_dir.php | 6 ++-- 5 files changed, 40 insertions(+), 17 deletions(-) create mode 100644 tests/Composer/Test/Autoload/Fixtures/autoload_functions.php diff --git a/src/Composer/Autoload/AutoloadGenerator.php b/src/Composer/Autoload/AutoloadGenerator.php index d10e3bd1f..97d3e496f 100644 --- a/src/Composer/Autoload/AutoloadGenerator.php +++ b/src/Composer/Autoload/AutoloadGenerator.php @@ -98,10 +98,10 @@ EOF; continue; } \$path = \$dir . implode('/', array_slice(explode('\\\\', \$class), $levels)).'.php'; - if (!stream_resolve_include_path(\$path)) { + if (!\$path = stream_resolve_include_path(\$path)) { return false; } - require_once \$path; + require \$path; return true; } @@ -121,12 +121,11 @@ EOF; } $classmapFile .= ");\n"; - $filesCode = ""; + $filesCode = ""; $autoloads['files'] = new \RecursiveIteratorIterator(new \RecursiveArrayIterator($autoloads['files'])); foreach ($autoloads['files'] as $functionFile) { - $filesCode .= 'require "' . $filesystem->findShortestPath(getcwd(), $functionFile) .'";' . "\n"; + $filesCode .= 'require __DIR__ . '. var_export('/'.$filesystem->findShortestPath($vendorPath, $functionFile), true).";\n"; } - $filesCode = rtrim($filesCode); file_put_contents($targetDir.'/autoload_namespaces.php', $namespacesFile); file_put_contents($targetDir.'/autoload_classmap.php', $classmapFile); @@ -290,6 +289,10 @@ EOF; protected function getAutoloadFile($vendorPathToTargetDirCode, $usePSR0, $useClassMap, $useIncludePath, $targetDirLoader, $filesCode) { + if ($filesCode) { + $filesCode = "\n".$filesCode; + } + $file = <<
findFile($class)) { - require $file; + include $file; return true; } diff --git a/tests/Composer/Test/Autoload/AutoloadGeneratorTest.php b/tests/Composer/Test/Autoload/AutoloadGeneratorTest.php index b7e1abc11..aceda0258 100644 --- a/tests/Composer/Test/Autoload/AutoloadGeneratorTest.php +++ b/tests/Composer/Test/Autoload/AutoloadGeneratorTest.php @@ -271,13 +271,9 @@ class AutoloadGeneratorTest extends TestCase file_put_contents($this->vendorDir.'/b/b/test2.php', 'generator->dump($this->repository, $package, $this->im, $this->vendorDir.'/composer'); - - $code = file_get_contents($this->vendorDir.'/autoload.php'); - $this->assertContains('a/a/test.php', $code); - $this->assertContains('b/b/test2.php', $code); + $this->assertFileEquals(__DIR__.'/Fixtures/autoload_functions.php', $this->vendorDir.'/autoload.php'); include $this->vendorDir . '/autoload.php'; - $this->assertTrue(function_exists('testFilesAutoloadGeneration1')); $this->assertTrue(function_exists('testFilesAutoloadGeneration2')); } diff --git a/tests/Composer/Test/Autoload/Fixtures/autoload_functions.php b/tests/Composer/Test/Autoload/Fixtures/autoload_functions.php new file mode 100644 index 000000000..4f0f37d3b --- /dev/null +++ b/tests/Composer/Test/Autoload/Fixtures/autoload_functions.php @@ -0,0 +1,28 @@ + $path) { + $loader->add($namespace, $path); + } + + $classMap = require $composerDir . '/autoload_classmap.php'; + if ($classMap) { + $loader->addClassMap($classMap); + } + + $loader->register(); + + return $loader; +}); diff --git a/tests/Composer/Test/Autoload/Fixtures/autoload_target_dir.php b/tests/Composer/Test/Autoload/Fixtures/autoload_target_dir.php index ce92241ec..0ba542c43 100644 --- a/tests/Composer/Test/Autoload/Fixtures/autoload_target_dir.php +++ b/tests/Composer/Test/Autoload/Fixtures/autoload_target_dir.php @@ -5,8 +5,6 @@ if (!class_exists('Composer\\Autoload\\ClassLoader', false)) { require __DIR__ . '/composer' . '/ClassLoader.php'; } - - return call_user_func(function() { $loader = new \Composer\Autoload\ClassLoader(); $composerDir = __DIR__ . '/composer'; @@ -29,10 +27,10 @@ return call_user_func(function() { continue; } $path = $dir . implode('/', array_slice(explode('\\', $class), 2)).'.php'; - if (!stream_resolve_include_path($path)) { + if (!$path = stream_resolve_include_path($path)) { return false; } - require_once $path; + require $path; return true; }