Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Filesystem] Fix makePathRelative when end path is a file #38684

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/Symfony/Component/Filesystem/Filesystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,8 @@ public function makePathRelative(string $endPath, string $startPath)
$startPath = str_replace('\\', '/', $startPath);
}

$endPathTypeIndicator = rtrim($endPath, '/') !== $endPath ? '/' : '';

$splitDriveLetter = function ($path) {
return (\strlen($path) > 2 && ':' === $path[1] && '/' === $path[2] && ctype_alpha($path[0]))
? [substr($path, 2), strtoupper($path[0])]
Expand Down Expand Up @@ -462,7 +464,7 @@ public function makePathRelative(string $endPath, string $startPath)

if ($endDriveLetter && $startDriveLetter && $endDriveLetter != $startDriveLetter) {
// End path is on another drive, so no relative path exists
return $endDriveLetter.':/'.($endPathArr ? implode('/', $endPathArr).'/' : '');
return $endDriveLetter.':/'.($endPathArr ? implode('/', $endPathArr).$endPathTypeIndicator : '');
}

// Find for which directory the common path stops
Expand All @@ -484,7 +486,7 @@ public function makePathRelative(string $endPath, string $startPath)
$endPathRemainder = implode('/', \array_slice($endPathArr, $index));

// Construct $endPath from traversing to the common path, then to the remaining $endPath
$relativePath = $traverser.('' !== $endPathRemainder ? $endPathRemainder.'/' : '');
$relativePath = $traverser.('' !== $endPathRemainder ? $endPathRemainder.$endPathTypeIndicator : '');

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