Browse Source

Ignore classes in ClassMapGenerator

Jordi Boggiano 11 years ago
parent
commit
05d9912f97

+ 6 - 6
src/Composer/Autoload/AutoloadGenerator.php

@@ -184,12 +184,12 @@ EOF;
                             preg_quote($dir),
                             ($psrType === 'psr-0' && strpos($namespace, '_') === false) ? preg_quote(strtr($namespace, '\\', '/')) : ''
                         );
-                        foreach (ClassMapGenerator::createMap($dir, $whitelist, $this->io) as $class => $path) {
-                            if ('' === $namespace || 0 === strpos($class, $namespace)) {
-                                if (!isset($classMap[$class])) {
-                                    $path = $this->getPathCode($filesystem, $basePath, $vendorPath, $path);
-                                    $classMap[$class] = $path.",\n";
-                                }
+
+                        $namespaceFilter = $namespace === '' ? null : $namespace;
+                        foreach (ClassMapGenerator::createMap($dir, $whitelist, $this->io, $namespaceFilter) as $class => $path) {
+                            if (!isset($classMap[$class])) {
+                                $path = $this->getPathCode($filesystem, $basePath, $vendorPath, $path);
+                                $classMap[$class] = $path.",\n";
                             }
                         }
                     }

+ 9 - 2
src/Composer/Autoload/ClassMapGenerator.php

@@ -45,13 +45,15 @@ class ClassMapGenerator
      * Iterate over all files in the given directory searching for classes
      *
      * @param \Iterator|string $path      The path to search in or an iterator
-     * @param string          $whitelist Regex that matches against the file path
+     * @param string           $whitelist Regex that matches against the file path
+     * @param IOInterface      $io        IO object
+     * @param string           $namespace Optional namespace prefix to filter by
      *
      * @return array A class map array
      *
      * @throws \RuntimeException When the path is neither an existing file nor directory
      */
-    public static function createMap($path, $whitelist = null, IOInterface $io = null)
+    public static function createMap($path, $whitelist = null, IOInterface $io = null, $namespace = null)
     {
         if (is_string($path)) {
             if (is_file($path)) {
@@ -82,6 +84,11 @@ class ClassMapGenerator
             $classes = self::findClasses($filePath);
 
             foreach ($classes as $class) {
+                // skip classes not within the given namespace prefix
+                if (null !== $namespace && 0 !== strpos($class, $namespace)) {
+                    continue;
+                }
+
                 if (!isset($map[$class])) {
                     $map[$class] = $filePath;
                 } elseif ($io && $map[$class] !== $filePath && !preg_match('{/(test|fixture|example)s?/}i', strtr($map[$class].' '.$filePath, '\\', '/'))) {