function Path::makeRelative
Turns a path into a relative path.
The relative path is created relative to the given base path:
```php echo Path::makeRelative("/symfony/style.css", "/symfony/puli"); // => ../style.css ```
If a relative path is passed and the base path is absolute, the relative path is returned unchanged:
```php Path::makeRelative("style.css", "/symfony/puli/css"); // => style.css ```
If both paths are relative, the relative path is created with the assumption that both paths are relative to the same directory:
```php Path::makeRelative("style.css", "symfony/puli/css"); // => ../../../style.css ```
If both paths are absolute, their root directory must be the same, otherwise an exception is thrown:
```php Path::makeRelative("C:/symfony/style.css", "/symfony/puli"); // InvalidArgumentException ```
If the passed path is absolute, but the base path is not, an exception is thrown as well:
```php Path::makeRelative("/symfony/style.css", "symfony/puli"); // InvalidArgumentException ```
If the base path is not an absolute path, an exception is thrown.
The result is a canonical path.
Throws
InvalidArgumentException if the base path is not absolute or if the given path has a different root than the base path
1 call to Path::makeRelative()
- UnknownPathExcluder::getExcludedPaths in core/
modules/ package_manager/ src/ PathExcluder/ UnknownPathExcluder.php - Returns the paths to exclude from stage operations.
File
-
vendor/
symfony/ filesystem/ Path.php, line 511
Class
- Path
- Contains utility methods for handling path strings.
Namespace
Symfony\Component\FilesystemCode
public static function makeRelative(string $path, string $basePath) : string {
$path = self::canonicalize($path);
$basePath = self::canonicalize($basePath);
[
$root,
$relativePath,
] = self::split($path);
[
$baseRoot,
$relativeBasePath,
] = self::split($basePath);
// If the base path is given as absolute path and the path is already
// relative, consider it to be relative to the given absolute path
// already
if ('' === $root && '' !== $baseRoot) {
// If base path is already in its root
if ('' === $relativeBasePath) {
$relativePath = ltrim($relativePath, './\\');
}
return $relativePath;
}
// If the passed path is absolute, but the base path is not, we
// cannot generate a relative path
if ('' !== $root && '' === $baseRoot) {
throw new InvalidArgumentException(\sprintf('The absolute path "%s" cannot be made relative to the relative path "%s". You should provide an absolute base path instead.', $path, $basePath));
}
// Fail if the roots of the two paths are different
if ($baseRoot && $root !== $baseRoot) {
throw new InvalidArgumentException(\sprintf('The path "%s" cannot be made relative to "%s", because they have different roots ("%s" and "%s").', $path, $basePath, $root, $baseRoot));
}
if ('' === $relativeBasePath) {
return $relativePath;
}
// Build a "../../" prefix with as many "../" parts as necessary
$parts = explode('/', $relativePath);
$baseParts = explode('/', $relativeBasePath);
$dotDotPrefix = '';
// Once we found a non-matching part in the prefix, we need to add
// "../" parts for all remaining parts
$match = true;
foreach ($baseParts as $index => $basePart) {
if ($match && isset($parts[$index]) && $basePart === $parts[$index]) {
unset($parts[$index]);
continue;
}
$match = false;
$dotDotPrefix .= '../';
}
return rtrim($dotDotPrefix . implode('/', $parts), '/');
}