normalize paths before making them relative

This commit is contained in:
Christian Flothmann 2017-03-21 09:40:59 +01:00
parent 80af0838f5
commit d50ffa1de7
2 changed files with 35 additions and 0 deletions

View File

@ -362,6 +362,31 @@ class Filesystem
$startPathArr = explode('/', trim($startPath, '/'));
$endPathArr = explode('/', trim($endPath, '/'));
if ('/' !== $startPath[0]) {
array_shift($startPathArr);
}
if ('/' !== $endPath[0]) {
array_shift($endPathArr);
}
$normalizePathArray = function ($pathSegments) {
$result = array();
foreach ($pathSegments as $segment) {
if ('..' === $segment) {
array_pop($result);
} else {
$result[] = $segment;
}
}
return $result;
};
$startPathArr = $normalizePathArray($startPathArr);
$endPathArr = $normalizePathArray($endPathArr);
// Find for which directory the common path stops
$index = 0;
while (isset($startPathArr[$index]) && isset($endPathArr[$index]) && $startPathArr[$index] === $endPathArr[$index]) {

View File

@ -868,6 +868,16 @@ class FilesystemTest extends FilesystemTestCase
array('/a/aab/bb/', '/b/aab', '../../a/aab/bb/'),
array('/aab/bb', '/aa', '../aab/bb/'),
array('/aab', '/aa', '../aab/'),
array('/aa/bb/cc', '/aa/dd/..', 'bb/cc/'),
array('/aa/../bb/cc', '/aa/dd/..', '../bb/cc/'),
array('/aa/bb/../../cc', '/aa/../dd/..', 'cc/'),
array('/../aa/bb/cc', '/aa/dd/..', 'bb/cc/'),
array('/../../aa/../bb/cc', '/aa/dd/..', '../bb/cc/'),
array('C:/aa/bb/cc', 'C:/aa/dd/..', 'bb/cc/'),
array('c:/aa/../bb/cc', 'c:/aa/dd/..', '../bb/cc/'),
array('C:/aa/bb/../../cc', 'C:/aa/../dd/..', 'cc/'),
array('C:/../aa/bb/cc', 'C:/aa/dd/..', 'bb/cc/'),
array('C:/../../aa/../bb/cc', 'C:/aa/dd/..', '../bb/cc/'),
);
if ('\\' === DIRECTORY_SEPARATOR) {