Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[filesystem] makeRelativePath does not work correctly from root #16226

Merged
merged 2 commits into from
Oct 16, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 7 additions & 2 deletions src/Symfony/Component/Filesystem/Filesystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -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] && 0 === $index && 1 === $depth) {
$traverser = '';
} else {
// Repeated "../" for each level need to reach the common path
$traverser = str_repeat('../', $depth);
}

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

Expand Down
2 changes: 2 additions & 0 deletions src/Symfony/Component/Filesystem/Tests/FilesystemTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down