diff --git a/core/Files/FileUtil.php b/core/Files/FileUtil.php index 9d48b4a2..ca079e11 100644 --- a/core/Files/FileUtil.php +++ b/core/Files/FileUtil.php @@ -236,4 +236,31 @@ abstract class FileUtil { public static function isHiddenFile($fileName) { return (substr($fileName, 0, 1) === '.'); } + + /** + * Shortens a path. + * Opposed to realpath, it follows symbolic links. + * + * @param string $path + * @return string + */ + public static function shortenPath(string $path) { + $root = substr($path, 0, 1) === '/'; + $path = str_replace(array('/', '\\'), DIRECTORY_SEPARATOR, $path); + $parts = array_filter(explode(DIRECTORY_SEPARATOR, $path), 'strlen'); + $absolutes = array(); + foreach ($parts as $part) { + if ('.' === $part) + continue; + + if ('..' === $part) + array_pop($absolutes); + else + array_push($absolutes, $part); + } + $path = implode(DIRECTORY_SEPARATOR, $absolutes); + if ($root) + $path = '/'.$path; + return $path; + } }