Browse Source

Merge pull request #2139 from donquixote/feature/autoload-files-corrected-1

Feature/autoload files corrected 1
Jordi Boggiano 11 years ago
parent
commit
26ef9d3e47

+ 47 - 14
src/Composer/Autoload/AutoloadGenerator.php

@@ -167,12 +167,6 @@ EOF;
         }
         $classmapFile .= ");\n";
 
-        $filesCode = "";
-        $autoloads['files'] = new \RecursiveIteratorIterator(new \RecursiveArrayIterator($autoloads['files']));
-        foreach ($autoloads['files'] as $functionFile) {
-            $filesCode .= '        require '.$this->getPathCode($filesystem, $basePath, $vendorPath, $functionFile).";\n";
-        }
-
         if (!$suffix) {
             $suffix = md5(uniqid('', true));
         }
@@ -182,8 +176,11 @@ EOF;
         if ($includePathFile = $this->getIncludePathsFile($packageMap, $filesystem, $basePath, $vendorPath, $vendorPathCode52, $appBaseDirCode)) {
             file_put_contents($targetDir.'/include_paths.php', $includePathFile);
         }
+        if ($includeFilesFile = $this->getIncludeFilesFile($autoloads['files'], $filesystem, $basePath, $vendorPath, $vendorPathCode52, $appBaseDirCode)) {
+            file_put_contents($targetDir.'/autoload_files.php', $includeFilesFile);
+        }
         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, $useGlobalIncludePath));
+        file_put_contents($targetDir.'/autoload_real.php', $this->getAutoloadRealFile(true, true, (bool) $includePathFile, $targetDirLoader, (bool) $includeFilesFile, $vendorPathCode, $appBaseDirCode, $suffix, $useGlobalIncludePath));
 
         // use stream_copy_to_stream instead of copy
         // to work around https://bugs.php.net/bug.php?id=64634
@@ -298,6 +295,31 @@ EOF;
         return $includePathsFile . ");\n";
     }
 
+    protected function getIncludeFilesFile(array $files, Filesystem $filesystem, $basePath, $vendorPath, $vendorPathCode, $appBaseDirCode)
+    {
+        $filesCode = '';
+        $files = new \RecursiveIteratorIterator(new \RecursiveArrayIterator($files));
+        foreach ($files as $functionFile) {
+            $filesCode .= '    '.$this->getPathCode($filesystem, $basePath, $vendorPath, $functionFile).",\n";
+        }
+
+        if (!$filesCode) {
+            return FALSE;
+        }
+
+        return <<<EOF
+<?php
+
+// autoload_files.php generated by Composer
+
+\$vendorDir = $vendorPathCode;
+\$baseDir = $appBaseDirCode;
+
+return array(
+$filesCode);
+EOF;
+    }
+
     protected function getPathCode(Filesystem $filesystem, $basePath, $vendorPath, $path)
     {
         if (!$filesystem->isAbsolutePath($path)) {
@@ -338,7 +360,7 @@ return ComposerAutoloaderInit$suffix::getLoader();
 AUTOLOAD;
     }
 
-    protected function getAutoloadRealFile($usePSR0, $useClassMap, $useIncludePath, $targetDirLoader, $filesCode, $vendorPathCode, $appBaseDirCode, $suffix, $useGlobalIncludePath)
+    protected function getAutoloadRealFile($usePSR0, $useClassMap, $useIncludePath, $targetDirLoader, $useIncludeFiles, $vendorPathCode, $appBaseDirCode, $suffix, $useGlobalIncludePath)
     {
         // TODO the class ComposerAutoloaderInit should be revert to a closure
         // when APC has been fixed:
@@ -347,10 +369,6 @@ AUTOLOAD;
         // - https://bugs.php.net/bug.php?id=61576
         // - https://bugs.php.net/bug.php?id=59298
 
-        if ($filesCode) {
-            $filesCode = "\n\n".rtrim($filesCode);
-        }
-
         $file = <<<HEADER
 <?php
 
@@ -431,9 +449,24 @@ REGISTER_AUTOLOAD;
 
         }
 
-        $file .= <<<METHOD_FOOTER
-        \$loader->register(true);{$filesCode}
+        $file .= <<<REGISTER_LOADER
+        \$loader->register(true);
+
+
+REGISTER_LOADER;
 
+        if ($useIncludeFiles) {
+            $file .= <<<INCLUDE_FILES
+        foreach (require __DIR__ . '/autoload_files.php' as \$file) {
+            require \$file;
+        }
+
+
+INCLUDE_FILES;
+
+        }
+
+        $file .= <<<METHOD_FOOTER
         return \$loader;
     }
 

+ 5 - 3
tests/Composer/Test/Autoload/AutoloadGeneratorTest.php

@@ -191,6 +191,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_real.php');
+        $this->assertFileEquals(__DIR__.'/Fixtures/autoload_files_target_dir.php', $this->vendorDir.'/composer/autoload_files.php');
         $this->assertAutoloadFiles('classmap6', $this->vendorDir.'/composer', 'classmap');
     }
 
@@ -374,6 +375,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_real.php');
+        $this->assertFileEquals(__DIR__.'/Fixtures/autoload_files_functions.php', $this->vendorDir.'/composer/autoload_files.php');
 
         include $this->vendorDir . '/autoload.php';
         $this->assertTrue(function_exists('testFilesAutoloadGeneration1'));
@@ -753,8 +755,8 @@ EOF;
 
         $this->assertEquals($expectedNamespace, file_get_contents($vendorDir.'/composer/autoload_namespaces.php'));
         $this->assertEquals($expectedClassmap, file_get_contents($vendorDir.'/composer/autoload_classmap.php'));
-        $this->assertContains("require \$vendorDir . '/b/b/bootstrap.php';", file_get_contents($vendorDir.'/composer/autoload_real.php'));
-        $this->assertContains("require \$baseDir . '/test.php';", file_get_contents($vendorDir.'/composer/autoload_real.php'));
+        $this->assertContains("\n    \$vendorDir . '/b/b/bootstrap.php',\n", file_get_contents($vendorDir.'/composer/autoload_files.php'));
+        $this->assertContains("\n    \$baseDir . '/test.php',\n", file_get_contents($vendorDir.'/composer/autoload_files.php'));
     }
 
     public function testUpLevelRelativePaths()
@@ -813,7 +815,7 @@ EOF;
 
         $this->assertEquals($expectedNamespace, file_get_contents($this->vendorDir.'/composer/autoload_namespaces.php'));
         $this->assertEquals($expectedClassmap, file_get_contents($this->vendorDir.'/composer/autoload_classmap.php'));
-        $this->assertContains("require \$baseDir . '/../test.php';", file_get_contents($this->vendorDir.'/composer/autoload_real.php'));
+        $this->assertContains("\n    \$baseDir . '/../test.php',\n", file_get_contents($this->vendorDir.'/composer/autoload_files.php'));
     }
 
     public function testEmptyPaths()

+ 11 - 0
tests/Composer/Test/Autoload/Fixtures/autoload_files.php

@@ -0,0 +1,11 @@
+<?php
+
+// autoload_files.php generated by Composer
+
+$vendorDir = dirname(dirname(__FILE__));
+$baseDir = dirname($vendorDir);
+
+return array(
+    $baseDir . '/foo.php',
+    $baseDir . '/bar.php',
+);

+ 15 - 0
tests/Composer/Test/Autoload/Fixtures/autoload_files_files_by_dependency.php

@@ -0,0 +1,15 @@
+<?php
+
+// autoload_files.php generated by Composer
+
+$vendorDir = dirname(dirname(__FILE__));
+$baseDir = dirname($vendorDir);
+
+return array(
+    $vendorDir . '/c/lorem/testC.php',
+    $vendorDir . '/z/foo/testA.php',
+    $vendorDir . '/d/d/testD.php',
+    $vendorDir . '/b/bar/testB.php',
+    $vendorDir . '/e/e/testE.php',
+    $baseDir . '/root.php',
+);

+ 14 - 0
tests/Composer/Test/Autoload/Fixtures/autoload_files_functions.php

@@ -0,0 +1,14 @@
+<?php
+
+// autoload_files.php generated by Composer
+
+$vendorDir = dirname(dirname(__FILE__));
+$baseDir = dirname($vendorDir);
+
+return array(
+    $vendorDir . '/a/a/test.php',
+    $vendorDir . '/b/b/test2.php',
+    $vendorDir . '/c/c/foo/bar/test3.php',
+    $baseDir . '/root.php',
+    $vendorDir . '/c/c/foo/bar/test4.php',
+);

+ 11 - 0
tests/Composer/Test/Autoload/Fixtures/autoload_files_target_dir.php

@@ -0,0 +1,11 @@
+<?php
+
+// autoload_files.php generated by Composer
+
+$vendorDir = dirname(dirname(__FILE__));
+$baseDir = dirname($vendorDir);
+
+return array(
+    $baseDir . '/foo.php',
+    $baseDir . '/bar.php',
+);

+ 3 - 6
tests/Composer/Test/Autoload/Fixtures/autoload_real_files_by_dependency.php

@@ -38,12 +38,9 @@ class ComposerAutoloaderInitFilesAutoloadOrder
 
         $loader->register(true);
 
-        require $vendorDir . '/c/lorem/testC.php';
-        require $vendorDir . '/z/foo/testA.php';
-        require $vendorDir . '/d/d/testD.php';
-        require $vendorDir . '/b/bar/testB.php';
-        require $vendorDir . '/e/e/testE.php';
-        require $baseDir . '/root.php';
+        foreach (require __DIR__ . '/autoload_files.php' as $file) {
+            require $file;
+        }
 
         return $loader;
     }

+ 3 - 5
tests/Composer/Test/Autoload/Fixtures/autoload_real_functions.php

@@ -38,11 +38,9 @@ class ComposerAutoloaderInitFilesAutoload
 
         $loader->register(true);
 
-        require $vendorDir . '/a/a/test.php';
-        require $vendorDir . '/b/b/test2.php';
-        require $vendorDir . '/c/c/foo/bar/test3.php';
-        require $baseDir . '/root.php';
-        require $vendorDir . '/c/c/foo/bar/test4.php';
+        foreach (require __DIR__ . '/autoload_files.php' as $file) {
+            require $file;
+        }
 
         return $loader;
     }

+ 3 - 2
tests/Composer/Test/Autoload/Fixtures/autoload_real_target_dir.php

@@ -40,8 +40,9 @@ class ComposerAutoloaderInitTargetDir
 
         $loader->register(true);
 
-        require $baseDir . '/foo.php';
-        require $baseDir . '/bar.php';
+        foreach (require __DIR__ . '/autoload_files.php' as $file) {
+            require $file;
+        }
 
         return $loader;
     }