Skip to content

Commit 7bb394e

Browse files
jaytaphfabpot
authored andcommitted
Added separated handling of root paths
1 parent d949fcc commit 7bb394e

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

src/Symfony/Component/Filesystem/Filesystem.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -330,8 +330,13 @@ public function makePathRelative($endPath, $startPath)
330330
// Determine how deep the start path is relative to the common path (ie, "web/bundles" = 2 levels)
331331
$depth = count($startPathArr) - $index;
332332

333-
// Repeated "../" for each level need to reach the common path
334-
$traverser = str_repeat('../', $depth);
333+
// When we need to traverse from the start, and we are starting from a root path, don't add '../'
334+
if ($startPath[0] == '/' && $index == 0 && $depth == 1) {
335+
$traverser = '';
336+
} else {
337+
// Repeated "../" for each level need to reach the common path
338+
$traverser = str_repeat('../', $depth);
339+
}
335340

336341
$endPathRemainder = implode('/', array_slice($endPathArr, $index));
337342

src/Symfony/Component/Filesystem/Tests/FilesystemTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -781,6 +781,8 @@ public function providePathsForMakePathRelative()
781781
array('/a/aab/bb', '/a/aa/', '../aab/bb/'),
782782
array('/a/aab/bb/', '/a/aa', '../aab/bb/'),
783783
array('/a/aab/bb/', '/a/aa/', '../aab/bb/'),
784+
array('/a/aab/bb/', '/', 'a/aab/bb/'),
785+
array('/a/aab/bb/', '/b/aab', '../../a/aab/bb/'),
784786
);
785787

786788
if ('\\' === DIRECTORY_SEPARATOR) {

0 commit comments

Comments
 (0)