Prechádzať zdrojové kódy

Add whitelist to the classmap generator to allow skipping of Test files

Jordi Boggiano 13 rokov pred
rodič
commit
ee14950972

+ 7 - 2
src/Composer/Autoload/AutoloadGenerator.php

@@ -121,7 +121,12 @@ EOF;
         foreach ($autoloads['psr-0'] as $namespace => $paths) {
             foreach ($paths as $dir) {
                 $dir = $this->getPath($filesystem, $relVendorPath, $vendorPath, $dir);
-                foreach (ClassMapGenerator::createMap($dir) as $class => $path) {
+                $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])) {
@@ -304,7 +309,7 @@ EOF;
                 // path starts with vendor dir
                 return $vendorPath . substr($path, strlen($relVendorPath));
             }
-            return getcwd().'/'.$path;
+            return strtr(getcwd(), '\\', '/').'/'.$path;
         }
 
         return $path;

+ 6 - 1
src/Composer/Autoload/ClassMapGenerator.php

@@ -41,10 +41,11 @@ class ClassMapGenerator
      * Iterate over all files in the given directory searching for classes
      *
      * @param Iterator|string $dir The directory to search in or an iterator
+     * @param string $whitelist Regex that matches against the file path
      *
      * @return array A class map array
      */
-    public static function createMap($dir)
+    public static function createMap($dir, $whitelist = null)
     {
         if (is_string($dir)) {
             if (is_file($dir)) {
@@ -67,6 +68,10 @@ class ClassMapGenerator
                 continue;
             }
 
+            if ($whitelist && !preg_match($whitelist, strtr($path, '\\', '/'))) {
+                continue;
+            }
+
             $classes = self::findClasses($path);
 
             foreach ($classes as $class) {