* $path = Path::join('/my', 'path', 'to/file.txt');
*
* Result:
*
* /my/path/to/file.txt
*
*
* @param string ...
* @return mixed
*/
public static function join()
{
$args = Arrays::filterNotBlank(func_get_args());
$path = preg_replace('~[/\\\]+~', DIRECTORY_SEPARATOR, implode(DIRECTORY_SEPARATOR, $args));
$path = str_replace("\0", '', $path);
return $path;
}
/**
* Returns a path starting in the system temporary directory.
*
* Example:
*
* $path = Path::joinWithTemp('my/file.txt');
*
* Result:
*
* //Unix
* /tmp/my/file.txt
*
*
* @param string ...
* @return mixed
*/
public static function joinWithTemp()
{
$args = array_merge([sys_get_temp_dir()], func_get_args());
return call_user_func_array([Path::class, 'join'], $args);
}
/**
* Returns a normalized path.
* Actions:
*
* $normalized = Path::normalize('dir/../dir2/file.txt');
* $normalized = Path::normalize('/tmp/../dir2/file.txt');
* $normalized = Path::normalize('../file.txt');
* $normalized = Path::normalize('//dir/file.txt');
*
* Result:
*
* dir2/file.txt
* /dir2/file.txt
* ../file.txt
* /dir/file.txt
*
*
* @param string $path
* @return string
*/
public static function normalize($path)
{
$parts = explode('/', trim($path, '/'));
$result = [];
foreach ($parts as $part) {
if ($part == '..' && !empty($result)) {
array_pop($result);
} elseif ($part != '.' && !empty($part)) {
array_push($result, $part);
}
}
$root = $path[0] == '/' ? '/' : '';
return $root . implode('/', $result);
}
}