diff --git a/src/Composer/Downloader/Util/Filesystem.php b/src/Composer/Downloader/Util/Filesystem.php index e20296b51..e486d8631 100644 --- a/src/Composer/Downloader/Util/Filesystem.php +++ b/src/Composer/Downloader/Util/Filesystem.php @@ -61,7 +61,7 @@ class Filesystem $from = rtrim(strtr($from, '\\', '/'), '/'); $to = rtrim(strtr($to, '\\', '/'), '/'); - $commonPath = strtr(dirname($to), '\\', '/'); + $commonPath = $to; while (strpos($from, $commonPath) !== 0 && '/' !== $commonPath && !preg_match('{^[a-z]:/?$}i', $commonPath) && '.' !== $commonPath) { $commonPath = strtr(dirname($commonPath), '\\', '/'); } @@ -70,14 +70,10 @@ class Filesystem return $to; } - if (strpos($from, $to) === 0) { - $sourcePathDepth = substr_count(substr($from, strlen($commonPath)), '/'); - return str_repeat('../', $sourcePathDepth); - } $commonPath = rtrim($commonPath, '/') . '/'; $sourcePathDepth = substr_count(substr($from, strlen($commonPath)), '/'); $commonPathCode = str_repeat('../', $sourcePathDepth); - return $commonPathCode . substr($to, strlen($commonPath)); + return ($commonPathCode . substr($to, strlen($commonPath))) ?: './'; } /** @@ -100,26 +96,23 @@ class Filesystem $from = strtr($from, '\\', '/'); $to = strtr($to, '\\', '/'); - $commonPath = dirname($to); - while (strpos($from, $commonPath) !== 0 && '/' !== $commonPath && !preg_match('{^[a-z]:/$}i', $commonPath)) { + $commonPath = $to; + while (strpos($from, $commonPath) !== 0 && '/' !== $commonPath && !preg_match('{^[a-z]:/?$}i', $commonPath) && '.' !== $commonPath) { $commonPath = strtr(dirname($commonPath), '\\', '/'); } - if (0 !== strpos($from, $commonPath) || '/' === $commonPath) { + if (0 !== strpos($from, $commonPath) || '/' === $commonPath || '.' === $commonPath) { return var_export($to, true); } $commonPath = rtrim($commonPath, '/') . '/'; - if (strpos($to, $from) === 0) { + if (strpos($to, $from.'/') === 0) { return '__DIR__ . '.var_export(substr($to, strlen($from)), true); } - if (strpos($from, $to) === 0) { - $sourcePathDepth = substr_count(substr($from, strlen($commonPath)), '/') - 1 + $directories; - return str_repeat('dirname(', $sourcePathDepth).'__DIR__'.str_repeat(')', $sourcePathDepth); - } $sourcePathDepth = substr_count(substr($from, strlen($commonPath)), '/') + $directories; $commonPathCode = str_repeat('dirname(', $sourcePathDepth).'__DIR__'.str_repeat(')', $sourcePathDepth); - return $commonPathCode . '.' . var_export('/' . substr($to, strlen($commonPath)), true); + $relTarget = substr($to, strlen($commonPath)); + return $commonPathCode . (strlen($relTarget) ? '.' . var_export('/' . $relTarget, true) : ''); } /** diff --git a/tests/Composer/Test/Downloader/Util/FilesystemTest.php b/tests/Composer/Test/Downloader/Util/FilesystemTest.php index 592875dc2..01a3189b8 100644 --- a/tests/Composer/Test/Downloader/Util/FilesystemTest.php +++ b/tests/Composer/Test/Downloader/Util/FilesystemTest.php @@ -49,6 +49,8 @@ class FilesystemTest extends TestCase array('c:\\bin\\run', 'd:/vendor/acme/bin/run', true, "'d:/vendor/acme/bin/run'"), array('C:/Temp/test', 'C:\Temp', true, "dirname(__DIR__)"), array('C:/Temp', 'C:\Temp\test', true, "__DIR__ . '/test'"), + array('/tmp/test', '/tmp', true, "dirname(__DIR__)"), + array('/tmp', '/tmp/test', true, "__DIR__ . '/test'"), ); } @@ -73,7 +75,11 @@ class FilesystemTest extends TestCase 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"), array('c:\\bin\\run', 'd:/vendor/acme/bin/run', "d:/vendor/acme/bin/run"), - array('C:/Temp/test', 'C:\Temp', "../"), + array('C:/Temp/test', 'C:\Temp', "./"), + array('/tmp/test', '/tmp', "./"), + array('C:/Temp/test/sub', 'C:\Temp', "../"), + array('/tmp/test/sub', '/tmp', "../"), + array('/tmp', '/tmp/test', "test"), array('C:/Temp', 'C:\Temp\test', "test"), ); }