From 7bb394e2c4daf520d8dbb0be3c7ec1ecceebe6d8 Mon Sep 17 00:00:00 2001 From: Joshua Thijssen Date: Thu, 26 Mar 2015 11:06:01 +0100 Subject: [PATCH 1/2] Added separated handling of root paths --- src/Symfony/Component/Filesystem/Filesystem.php | 9 +++++++-- .../Component/Filesystem/Tests/FilesystemTest.php | 2 ++ 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/src/Symfony/Component/Filesystem/Filesystem.php b/src/Symfony/Component/Filesystem/Filesystem.php index a934fd201fdf..0952bd2e9a3a 100644 --- a/src/Symfony/Component/Filesystem/Filesystem.php +++ b/src/Symfony/Component/Filesystem/Filesystem.php @@ -330,8 +330,13 @@ public function makePathRelative($endPath, $startPath) // Determine how deep the start path is relative to the common path (ie, "web/bundles" = 2 levels) $depth = count($startPathArr) - $index; - // Repeated "../" for each level need to reach the common path - $traverser = str_repeat('../', $depth); + // When we need to traverse from the start, and we are starting from a root path, don't add '../' + if ($startPath[0] == '/' && $index == 0 && $depth == 1) { + $traverser = ''; + } else { + // Repeated "../" for each level need to reach the common path + $traverser = str_repeat('../', $depth); + } $endPathRemainder = implode('/', array_slice($endPathArr, $index)); diff --git a/src/Symfony/Component/Filesystem/Tests/FilesystemTest.php b/src/Symfony/Component/Filesystem/Tests/FilesystemTest.php index cd9adee97ec2..b57610cb8120 100644 --- a/src/Symfony/Component/Filesystem/Tests/FilesystemTest.php +++ b/src/Symfony/Component/Filesystem/Tests/FilesystemTest.php @@ -781,6 +781,8 @@ public function providePathsForMakePathRelative() array('/a/aab/bb', '/a/aa/', '../aab/bb/'), array('/a/aab/bb/', '/a/aa', '../aab/bb/'), array('/a/aab/bb/', '/a/aa/', '../aab/bb/'), + array('/a/aab/bb/', '/', 'a/aab/bb/'), + array('/a/aab/bb/', '/b/aab', '../../a/aab/bb/'), ); if ('\\' === DIRECTORY_SEPARATOR) { From 791b1247f90adf38bb4d41189fe0b30f2ed8154f Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Tue, 13 Oct 2015 17:24:19 +0200 Subject: [PATCH 2/2] fixed CS --- src/Symfony/Component/Filesystem/Filesystem.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Symfony/Component/Filesystem/Filesystem.php b/src/Symfony/Component/Filesystem/Filesystem.php index 0952bd2e9a3a..6fc9b8b5f7c0 100644 --- a/src/Symfony/Component/Filesystem/Filesystem.php +++ b/src/Symfony/Component/Filesystem/Filesystem.php @@ -331,7 +331,7 @@ public function makePathRelative($endPath, $startPath) $depth = count($startPathArr) - $index; // When we need to traverse from the start, and we are starting from a root path, don't add '../' - if ($startPath[0] == '/' && $index == 0 && $depth == 1) { + if ('/' === $startPath[0] && 0 === $index && 1 === $depth) { $traverser = ''; } else { // Repeated "../" for each level need to reach the common path