瀏覽代碼

Make PSR0 scanning optional

Jordi Boggiano 13 年之前
父節點
當前提交
44d7d4a38e
共有 2 個文件被更改,包括 29 次插入26 次删除
  1. 17 14
      src/Composer/Autoload/AutoloadGenerator.php
  2. 12 12
      tests/Composer/Test/Autoload/AutoloadGeneratorTest.php

+ 17 - 14
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, $suffix = '')
+    public function dump(Config $config, RepositoryInterface $localRepo, PackageInterface $mainPackage, InstallationManager $installationManager, $targetDir, $scanPsr0Packages = false, $suffix = '')
     {
         $filesystem = new Filesystem();
         $filesystem->ensureDirectoryExists($config->get('vendor-dir'));
@@ -118,19 +118,22 @@ EOF;
         // flatten array
         $classMap = array();
         $autoloads['classmap'] = new \RecursiveIteratorIterator(new \RecursiveArrayIterator($autoloads['classmap']));
-        foreach ($autoloads['psr-0'] as $namespace => $paths) {
-            foreach ($paths as $dir) {
-                $dir = $this->getPath($filesystem, $relVendorPath, $vendorPath, $dir);
-                $whitelist = sprintf(
-                    '{%s/%s.+(?<!(?<!/)Test\.php)$}',
-                    preg_quote(rtrim($dir, '/')),
-                    strpos($namespace, '_') === false ? preg_quote(strtr($namespace, '\\', '/')) : ''
-                );
-                foreach (ClassMapGenerator::createMap($dir, $whitelist) as $class => $path) {
-                    if (0 === strpos($class, $namespace)) {
-                        $path = '/'.$filesystem->findShortestPath(getcwd(), $path, true);
-                        if (!isset($classMap[$class])) {
-                            $classMap[$class] = '$baseDir . '.var_export($path, true).",\n";
+
+        if ($scanPsr0Packages) {
+            foreach ($autoloads['psr-0'] as $namespace => $paths) {
+                foreach ($paths as $dir) {
+                    $dir = $this->getPath($filesystem, $relVendorPath, $vendorPath, $dir);
+                    $whitelist = sprintf(
+                        '{%s/%s.+(?<!(?<!/)Test\.php)$}',
+                        preg_quote(rtrim($dir, '/')),
+                        strpos($namespace, '_') === false ? preg_quote(strtr($namespace, '\\', '/')) : ''
+                    );
+                    foreach (ClassMapGenerator::createMap($dir, $whitelist) as $class => $path) {
+                        if (0 === strpos($class, $namespace)) {
+                            $path = '/'.$filesystem->findShortestPath(getcwd(), $path, true);
+                            if (!isset($classMap[$class])) {
+                                $classMap[$class] = '$baseDir . '.var_export($path, true).",\n";
+                            }
                         }
                     }
                 }

+ 12 - 12
tests/Composer/Test/Autoload/AutoloadGeneratorTest.php

@@ -92,7 +92,7 @@ class AutoloadGeneratorTest extends TestCase
 
         $this->createClassFile($this->workingDir);
 
-        $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', '_1');
+        $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', false, '_1');
         $this->assertAutoloadFiles('main', $this->vendorDir.'/composer');
         $this->assertAutoloadFiles('classmap', $this->vendorDir.'/composer', 'classmap');
     }
@@ -117,7 +117,7 @@ class AutoloadGeneratorTest extends TestCase
 
         $this->createClassFile($this->vendorDir);
 
-        $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', '_2');
+        $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', true, '_2');
         $this->assertAutoloadFiles('main3', $this->vendorDir.'/composer');
         $this->assertAutoloadFiles('classmap3', $this->vendorDir.'/composer', 'classmap');
     }
@@ -140,7 +140,7 @@ class AutoloadGeneratorTest extends TestCase
         $this->fs->ensureDirectoryExists($this->workingDir.'/src');
 
         $this->createClassFile($this->workingDir);
-        $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', '_3');
+        $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', false, '_3');
         $this->assertAutoloadFiles('main2', $this->vendorDir.'/composer');
         $this->assertAutoloadFiles('classmap2', $this->vendorDir.'/composer', 'classmap');
     }
@@ -159,7 +159,7 @@ class AutoloadGeneratorTest extends TestCase
 
         $this->fs->ensureDirectoryExists($this->vendorDir.'/a');
 
-        $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', 'TargetDir');
+        $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_realTargetDir.php');
     }
@@ -184,7 +184,7 @@ class AutoloadGeneratorTest extends TestCase
         $this->fs->ensureDirectoryExists($this->vendorDir.'/a/a/lib');
         $this->fs->ensureDirectoryExists($this->vendorDir.'/b/b/src');
 
-        $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', '_5');
+        $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', false, '_5');
         $this->assertAutoloadFiles('vendors', $this->vendorDir.'/composer');
         $this->assertTrue(file_exists($this->vendorDir.'/composer/autoload_classmap.php'), "ClassMap file needs to be generated, even if empty.");
     }
@@ -211,7 +211,7 @@ class AutoloadGeneratorTest extends TestCase
         file_put_contents($this->vendorDir.'/b/b/src/b.php', '<?php class ClassMapBar {}');
         file_put_contents($this->vendorDir.'/b/b/lib/c.php', '<?php class ClassMapBaz {}');
 
-        $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', '_6');
+        $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', false, '_6');
         $this->assertTrue(file_exists($this->vendorDir.'/composer/autoload_classmap.php'), "ClassMap file needs to be generated.");
         $this->assertEquals(
             array(
@@ -248,7 +248,7 @@ class AutoloadGeneratorTest extends TestCase
         file_put_contents($this->vendorDir.'/b/b/test.php', '<?php class ClassMapBar {}');
         file_put_contents($this->vendorDir.'/c/c/foo/test.php', '<?php class ClassMapBaz {}');
 
-        $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', '_7');
+        $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', false, '_7');
         $this->assertTrue(file_exists($this->vendorDir.'/composer/autoload_classmap.php'), "ClassMap file needs to be generated.");
         $this->assertEquals(
             array(
@@ -280,7 +280,7 @@ class AutoloadGeneratorTest extends TestCase
         file_put_contents($this->vendorDir.'/a/a/test.php', '<?php function testFilesAutoloadGeneration1() {}');
         file_put_contents($this->vendorDir.'/b/b/test2.php', '<?php function testFilesAutoloadGeneration2() {}');
 
-        $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', 'FilesAutoload');
+        $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_realFilesAutoload.php');
 
@@ -346,7 +346,7 @@ return array(
 
 EOF;
 
-        $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', '_9');
+        $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', true, '_9');
         $this->assertEquals($expectedNamespace, file_get_contents($this->vendorDir.'/composer/autoload_namespaces.php'));
         $this->assertEquals($expectedClassmap, file_get_contents($this->vendorDir.'/composer/autoload_classmap.php'));
     }
@@ -375,7 +375,7 @@ EOF;
 
         $this->fs->ensureDirectoryExists($this->vendorDir.'/composer');
 
-        $this->generator->dump($this->config, $this->repository, $package, $this->im, "composer", '_10');
+        $this->generator->dump($this->config, $this->repository, $package, $this->im, "composer", false, '_10');
 
         $this->assertFileEquals(__DIR__.'/Fixtures/include_paths.php', $this->vendorDir.'/composer/include_paths.php');
         $this->assertEquals(
@@ -404,7 +404,7 @@ EOF;
 
         mkdir($this->vendorDir."/composer", 0777, true);
 
-        $this->generator->dump($this->config, $this->repository, $package, $this->im, "composer", '_11');
+        $this->generator->dump($this->config, $this->repository, $package, $this->im, "composer", false, '_11');
 
         $oldIncludePath = get_include_path();
 
@@ -435,7 +435,7 @@ EOF;
 
         mkdir($this->vendorDir."/composer", 0777, true);
 
-        $this->generator->dump($this->config, $this->repository, $package, $this->im, "composer", '_12');
+        $this->generator->dump($this->config, $this->repository, $package, $this->im, "composer", false, '_12');
 
         $this->assertFalse(file_exists($this->vendorDir."/composer/include_paths.php"));
     }