Changeset View
Changeset View
Standalone View
Standalone View
src/filesystem/Filesystem.php
| Show All 9 Lines | |||||
| * FilesystemException. | * FilesystemException. | ||||
| * | * | ||||
| * @task directory Directories | * @task directory Directories | ||||
| * @task file Files | * @task file Files | ||||
| * @task path Paths | * @task path Paths | ||||
| * @task exec Executables | * @task exec Executables | ||||
| * @task assert Assertions | * @task assert Assertions | ||||
| */ | */ | ||||
| final class Filesystem { | final class Filesystem extends Phobject { | ||||
| /* -( Files )-------------------------------------------------------------- */ | /* -( Files )-------------------------------------------------------------- */ | ||||
| /** | /** | ||||
| * Read a file in a manner similar to file_get_contents(), but throw detailed | * Read a file in a manner similar to file_get_contents(), but throw detailed | ||||
| * exceptions on failure. | * exceptions on failure. | ||||
| ▲ Show 20 Lines • Show All 699 Lines • ▼ Show 20 Lines | foreach ($list as $k => $v) { | ||||
| } | } | ||||
| } | } | ||||
| return array_values($list); | return array_values($list); | ||||
| } | } | ||||
| /** | /** | ||||
| * Return all directories between a path and "/". Iterating over them walks | * Return all directories between a path and the specified root directory | ||||
| * 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. | ||||
| * @return list List of parent paths, including the provided path. | * @param string The root directory. | ||||
| * @return list<string> List of parent paths, including the provided path. | |||||
| * @task directory | * @task directory | ||||
| */ | */ | ||||
| public static function walkToRoot($path) { | public static function walkToRoot($path, $root = '/') { | ||||
| $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); | ||||
| } | } | ||||
| if (is_link($root)) { | |||||
| $root = realpath($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]); | ||||
| } | } | ||||
| } | } | ||||
| do { | |||||
| if (!self::isDescendant($path, $root)) { | |||||
| return array(); | |||||
| } | |||||
| while ($parts) { | |||||
| if (phutil_is_windows()) { | if (phutil_is_windows()) { | ||||
| $walk[] = implode(DIRECTORY_SEPARATOR, $parts); | $next = implode(DIRECTORY_SEPARATOR, $parts); | ||||
| } else { | } else { | ||||
| $walk[] = DIRECTORY_SEPARATOR.implode(DIRECTORY_SEPARATOR, $parts); | $next = DIRECTORY_SEPARATOR.implode(DIRECTORY_SEPARATOR, $parts); | ||||
| } | } | ||||
| if (empty($parts)) { | |||||
| $walk[] = $next; | |||||
| if ($next == $root) { | |||||
| break; | break; | ||||
| } | } | ||||
| array_pop($parts); | array_pop($parts); | ||||
| } while (true); | } | ||||
| return $walk; | return $walk; | ||||
| } | } | ||||
| /* -( Paths )-------------------------------------------------------------- */ | /* -( Paths )-------------------------------------------------------------- */ | ||||
| ▲ Show 20 Lines • Show All 72 Lines • ▼ Show 20 Lines | /* -( Paths )-------------------------------------------------------------- */ | ||||
| * | * | ||||
| * @param string Child path, absolute or relative to PWD. | * @param string Child path, absolute or relative to PWD. | ||||
| * @param string Root path, absolute or relative to PWD. | * @param string Root path, absolute or relative to PWD. | ||||
| * @return bool True if resolved child path is in fact a descendant of | * @return bool True if resolved child path is in fact a descendant of | ||||
| * resolved root path and both exist. | * resolved root path and both exist. | ||||
| * @task path | * @task path | ||||
| */ | */ | ||||
| public static function isDescendant($path, $root) { | public static function isDescendant($path, $root) { | ||||
| try { | try { | ||||
| self::assertExists($path); | self::assertExists($path); | ||||
| self::assertExists($root); | self::assertExists($root); | ||||
| } catch (FilesystemException $e) { | } catch (FilesystemException $e) { | ||||
| return false; | return false; | ||||
| } | } | ||||
| $fs = new FileList(array($root)); | $fs = new FileList(array($root)); | ||||
| return $fs->contains($path); | return $fs->contains($path); | ||||
| ▲ Show 20 Lines • Show All 223 Lines • Show Last 20 Lines | |||||