Wraps the PHP `realpath()` function to add support for finding paths to files inside Phar
archives.
Parameters
- string $path An unresolved path to a file inside a Phar archive which may or may not exist.
Returns
string If `$path` is a valid path to a file inside a Phar archive, returns a string in the format `'phar://<path-to-phar>/<path-to-file>'`. Otherwise returns `null`.Source
public static function realPath($path) {
if (($absolutePath = realpath($path)) !== false) {
return $absolutePath;
}
if (!preg_match('%^phar://([^.]+\.phar(?:\.gz)?)(.+)%', $path, $pathComponents)) {
return;
}
list(, $relativePath, $pharPath) = $pathComponents;
$pharPath = implode('/', array_reduce(explode('/', $pharPath), function ($parts, $value) {
if ($value == '..') {
array_pop($parts);
} elseif ($value != '.') {
$parts[] = $value;
}
return $parts;
}));
if (($resolvedPath = realpath($relativePath)) !== false) {
if (file_exists($absolutePath = "phar://{$resolvedPath}{$pharPath}")) {
return $absolutePath;
}
}
}