Skip to content

Commit 835ca5e

Browse files
Merge branch '3.4' into 4.4
* 3.4: [Filesystem] Handle paths on different drives [WebProfiler] Do not add src-elem CSP directives if they do not exist [Yaml] fix parse error when unindented collections contain a comment [3.4][Inflector] Improve testSingularize() argument name [PhpUnitBridge] fix PHP 5.3 compat again Skip validation when email is an empty object fix sr_Latn translation [Validator] fix lazy property usage. Fix annotation [PhpUnitBridge] fix compat with PHP 5.3 [DX] Show the ParseException message in YAML file loaders
2 parents a3ebf3b + 4934b0f commit 835ca5e

File tree

2 files changed

+21
-18
lines changed

2 files changed

+21
-18
lines changed

Filesystem.php

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -454,37 +454,36 @@ public function makePathRelative($endPath, $startPath)
454454
$startPath = str_replace('\\', '/', $startPath);
455455
}
456456

457-
$stripDriveLetter = function ($path) {
458-
if (\strlen($path) > 2 && ':' === $path[1] && '/' === $path[2] && ctype_alpha($path[0])) {
459-
return substr($path, 2);
460-
}
461-
462-
return $path;
457+
$splitDriveLetter = function ($path) {
458+
return (\strlen($path) > 2 && ':' === $path[1] && '/' === $path[2] && ctype_alpha($path[0]))
459+
? [substr($path, 2), strtoupper($path[0])]
460+
: [$path, null];
463461
};
464462

465-
$endPath = $stripDriveLetter($endPath);
466-
$startPath = $stripDriveLetter($startPath);
467-
468-
// Split the paths into arrays
469-
$startPathArr = explode('/', trim($startPath, '/'));
470-
$endPathArr = explode('/', trim($endPath, '/'));
471-
472-
$normalizePathArray = function ($pathSegments) {
463+
$splitPath = function ($path) {
473464
$result = [];
474465

475-
foreach ($pathSegments as $segment) {
466+
foreach (explode('/', trim($path, '/')) as $segment) {
476467
if ('..' === $segment) {
477468
array_pop($result);
478-
} elseif ('.' !== $segment) {
469+
} elseif ('.' !== $segment && '' !== $segment) {
479470
$result[] = $segment;
480471
}
481472
}
482473

483474
return $result;
484475
};
485476

486-
$startPathArr = $normalizePathArray($startPathArr);
487-
$endPathArr = $normalizePathArray($endPathArr);
477+
list($endPath, $endDriveLetter) = $splitDriveLetter($endPath);
478+
list($startPath, $startDriveLetter) = $splitDriveLetter($startPath);
479+
480+
$startPathArr = $splitPath($startPath);
481+
$endPathArr = $splitPath($endPath);
482+
483+
if ($endDriveLetter && $startDriveLetter && $endDriveLetter != $startDriveLetter) {
484+
// End path is on another drive, so no relative path exists
485+
return $endDriveLetter.':/'.($endPathArr ? implode('/', $endPathArr).'/' : '');
486+
}
488487

489488
// Find for which directory the common path stops
490489
$index = 0;

Tests/FilesystemTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1107,10 +1107,14 @@ public function providePathsForMakePathRelative()
11071107
['/../aa/bb/cc', '/aa/dd/..', 'bb/cc/'],
11081108
['/../../aa/../bb/cc', '/aa/dd/..', '../bb/cc/'],
11091109
['C:/aa/bb/cc', 'C:/aa/dd/..', 'bb/cc/'],
1110+
['C:/aa/bb/cc', 'c:/aa/dd/..', 'bb/cc/'],
11101111
['c:/aa/../bb/cc', 'c:/aa/dd/..', '../bb/cc/'],
11111112
['C:/aa/bb/../../cc', 'C:/aa/../dd/..', 'cc/'],
11121113
['C:/../aa/bb/cc', 'C:/aa/dd/..', 'bb/cc/'],
11131114
['C:/../../aa/../bb/cc', 'C:/aa/dd/..', '../bb/cc/'],
1115+
['D:/', 'C:/aa/../bb/cc', 'D:/'],
1116+
['D:/aa/bb', 'C:/aa', 'D:/aa/bb/'],
1117+
['D:/../../aa/../bb/cc', 'C:/aa/dd/..', 'D:/bb/cc/'],
11141118
];
11151119

11161120
if ('\\' === \DIRECTORY_SEPARATOR) {

0 commit comments

Comments
 (0)