Changeset View
Changeset View
Standalone View
Standalone View
src/filesystem/Filesystem.php
Show First 20 Lines • Show All 771 Lines • ▼ Show 20 Lines | /* -( Directories )-------------------------------------------------------- */ | ||||
* Return all directories between a path and the specified root directory | * Return all directories between a path and the specified root directory | ||||
* (defaulting to "/"). Iterating over them walks from the path to the root. | * (defaulting to "/"). Iterating over them walks from the path to the root. | ||||
* | * | ||||
* @param string Path, absolute or relative to PWD. | * @param string Path, absolute or relative to PWD. | ||||
* @param string The root directory. | * @param string The root directory. | ||||
* @return list<string> List of parent paths, including the provided path. | * @return list<string> List of parent paths, including the provided path. | ||||
* @task directory | * @task directory | ||||
*/ | */ | ||||
public static function walkToRoot($path, $root = '/') { | public static function walkToRoot($path, $root = null) { | ||||
$path = self::resolvePath($path); | $path = self::resolvePath($path); | ||||
$root = self::resolvePath($root); | |||||
if (is_link($path)) { | if (is_link($path)) { | ||||
$path = realpath($path); | $path = realpath($path); | ||||
} | } | ||||
// NOTE: On Windows, paths start like "C:\", so "/" does not contain | |||||
// every other path. We could possibly special case "/" to have the same | |||||
// meaning on Windows that it does on Linux, but just special case the | |||||
// common case for now. See PHI817. | |||||
if ($root !== null) { | |||||
$root = self::resolvePath($root); | |||||
if (is_link($root)) { | if (is_link($root)) { | ||||
$root = realpath($root); | $root = realpath($root); | ||||
} | } | ||||
// NOTE: We don't use `isDescendant()` here because we don't want to reject | // NOTE: We don't use `isDescendant()` here because we don't want to | ||||
// paths which don't exist on disk. | // reject paths which don't exist on disk. | ||||
$root_list = new FileList(array($root)); | $root_list = new FileList(array($root)); | ||||
if (!$root_list->contains($path)) { | if (!$root_list->contains($path)) { | ||||
return array(); | return array(); | ||||
} | } | ||||
} else { | |||||
if (phutil_is_windows()) { | |||||
$root = null; | |||||
} else { | |||||
$root = '/'; | |||||
} | |||||
} | |||||
$walk = array(); | $walk = array(); | ||||
$parts = explode(DIRECTORY_SEPARATOR, $path); | $parts = explode(DIRECTORY_SEPARATOR, $path); | ||||
foreach ($parts as $k => $part) { | foreach ($parts as $k => $part) { | ||||
if (!strlen($part)) { | if (!strlen($part)) { | ||||
unset($parts[$k]); | unset($parts[$k]); | ||||
} | } | ||||
} | } | ||||
▲ Show 20 Lines • Show All 338 Lines • Show Last 20 Lines |