function Path::getLongestCommonBasePath
Returns the longest common base path in canonical form of a set of paths or `null` if the paths are on different Windows partitions.
Dot segments ("." and "..") are removed/collapsed and all slashes turned into forward slashes.
```php $basePath = Path::getLongestCommonBasePath( '/symfony/css/style.css', '/symfony/css/..' ); // => /symfony ```
The root is returned if no common base path can be found:
```php $basePath = Path::getLongestCommonBasePath( '/symfony/css/style.css', '/puli/css/..' ); // => / ```
If the paths are located on different Windows partitions, `null` is returned.
```php $basePath = Path::getLongestCommonBasePath( 'C:/symfony/css/style.css', 'D:/symfony/css/..' ); // => null ```
File
-
vendor/
symfony/ filesystem/ Path.php, line 613
Class
- Path
- Contains utility methods for handling path strings.
Namespace
Symfony\Component\FilesystemCode
public static function getLongestCommonBasePath(string ...$paths) : ?string {
[
$bpRoot,
$basePath,
] = self::split(self::canonicalize(reset($paths)));
for (next($paths); null !== key($paths) && '' !== $basePath; next($paths)) {
[
$root,
$path,
] = self::split(self::canonicalize(current($paths)));
// If we deal with different roots (e.g. C:/ vs. D:/), it's time
// to quit
if ($root !== $bpRoot) {
return null;
}
// Make the base path shorter until it fits into path
while (true) {
if ('.' === $basePath) {
// No more base paths
$basePath = '';
// next path
continue 2;
}
// Prevent false positives for common prefixes
// see isBasePath()
if (str_starts_with($path . '/', $basePath . '/')) {
// next path
continue 2;
}
$basePath = \dirname($basePath);
}
}
return $bpRoot . $basePath;
}