浏览代码

Up-level relative paths in exclude-from-classmap

Omer Karadagli 9 年之前
父节点
当前提交
4046ae042d
共有 2 个文件被更改,包括 21 次插入2 次删除
  1. 18 1
      src/Composer/Autoload/AutoloadGenerator.php
  2. 3 1
      tests/Composer/Test/Autoload/AutoloadGeneratorTest.php

+ 18 - 1
src/Composer/Autoload/AutoloadGenerator.php

@@ -697,8 +697,25 @@ FOOTER;
                         // add support for wildcards * and **
                         $path = str_replace('\\*\\*', '.+?', $path);
                         $path = str_replace('\\*', '[^/]+?', $path);
+                        // add support for up-level relative paths
+                        $path = preg_replace_callback(
+                            '{^((\\\.{1,2})+)/}',
+                            function ($matches) use (&$updir) {
+                                if (isset($matches[1])) {
+                                    // undo preg_quote for the matched string
+                                    $updir = str_replace('\\.', '.', $matches[1]);
+                                }
+
+                                return '';
+                            },
+                            $path
+                        );
+                        if (empty($installPath)) {
+                            $installPath = strtr(getcwd(), '\\', '/');
+                        }
 
-                        $autoloads[] = empty($installPath) ? preg_quote(strtr(getcwd(), '\\', '/')) . '/' . $path : preg_quote($installPath) . '/' . $path;
+                        $resolvedPath = realpath($installPath . '/' . $updir);
+                        $autoloads[] = preg_quote(strtr($resolvedPath, '\\', '/')) . '/' . $path;
                         continue;
                     }
 

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

@@ -1084,6 +1084,7 @@ EOF;
             'psr-4' => array('Acme\Foo\\' => '../path/../src-psr4'),
             'classmap' => array('../classmap'),
             'files' => array('../test.php'),
+            'exclude-from-classmap' => array('./../classmap/excluded'),
         ));
 
         $this->repository->expects($this->once())
@@ -1091,9 +1092,10 @@ EOF;
             ->will($this->returnValue(array()));
 
         $this->fs->ensureDirectoryExists($this->workingDir.'/src/Foo');
-        $this->fs->ensureDirectoryExists($this->workingDir.'/classmap');
+        $this->fs->ensureDirectoryExists($this->workingDir.'/classmap/excluded');
         file_put_contents($this->workingDir.'/src/Foo/Bar.php', '<?php namespace Foo; class Bar {}');
         file_put_contents($this->workingDir.'/classmap/classes.php', '<?php namespace Foo; class Foo {}');
+        file_put_contents($this->workingDir.'/classmap/excluded/classes.php', '<?php namespace Foo; class Boo {}');
         file_put_contents($this->workingDir.'/test.php', '<?php class Foo {}');
 
         $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', true, '_14');