public static function getRoot($path)
{
if ('' === $path) {
return '';
}
Assert::string($path, 'The path must be a string. Got: %s');
// Maintain scheme
if (false !== ($pos = strpos($path, '://'))) {
$scheme = substr($path, 0, $pos + 3);
$path = substr($path, $pos + 3);
} else {
$scheme = '';
}
// UNIX root "/" or "\" (Windows style)
if ('/' === $path[0] || '\\' === $path[0]) {
return $scheme . '/';
}
$length = strlen($path);
// Windows root
if ($length > 1 && ctype_alpha($path[0]) && ':' === $path[1]) {
// Special case: "C:"
if (2 === $length) {
return $scheme . $path . '/';
}
// Normal case: "C:/ or "C:\"
if ('/' === $path[2] || '\\' === $path[2]) {
return $scheme . $path[0] . $path[1] . '/';
}
}
return '';
}