8000 [Filesystem] Fixed makePathRelative by ausi · Pull Request #22321 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Filesystem] Fixed makePathRelative #22321

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

Closed
wants to merge 11 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Do not strip ../ segments from the beginning of relative paths
  • Loading branch information
ausi committed Apr 6, 2017
commit 9586e880d69f613b10e23dd53cea877e622b221a
9 changes: 5 additions & 4 deletions src/Symfony/Component/Filesystem/Filesystem.php
D810
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down
6 changes: 6 additions & 0 deletions src/Symfony/Component/Filesystem/Tests/FilesystemTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
0