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 Nov 28, 2017
1 parent e3e239f commit a69279c
Show file tree
Hide file tree
Showing 2 changed files with 513 additions and 63 deletions.
28 changes: 18 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,11 +425,13 @@ 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
$relativePath = $traverser.('' !== $endPathRemainder ? $endPathRemainder.'/' : '');
if ('/' !== substr($endPath, -1)) {
$relativePath = substr($relativePath, 0, -1);
}

return '' === $relativePath ? './' : $relativePath;
}
Expand Down

0 comments on commit a69279c

Please sign in to comment.