Skip to content

Commit

Permalink
[Filesystem] Several issues with Filesystem::makePathRelative
Browse files Browse the repository at this point in the history
  • Loading branch information
Amrouche Hamza committed Jan 5, 2018
1 parent 05adcd0 commit 53700b6
Show file tree
Hide file tree
Showing 2 changed files with 503 additions and 61 deletions.
25 changes: 15 additions & 10 deletions src/Symfony/Component/Filesystem/Filesystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -370,19 +370,25 @@ public function makePathRelative($endPath, $startPath)
if ('\\' === DIRECTORY_SEPARATOR) {
$endPath = str_replace('\\', '/', $endPath);
$startPath = str_replace('\\', '/', $startPath);
}

$stripDriveLetter = function ($path) {
if (strlen($path) > 2 && ':' === $path[1] && '/' === $path[2] && ctype_alpha($path[0])) {
return substr($path, 2);
}
$stripDriveLetter = function ($path) {
if (strlen($path) > 2 && ':' === $path[1] && '/' === $path[2] && ctype_alpha($path[0])) {
return array(substr($path, 2), $path[0].$path[1]);
}

return $path;
};
return $path;
};

$endPath = $stripDriveLetter($endPath);
$startPath = $stripDriveLetter($startPath);
$endPath = $stripDriveLetter($endPath);
$startPath = $stripDriveLetter($startPath);

if ($endPath[1] !== $startPath[1]) {
return '';
}

$endPath = $endPath[0];
$startPath = $startPath[0];
}
// Split the paths into arrays
$startPathArr = explode('/', trim($startPath, '/'));
$endPathArr = explode('/', trim($endPath, '/'));
Expand Down Expand Up @@ -419,7 +425,6 @@ public function makePathRelative($endPath, $startPath)

// Repeated "../" for each level need to reach the common path
$traverser = str_repeat('../', $depth);

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

// Construct $endPath from traversing to the common path, then to the remaining $endPath
Expand Down

0 comments on commit 53700b6

Please sign in to comment.