Browse Source

Merge remote-tracking branch 'vicb/autoload_files'

Jordi Boggiano 12 years ago
parent
commit
034b46bdf6

+ 62 - 46
src/Composer/Autoload/AutoloadGenerator.php

@@ -25,7 +25,7 @@ use Composer\Util\Filesystem;
  */
 class AutoloadGenerator
 {
-    public function dump(Config $config, RepositoryInterface $localRepo, PackageInterface $mainPackage, InstallationManager $installationManager, $targetDir, $classSuffix = '')
+    public function dump(Config $config, RepositoryInterface $localRepo, PackageInterface $mainPackage, InstallationManager $installationManager, $targetDir, $suffix = '')
     {
         $filesystem = new Filesystem();
         $filesystem->ensureDirectoryExists($config->get('vendor-dir'));
@@ -90,27 +90,27 @@ EOF;
             $prefixes = implode(', ', array_map(function ($prefix) {
                 return var_export($prefix, true);
             }, array_keys($mainAutoload['psr-0'])));
-            $baseDirFromVendorDirCode = $filesystem->findShortestPathCode($vendorPath, getcwd(), true);
+            $baseDirFromTargetDirCode = $filesystem->findShortestPathCode($targetDir, getcwd(), true);
 
             $targetDirLoader = <<<EOF
 
-        public static function autoload(\$class)
-        {
-            \$dir = $baseDirFromVendorDirCode . '/';
-            \$prefixes = array($prefixes);
-            foreach (\$prefixes as \$prefix) {
-                if (0 !== strpos(\$class, \$prefix)) {
-                    continue;
-                }
-                \$path = \$dir . implode('/', array_slice(explode('\\\\', \$class), $levels)).'.php';
-                if (!\$path = stream_resolve_include_path(\$path)) {
-                    return false;
-                }
-                require \$path;
-
-                return true;
+    public static function autoload(\$class)
+    {
+        \$dir = $baseDirFromTargetDirCode . '/';
+        \$prefixes = array($prefixes);
+        foreach (\$prefixes as \$prefix) {
+            if (0 !== strpos(\$class, \$prefix)) {
+                continue;
             }
+            \$path = \$dir . implode('/', array_slice(explode('\\\\', \$class), $levels)).'.php';
+            if (!\$path = stream_resolve_include_path(\$path)) {
+                return false;
+            }
+            require \$path;
+
+            return true;
         }
+    }
 
 EOF;
         }
@@ -128,7 +128,7 @@ EOF;
         $filesCode = "";
         $autoloads['files'] = new \RecursiveIteratorIterator(new \RecursiveArrayIterator($autoloads['files']));
         foreach ($autoloads['files'] as $functionFile) {
-            $filesCode .= '            require '.$this->getPathCode($filesystem, $relVendorPath, $vendorPath, $functionFile).";\n";
+            $filesCode .= '        require '.$this->getPathCode($filesystem, $relVendorPath, $vendorPath, $functionFile).";\n";
         }
 
         file_put_contents($targetDir.'/autoload_namespaces.php', $namespacesFile);
@@ -136,7 +136,8 @@ EOF;
         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, true, true, (bool) $includePathFile, $targetDirLoader, $filesCode, $classSuffix));
+        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));
         copy(__DIR__.'/ClassLoader.php', $targetDir.'/ClassLoader.php');
     }
 
@@ -278,13 +279,30 @@ EOF;
         return $baseDir.var_export($path, true);
     }
 
-    protected function getAutoloadFile($vendorPathToTargetDirCode, $usePSR0, $useClassMap, $useIncludePath, $targetDirLoader, $filesCode, $classSuffix)
+    protected function getAutoloadFile($vendorPathToTargetDirCode, $suffix)
+    {
+
+
+        return <<<AUTOLOAD
+<?php
+
+// autoload.php generated by Composer
+
+require_once $vendorPathToTargetDirCode . '/autoload_real$suffix.php';
+
+return ComposerAutoloaderInit$suffix::getLoader();
+
+AUTOLOAD;
+    }
+
+    protected function getAutoloadRealFile($usePSR0, $useClassMap, $useIncludePath, $targetDirLoader, $filesCode, $vendorPathCode, $appBaseDirCode, $suffix)
     {
         // TODO the class ComposerAutoloaderInit should be revert to a closure
         // when APC has been fixed:
         // - https://github.com/composer/composer/issues/959
         // - https://bugs.php.net/bug.php?id=52144
         // - https://bugs.php.net/bug.php?id=61576
+        // - https://bugs.php.net/bug.php?id=59298
 
         if ($filesCode) {
             $filesCode = "\n".$filesCode;
@@ -293,27 +311,27 @@ EOF;
         $file = <<<HEADER
 <?php
 
-// autoload.php generated by Composer
+// autoload_real$suffix.php generated by Composer
 if (!class_exists('Composer\\\\Autoload\\\\ClassLoader', false)) {
-    require $vendorPathToTargetDirCode . '/ClassLoader.php';
+    require __DIR__ . '/ClassLoader.php';
 }
 
-if (!class_exists('ComposerAutoloaderInit$classSuffix', false)) {
-    class ComposerAutoloaderInit$classSuffix
+class ComposerAutoloaderInit$suffix
+{
+    public static function getLoader()
     {
-        public static function getLoader()
-        {
-            \$loader = new \\Composer\\Autoload\\ClassLoader();
-            \$composerDir = $vendorPathToTargetDirCode;
+        \$loader = new \\Composer\\Autoload\\ClassLoader();
+        \$vendorDir = $vendorPathCode;
+        \$baseDir = $appBaseDirCode;
 
 
 HEADER;
 
         if ($useIncludePath) {
             $file .= <<<'INCLUDE_PATH'
-            $includePaths = require $composerDir . '/include_paths.php';
-            array_push($includePaths, get_include_path());
-            set_include_path(join(PATH_SEPARATOR, $includePaths));
+        $includePaths = require __DIR__ . '/include_paths.php';
+        array_push($includePaths, get_include_path());
+        set_include_path(join(PATH_SEPARATOR, $includePaths));
 
 
 INCLUDE_PATH;
@@ -321,10 +339,10 @@ INCLUDE_PATH;
 
         if ($usePSR0) {
             $file .= <<<'PSR0'
-            $map = require $composerDir . '/autoload_namespaces.php';
-            foreach ($map as $namespace => $path) {
-                $loader->add($namespace, $path);
-            }
+        $map = require __DIR__ . '/autoload_namespaces.php';
+        foreach ($map as $namespace => $path) {
+            $loader->add($namespace, $path);
+        }
 
 
 PSR0;
@@ -332,10 +350,10 @@ PSR0;
 
         if ($useClassMap) {
             $file .= <<<'CLASSMAP'
-            $classMap = require $composerDir . '/autoload_classmap.php';
-            if ($classMap) {
-                $loader->addClassMap($classMap);
-            }
+        $classMap = require __DIR__ . '/autoload_classmap.php';
+        if ($classMap) {
+            $loader->addClassMap($classMap);
+        }
 
 
 CLASSMAP;
@@ -343,7 +361,7 @@ CLASSMAP;
 
         if ($targetDirLoader) {
             $file .= <<<REGISTER_AUTOLOAD
-            spl_autoload_register(array('ComposerAutoloaderInit$classSuffix', 'autoload'));
+        spl_autoload_register(array('ComposerAutoloaderInit$suffix', 'autoload'));
 
 
 REGISTER_AUTOLOAD;
@@ -351,23 +369,21 @@ REGISTER_AUTOLOAD;
         }
 
         $file .= <<<METHOD_FOOTER
-            \$loader->register();
+        \$loader->register();
 $filesCode
-            return \$loader;
-        }
+        return \$loader;
+    }
 
 METHOD_FOOTER;
 
         $file .= $targetDirLoader;
 
         return $file . <<<FOOTER
-    }
 }
 
-return ComposerAutoloaderInit$classSuffix::getLoader();
-
 FOOTER;
 
     }
+
 }
 

+ 2 - 0
tests/Composer/Test/Autoload/AutoloadGeneratorTest.php

@@ -159,6 +159,7 @@ class AutoloadGeneratorTest extends TestCase
 
         $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', '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');
     }
 
     public function testVendorsAutoloading()
@@ -275,6 +276,7 @@ class AutoloadGeneratorTest extends TestCase
 
         $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', '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');
 
         include $this->vendorDir . '/autoload.php';
         $this->assertTrue(function_exists('testFilesAutoloadGeneration1'));

+ 1 - 30
tests/Composer/Test/Autoload/Fixtures/autoload_functions.php

@@ -1,36 +1,7 @@
 <?php
 
 // autoload.php generated by Composer
-if (!class_exists('Composer\\Autoload\\ClassLoader', false)) {
-    require __DIR__ . '/composer' . '/ClassLoader.php';
-}
 
-if (!class_exists('ComposerAutoloaderInitFilesAutoload', false)) {
-    class ComposerAutoloaderInitFilesAutoload
-    {
-        public static function getLoader()
-        {
-            $loader = new \Composer\Autoload\ClassLoader();
-            $composerDir = __DIR__ . '/composer';
-
-            $map = require $composerDir . '/autoload_namespaces.php';
-            foreach ($map as $namespace => $path) {
-                $loader->add($namespace, $path);
-            }
-
-            $classMap = require $composerDir . '/autoload_classmap.php';
-            if ($classMap) {
-                $loader->addClassMap($classMap);
-            }
-
-            $loader->register();
-
-            require $vendorDir . '/a/a/test.php';
-            require $vendorDir . '/b/b/test2.php';
-
-            return $loader;
-        }
-    }
-}
+require_once __DIR__ . '/composer' . '/autoload_realFilesAutoload.php';
 
 return ComposerAutoloaderInitFilesAutoload::getLoader();

+ 33 - 0
tests/Composer/Test/Autoload/Fixtures/autoload_real_functions.php

@@ -0,0 +1,33 @@
+<?php
+
+// autoload_realFilesAutoload.php generated by Composer
+if (!class_exists('Composer\\Autoload\\ClassLoader', false)) {
+    require __DIR__ . '/ClassLoader.php';
+}
+
+class ComposerAutoloaderInitFilesAutoload
+{
+    public static function getLoader()
+    {
+        $loader = new \Composer\Autoload\ClassLoader();
+        $vendorDir = dirname(__DIR__);
+        $baseDir = dirname($vendorDir);
+
+        $map = require __DIR__ . '/autoload_namespaces.php';
+        foreach ($map as $namespace => $path) {
+            $loader->add($namespace, $path);
+        }
+
+        $classMap = require __DIR__ . '/autoload_classmap.php';
+        if ($classMap) {
+            $loader->addClassMap($classMap);
+        }
+
+        $loader->register();
+
+        require $vendorDir . '/a/a/test.php';
+        require $vendorDir . '/b/b/test2.php';
+
+        return $loader;
+    }
+}

+ 50 - 0
tests/Composer/Test/Autoload/Fixtures/autoload_real_target_dir.php

@@ -0,0 +1,50 @@
+<?php
+
+// autoload_realTargetDir.php generated by Composer
+if (!class_exists('Composer\\Autoload\\ClassLoader', false)) {
+    require __DIR__ . '/ClassLoader.php';
+}
+
+class ComposerAutoloaderInitTargetDir
+{
+    public static function getLoader()
+    {
+        $loader = new \Composer\Autoload\ClassLoader();
+        $vendorDir = dirname(__DIR__);
+        $baseDir = dirname($vendorDir);
+
+        $map = require __DIR__ . '/autoload_namespaces.php';
+        foreach ($map as $namespace => $path) {
+            $loader->add($namespace, $path);
+        }
+
+        $classMap = require __DIR__ . '/autoload_classmap.php';
+        if ($classMap) {
+            $loader->addClassMap($classMap);
+        }
+
+        spl_autoload_register(array('ComposerAutoloaderInitTargetDir', 'autoload'));
+
+        $loader->register();
+
+        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;
+        }
+    }
+}

+ 1 - 47
tests/Composer/Test/Autoload/Fixtures/autoload_target_dir.php

@@ -1,53 +1,7 @@
 <?php
 
 // autoload.php generated by Composer
-if (!class_exists('Composer\\Autoload\\ClassLoader', false)) {
-    require __DIR__ . '/composer' . '/ClassLoader.php';
-}
 
-if (!class_exists('ComposerAutoloaderInitTargetDir', false)) {
-    class ComposerAutoloaderInitTargetDir
-    {
-        public static function getLoader()
-        {
-            $loader = new \Composer\Autoload\ClassLoader();
-            $composerDir = __DIR__ . '/composer';
-
-            $map = require $composerDir . '/autoload_namespaces.php';
-            foreach ($map as $namespace => $path) {
-                $loader->add($namespace, $path);
-            }
-
-            $classMap = require $composerDir . '/autoload_classmap.php';
-            if ($classMap) {
-                $loader->addClassMap($classMap);
-            }
-
-            spl_autoload_register(array('ComposerAutoloaderInitTargetDir', 'autoload'));
-
-            $loader->register();
-
-            return $loader;
-        }
-
-        public static function autoload($class)
-        {
-            $dir = 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;
-            }
-        }
-    }
-}
+require_once __DIR__ . '/composer' . '/autoload_realTargetDir.php';
 
 return ComposerAutoloaderInitTargetDir::getLoader();