From fc745f45949fdb8d5aa590618ec73537721f99b4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Auswo=CC=88ger?= Date: Thu, 6 Apr 2017 20:04:32 +0200 Subject: [PATCH 01/11] Remove unnecessary if construct If `0 === $depth` evaluates to true `str_repeat('../', $depth)` would result in the empty string anyway, so there is no need to handle this case. --- src/Symfony/Component/Filesystem/Filesystem.php | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/src/Symfony/Component/Filesystem/Filesystem.php b/src/Symfony/Component/Filesystem/Filesystem.php index edfc1b9d46a23..648647e71f284 100644 --- a/src/Symfony/Component/Filesystem/Filesystem.php +++ b/src/Symfony/Component/Filesystem/Filesystem.php @@ -400,13 +400,8 @@ 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] && 0 === $index && 0 === $depth) { - $traverser = ''; - } else { - // Repeated "../" for each level need to reach the common path - $traverser = str_repeat('../', $depth); - } + // Repeated "../" for each level need to reach the common path + $traverser = str_repeat('../', $depth); $endPathRemainder = implode('/', array_slice($endPathArr, $index)); From 3a29c1de8e05bba7ed494cc65f1da4af5bcd6d8d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Auswo=CC=88ger?= Date: Thu, 6 Apr 2017 20:14:36 +0200 Subject: [PATCH 02/11] Do not strip the beginning of relative paths, only drive letters --- .../Component/Filesystem/Filesystem.php | 18 ++++++++++-------- .../Filesystem/Tests/FilesystemTest.php | 3 +++ 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/src/Symfony/Component/Filesystem/Filesystem.php b/src/Symfony/Component/Filesystem/Filesystem.php index 648647e71f284..42b676d36a1ab 100644 --- a/src/Symfony/Component/Filesystem/Filesystem.php +++ b/src/Symfony/Component/Filesystem/Filesystem.php @@ -358,18 +358,20 @@ public function makePathRelative($endPath, $startPath) $startPath = str_replace('\\', '/', $startPath); } + $stripDriveLetter = function($path) { + if (strlen($path) > 2 && substr($path, 1, 2) === ':/' && ctype_alpha($path[0])) { + return substr($path, 2); + } + return $path; + }; + + $endPath = $stripDriveLetter($endPath); + $startPath = $stripDriveLetter($startPath); + // Split the paths into arrays $startPathArr = explode('/', trim($startPath, '/')); $endPathArr = explode('/', trim($endPath, '/')); - if ('/' !== $startPath[0]) { - array_shift($startPathArr); - } - - if ('/' !== $endPath[0]) { - array_shift($endPathArr); - } - $normalizePathArray = function ($pathSegments) { $result = array(); diff --git a/src/Symfony/Component/Filesystem/Tests/FilesystemTest.php b/src/Symfony/Component/Filesystem/Tests/FilesystemTest.php index ab2395cd001c0..82c96620853ca 100644 --- a/src/Symfony/Component/Filesystem/Tests/FilesystemTest.php +++ b/src/Symfony/Component/Filesystem/Tests/FilesystemTest.php @@ -847,6 +847,7 @@ public function providePathsForMakePathRelative() array('/var/lib/symfony/src/Symfony', '/var/lib/symfony/src/Symfony/Component/', '../'), array('var/lib/symfony/', 'var/lib/symfony/src/Symfony/Component', '../../../'), array('/usr/lib/symfony/', '/var/lib/symfony/src/Symfony/Component', '../../../../../../usr/lib/symfony/'), + array('usr/lib/symfony/', 'var/lib/symfony/src/Symfony/Component', '../../../../../../usr/lib/symfony/'), array('/var/lib/symfony/src/Symfony/', '/var/lib/symfony/', 'src/Symfony/'), array('/aa/bb', '/aa/bb', './'), array('/aa/bb', '/aa/bb/', './'), @@ -878,6 +879,8 @@ public function providePathsForMakePathRelative() array('C:/aa/bb/../../cc', 'C:/aa/../dd/..', 'cc/'), array('C:/../aa/bb/cc', 'C:/aa/dd/..', 'bb/cc/'), array('C:/../../aa/../bb/cc', 'C:/aa/dd/..', '../bb/cc/'), + array('aa/bb', 'aa/cc', '../bb/'), + array('aa/cc', 'bb/cc', '../../aa/cc/'), ); if ('\\' === DIRECTORY_SEPARATOR) { From 15982d4b083723555cfa149368eaaae9609d0e22 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Auswo=CC=88ger?= Date: Thu, 6 Apr 2017 20:15:45 +0200 Subject: [PATCH 03/11] Normalize ./ path segments --- src/Symfony/Component/Filesystem/Filesystem.php | 2 +- src/Symfony/Component/Filesystem/Tests/FilesystemTest.php | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/src/Symfony/Component/Filesystem/Filesystem.php b/src/Symfony/Component/Filesystem/Filesystem.php index 42b676d36a1ab..9da8ac5f83051 100644 --- a/src/Symfony/Component/Filesystem/Filesystem.php +++ b/src/Symfony/Component/Filesystem/Filesystem.php @@ -378,7 +378,7 @@ public function makePathRelative($endPath, $startPath) foreach ($pathSegments as $segment) { if ('..' === $segment) { array_pop($result); - } else { + } elseif ('.' !== $segment) { $result[] = $segment; } } diff --git a/src/Symfony/Component/Filesystem/Tests/FilesystemTest.php b/src/Symfony/Component/Filesystem/Tests/FilesystemTest.php index 82c96620853ca..3bdf60f0b2894 100644 --- a/src/Symfony/Component/Filesystem/Tests/FilesystemTest.php +++ b/src/Symfony/Component/Filesystem/Tests/FilesystemTest.php @@ -881,6 +881,9 @@ public function providePathsForMakePathRelative() array('C:/../../aa/../bb/cc', 'C:/aa/dd/..', '../bb/cc/'), array('aa/bb', 'aa/cc', '../bb/'), array('aa/cc', 'bb/cc', '../../aa/cc/'), + array('aa/bb', 'aa/./cc', '../bb/'), + array('aa/./bb', 'aa/cc', '../bb/'), + array('aa/./bb', 'aa/./cc', '../bb/'), ); if ('\\' === DIRECTORY_SEPARATOR) { From 9586e880d69f613b10e23dd53cea877e622b221a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Auswo=CC=88ger?= Date: Thu, 6 Apr 2017 20:16:30 +0200 Subject: [PATCH 04/11] Do not strip ../ segments from the beginning of relative paths --- src/Symfony/Component/Filesystem/Filesystem.php | 9 +++++---- .../Component/Filesystem/Tests/FilesystemTest.php | 6 ++++++ 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/src/Symfony/Component/Filesystem/Filesystem.php b/src/Symfony/Component/Filesystem/Filesystem.php index 9da8ac5f83051..3902aed3ad011 100644 --- a/src/Symfony/Component/Filesystem/Filesystem.php +++ b/src/Symfony/Component/Filesystem/Filesystem.php @@ -372,11 +372,12 @@ public function makePathRelative($endPath, $startPath) $startPathArr = explode('/', trim($startPath, '/')); $endPathArr = explode('/', trim($endPath, '/')); - $normalizePathArray = function ($pathSegments) { + $normalizePathArray = function ($pathSegments, $path) { + $absolute = static::isAbsolutePath($path); $result = array(); foreach ($pathSegments as $segment) { - if ('..' === $segment) { + if ('..' === $segment && ($absolute || count($result))) { array_pop($result); } elseif ('.' !== $segment) { $result[] = $segment; @@ -386,8 +387,8 @@ public function makePathRelative($endPath, $startPath) return $result; }; - $startPathArr = $normalizePathArray($startPathArr); - $endPathArr = $normalizePathArray($endPathArr); + $startPathArr = $normalizePathArray($startPathArr, $startPath); + $endPathArr = $normalizePathArray($endPathArr, $endPath); // Find for which directory the common path stops $index = 0; diff --git a/src/Symfony/Component/Filesystem/Tests/FilesystemTest.php b/src/Symfony/Component/Filesystem/Tests/FilesystemTest.php index 3bdf60f0b2894..d2058d0a01cf3 100644 --- a/src/Symfony/Component/Filesystem/Tests/FilesystemTest.php +++ b/src/Symfony/Component/Filesystem/Tests/FilesystemTest.php @@ -884,6 +884,12 @@ public function providePathsForMakePathRelative() array('aa/bb', 'aa/./cc', '../bb/'), array('aa/./bb', 'aa/cc', '../bb/'), array('aa/./bb', 'aa/./cc', '../bb/'), + array('../../', '../../', './'), + array('../aa/bb/', 'aa/bb/', '../../../aa/bb/'), + array('../../../', '../../', '../'), + array('', '', './'), + array('', 'aa/', '../'), + array('aa/', '', 'aa/'), ); if ('\\' === DIRECTORY_SEPARATOR) { From cec473eeb099c074b5883e7187f74663402f9d87 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Auswo=CC=88ger?= Date: Thu, 6 Apr 2017 20:17:06 +0200 Subject: [PATCH 05/11] Fix parameter description --- src/Symfony/Component/Filesystem/Filesystem.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Symfony/Component/Filesystem/Filesystem.php b/src/Symfony/Component/Filesystem/Filesystem.php index 3902aed3ad011..f99d22e97564e 100644 --- a/src/Symfony/Component/Filesystem/Filesystem.php +++ b/src/Symfony/Component/Filesystem/Filesystem.php @@ -345,8 +345,8 @@ public function symlink($originDir, $targetDir, $copyOnWindows = false) /** * Given an existing path, convert it to a path relative to a given starting path. * - * @param string $endPath Absolute path of target - * @param string $startPath Absolute path where traversal begins + * @param string $endPath Path of target + * @param string $startPath Path where traversal begins * * @return string Path of target relative to starting path */ From 1b795d7c9b977ad300bd67c57d7fdeea733586bd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Auswo=CC=88ger?= Date: Fri, 7 Apr 2017 09:00:21 +0200 Subject: [PATCH 06/11] Fixed compatibility with PHP 5.3 --- 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 f99d22e97564e..0e79a56d8af20 100644 --- a/src/Symfony/Component/Filesystem/Filesystem.php +++ b/src/Symfony/Component/Filesystem/Filesystem.php @@ -373,7 +373,7 @@ public function makePathRelative($endPath, $startPath) $endPathArr = explode('/', trim($endPath, '/')); $normalizePathArray = function ($pathSegments, $path) { - $absolute = static::isAbsolutePath($path); + $absolute = Filesystem::isAbsolutePath($path); $result = array(); foreach ($pathSegments as $segment) { From fea3bb03d64f400df3db5d473f4346a7535d82df Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Auswo=CC=88ger?= Date: Fri, 7 Apr 2017 09:00:52 +0200 Subject: [PATCH 07/11] Fixed coding style --- src/Symfony/Component/Filesystem/Filesystem.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Symfony/Component/Filesystem/Filesystem.php b/src/Symfony/Component/Filesystem/Filesystem.php index 0e79a56d8af20..abc3b92740bb0 100644 --- a/src/Symfony/Component/Filesystem/Filesystem.php +++ b/src/Symfony/Component/Filesystem/Filesystem.php @@ -358,10 +358,11 @@ public function makePathRelative($endPath, $startPath) $startPath = str_replace('\\', '/', $startPath); } - $stripDriveLetter = function($path) { + $stripDriveLetter = function ($path) { if (strlen($path) > 2 && substr($path, 1, 2) === ':/' && ctype_alpha($path[0])) { return substr($path, 2); } + return $path; }; From 28b29ee4aee3969561d27df033c82d99f7c77309 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Auswo=CC=88ger?= Date: Fri, 7 Apr 2017 09:16:39 +0200 Subject: [PATCH 08/11] Fixed compatibility with PHP 5.3 --- src/Symfony/Component/Filesystem/Filesystem.php | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/Symfony/Component/Filesystem/Filesystem.php b/src/Symfony/Component/Filesystem/Filesystem.php index abc3b92740bb0..2229c05e1e341 100644 --- a/src/Symfony/Component/Filesystem/Filesystem.php +++ b/src/Symfony/Component/Filesystem/Filesystem.php @@ -373,8 +373,7 @@ public function makePathRelative($endPath, $startPath) $startPathArr = explode('/', trim($startPath, '/')); $endPathArr = explode('/', trim($endPath, '/')); - $normalizePathArray = function ($pathSegments, $path) { - $absolute = Filesystem::isAbsolutePath($path); + $normalizePathArray = function ($pathSegments, $absolute) { $result = array(); foreach ($pathSegments as $segment) { @@ -388,8 +387,8 @@ public function makePathRelative($endPath, $startPath) return $result; }; - $startPathArr = $normalizePathArray($startPathArr, $startPath); - $endPathArr = $normalizePathArray($endPathArr, $endPath); + $startPathArr = $normalizePathArray($startPathArr, static::isAbsolutePath($startPath)); + $endPathArr = $normalizePathArray($endPathArr, static::isAbsolutePath($endPath)); // Find for which directory the common path stops $index = 0; From 193cdf802997e2b469e77d919cf74a740e2ba4ee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Auswo=CC=88ger?= Date: Wed, 9 Aug 2017 10:26:50 +0200 Subject: [PATCH 09/11] Revert "Fix parameter description" This reverts commit cec473eeb099c074b5883e7187f74663402f9d87. --- src/Symfony/Component/Filesystem/Filesystem.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Symfony/Component/Filesystem/Filesystem.php b/src/Symfony/Component/Filesystem/Filesystem.php index 2229c05e1e341..88df1aa01184a 100644 --- a/src/Symfony/Component/Filesystem/Filesystem.php +++ b/src/Symfony/Component/Filesystem/Filesystem.php @@ -345,8 +345,8 @@ public function symlink($originDir, $targetDir, $copyOnWindows = false) /** * Given an existing path, convert it to a path relative to a given starting path. * - * @param string $endPath Path of target - * @param string $startPath Path where traversal begins + * @param string $endPath Absolute path of target + * @param string $startPath Absolute path where traversal begins * * @return string Path of target relative to starting path */ From bce33c4925be1eeea6b9c56a1e934d874c67f841 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Auswo=CC=88ger?= Date: Thu, 14 Sep 2017 17:10:24 +0200 Subject: [PATCH 10/11] =?UTF-8?q?Don=E2=80=99t=20use=20substr()?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 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 88df1aa01184a..8eca00a4fd3f2 100644 --- a/src/Symfony/Component/Filesystem/Filesystem.php +++ b/src/Symfony/Component/Filesystem/Filesystem.php @@ -359,7 +359,7 @@ public function makePathRelative($endPath, $startPath) } $stripDriveLetter = function ($path) { - if (strlen($path) > 2 && substr($path, 1, 2) === ':/' && ctype_alpha($path[0])) { + if (strlen($path) > 2 && ':' === $path[1] && '/' === $path[2] && ctype_alpha($path[0])) { return substr($path, 2); } From 23b4a56d070207ffa2d9b80301fcdc592c8b1216 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Auswo=CC=88ger?= Date: Thu, 14 Sep 2017 17:16:01 +0200 Subject: [PATCH 11/11] Coding style --- src/Symfony/Component/Filesystem/Filesystem.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Symfony/Component/Filesystem/Filesystem.php b/src/Symfony/Component/Filesystem/Filesystem.php index 8eca00a4fd3f2..ac2a1c7857030 100644 --- a/src/Symfony/Component/Filesystem/Filesystem.php +++ b/src/Symfony/Component/Filesystem/Filesystem.php @@ -397,7 +397,7 @@ public function makePathRelative($endPath, $startPath) } // Determine how deep the start path is relative to the common path (ie, "web/bundles" = 2 levels) - if (count($startPathArr) === 1 && $startPathArr[0] === '') { + if (1 === count($startPathArr) && '' === $startPathArr[0]) { $depth = 0; } else { $depth = count($startPathArr) - $index; @@ -498,7 +498,7 @@ public function isAbsolutePath($file) { return strspn($file, '/\\', 0, 1) || (strlen($file) > 3 && ctype_alpha($file[0]) - && substr($file, 1, 1) === ':' + && ':' === substr($file, 1, 1) && strspn($file, '/\\', 2, 1) ) || null !== parse_url($file, PHP_URL_SCHEME)