Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitbd952b9

Browse files
bug#36497 [Filesystem] Handle paths on different drives (crishoj)
This PR was squashed before being merged into the 3.4 branch.Discussion----------[Filesystem] Handle paths on different drives| Q | A| ------------- | ---| Branch? | 3.4| Bug fix? | yes| New feature? | no| Deprecations? | no| License | MIT`makePathRelative` strips and ignores the drive letters given Windows paths on different drives, resulting in a relative path which does not resolve to the desired target.This PR makes `makePathRelative` notice paths on different drives, and return the full (absolute) target path in case instead.Commits-------00e727a [Filesystem] Handle paths on different drives
2 parentscf0d086 +00e727a commitbd952b9

File tree

2 files changed

+21
-18
lines changed

2 files changed

+21
-18
lines changed

‎src/Symfony/Component/Filesystem/Filesystem.php‎

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

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

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

476-
foreach ($pathSegmentsas$segment) {
467+
foreach (explode('/',trim($path,'/'))as$segment) {
477468
if ('..' ===$segment && ($absolute ||\count($result))) {
478469
array_pop($result);
479-
}elseif ('.' !==$segment) {
470+
}elseif ('.' !==$segment &&'' !==$segment) {
480471
$result[] =$segment;
481472
}
482473
}
483474

484475
return$result;
485476
};
486477

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

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

‎src/Symfony/Component/Filesystem/Tests/FilesystemTest.php‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1111,10 +1111,14 @@ public function providePathsForMakePathRelative()
11111111
['/../aa/bb/cc','/aa/dd/..','bb/cc/'],
11121112
['/../../aa/../bb/cc','/aa/dd/..','../bb/cc/'],
11131113
['C:/aa/bb/cc','C:/aa/dd/..','bb/cc/'],
1114+
['C:/aa/bb/cc','c:/aa/dd/..','bb/cc/'],
11141115
['c:/aa/../bb/cc','c:/aa/dd/..','../bb/cc/'],
11151116
['C:/aa/bb/../../cc','C:/aa/../dd/..','cc/'],
11161117
['C:/../aa/bb/cc','C:/aa/dd/..','bb/cc/'],
11171118
['C:/../../aa/../bb/cc','C:/aa/dd/..','../bb/cc/'],
1119+
['D:/','C:/aa/../bb/cc','D:/'],
1120+
['D:/aa/bb','C:/aa','D:/aa/bb/'],
1121+
['D:/../../aa/../bb/cc','C:/aa/dd/..','D:/bb/cc/'],
11181122
];
11191123

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

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp