Ver código fonte

Added parameter $inDirectory to method Filesystem::findShortestPath

Martin Hasoň 13 anos atrás
pai
commit
1f8c9eeb6c

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

@@ -58,7 +58,7 @@ EOF;
 
         $filesystem = new Filesystem();
         $vendorPath = strtr(realpath($installationManager->getVendorPath()), '\\', '/');
-        $relVendorPath = $filesystem->findShortestPath(getcwd().'/inDir', $vendorPath);
+        $relVendorPath = $filesystem->findShortestPath(getcwd(), $vendorPath, true);
         $vendorDirCode = $filesystem->findShortestPathCode(realpath($targetDir), $vendorPath, true);
 
         $appBaseDir = $filesystem->findShortestPathCode($vendorPath, getcwd(), true);
@@ -125,7 +125,7 @@ EOF;
         $autoloads['classmap'] = new \RecursiveIteratorIterator(new \RecursiveArrayIterator($autoloads['classmap']));
         foreach ($autoloads['classmap'] as $dir) {
             foreach (ClassMapGenerator::createMap($dir) as $class => $path) {
-                $path = '/'.$filesystem->findShortestPath(getcwd().'/inDir', $path);
+                $path = '/'.$filesystem->findShortestPath(getcwd(), $path, true);
                 $classmapFile .= '    '.var_export($class, true).' => $baseDir . '.var_export($path, true).",\n";
             }
         }

+ 7 - 2
src/Composer/Util/Filesystem.php

@@ -58,9 +58,10 @@ class Filesystem
      *
      * @param string $from
      * @param string $to
+     * @param Boolean $inDirectory if true, the source is considered to be in the directory
      * @return string
      */
-    public function findShortestPath($from, $to)
+    public function findShortestPath($from, $to, $inDirectory = false)
     {
         if (!$this->isAbsolutePath($from) || !$this->isAbsolutePath($to)) {
             throw new \InvalidArgumentException('from and to must be absolute paths');
@@ -69,6 +70,10 @@ class Filesystem
         $from = lcfirst(rtrim(strtr($from, '\\', '/'), '/'));
         $to = lcfirst(rtrim(strtr($to, '\\', '/'), '/'));
 
+        if ($inDirectory) {
+            $from .= '/dummy_file';
+        }
+
         if (dirname($from) === dirname($to)) {
             return './'.basename($to);
         }
@@ -143,4 +148,4 @@ class Filesystem
     {
         return new ProcessExecutor;
     }
-}
+}

+ 8 - 2
tests/Composer/Test/Util/FilesystemTest.php

@@ -58,10 +58,10 @@ class FilesystemTest extends TestCase
     /**
      * @dataProvider providePathCouples
      */
-    public function testFindShortestPath($a, $b, $expected)
+    public function testFindShortestPath($a, $b, $expected, $inDirectory = false)
     {
         $fs = new Filesystem;
-        $this->assertEquals($expected, $fs->findShortestPath($a, $b));
+        $this->assertEquals($expected, $fs->findShortestPath($a, $b, $inDirectory));
     }
 
     public function providePathCouples()
@@ -69,8 +69,13 @@ class FilesystemTest extends TestCase
         return array(
             array('/foo/bar', '/foo/bar', "./bar"),
             array('/foo/bar', '/foo/baz', "./baz"),
+            array('/foo/bar/', '/foo/baz', "./baz"),
+            array('/foo/bar', '/foo/bar', "./", true),
+            array('/foo/bar', '/foo/baz', "../baz", true),
+            array('/foo/bar/', '/foo/baz', "../baz", true),
             array('/foo/bin/run', '/foo/vendor/acme/bin/run', "../vendor/acme/bin/run"),
             array('/foo/bin/run', '/bar/bin/run', "/bar/bin/run"),
+            array('/foo/bin/run', '/bar/bin/run', "/bar/bin/run", true),
             array('c:/bin/run', 'c:/vendor/acme/bin/run', "../vendor/acme/bin/run"),
             array('c:\\bin\\run', 'c:/vendor/acme/bin/run', "../vendor/acme/bin/run"),
             array('c:/bin/run', 'd:/vendor/acme/bin/run', "d:/vendor/acme/bin/run"),
@@ -79,6 +84,7 @@ class FilesystemTest extends TestCase
             array('/tmp/test', '/tmp', "./"),
             array('C:/Temp/test/sub', 'C:\Temp', "../"),
             array('/tmp/test/sub', '/tmp', "../"),
+            array('/tmp/test/sub', '/tmp', "../../", true),
             array('/tmp', '/tmp/test', "test"),
             array('C:/Temp', 'C:\Temp\test', "test"),
             array('C:/Temp', 'c:\Temp\test', "test"),