Changeset View
Changeset View
Standalone View
Standalone View
src/filesystem/Filesystem.php
| Show First 20 Lines • Show All 847 Lines • ▼ Show 20 Lines | while (end($parts) !== false) { | ||||
| break; | break; | ||||
| } | } | ||||
| } | } | ||||
| return $path; | return $path; | ||||
| } | } | ||||
| /** | /** | ||||
| * Retrieve the relative location between two absolute paths. | |||||
| * | |||||
| * This function returns the relative path between two given absolute paths. | |||||
| * The implementation was based on a post on | |||||
| * [[http://stackoverflow.com/a/2638272/1369417 | StackOverflow]]. | |||||
| * | |||||
| * @param string The source destination. | |||||
| * @param string The target destination. | |||||
| * @return string The relative path between the source and target | |||||
| * destinations. | |||||
| * @task path | |||||
| */ | |||||
| public static function relativePath($from, $to) { | |||||
| // Some compatibility fixes for Windows paths. | |||||
| $from = is_dir($from) ? rtrim($from, '\/').'/' : $from; | |||||
| $to = is_dir($to) ? rtrim($to, '\/').'/' : $to; | |||||
| $from = str_replace('\\', '/', $from); | |||||
| $to = str_replace('\\', '/', $to); | |||||
| $from = explode('/', $from); | |||||
| $to = explode('/', $to); | |||||
| $rel_path = $to; | |||||
| foreach ($from as $depth => $dir) { | |||||
| // Find first non-matching directory. | |||||
| if ($dir === $to[$depth]) { | |||||
| // Ignore this directory. | |||||
| array_shift($rel_path); | |||||
| } else { | |||||
| // Get number of remaining directories to $from. | |||||
| $remaining = count($from) - $depth; | |||||
| if ($remaining > 1) { | |||||
| // Add traversals up to first matching directory. | |||||
| $pad_length = (count($rel_path) + $remaining - 1) * -1; | |||||
| $rel_path = array_pad($rel_path, $pad_length, '..'); | |||||
| break; | |||||
| } | |||||
| } | |||||
| } | |||||
| // `DIRECTORY_SEPARATOR` is not necessary. See | |||||
| // http://us2.php.net/manual/en/ref.filesystem.php#73954. | |||||
| return implode('/', $rel_path); | |||||
| } | |||||
| /** | |||||
| * Test whether a path is descendant from some root path after resolving all | * Test whether a path is descendant from some root path after resolving all | ||||
| * symlinks and removing artifacts. Both paths must exists for the relation | * symlinks and removing artifacts. Both paths must exists for the relation | ||||
| * to obtain. A path is always a descendant of itself as long as it exists. | * to obtain. A path is always a descendant of itself as long as it exists. | ||||
| * | * | ||||
| * @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. | ||||
| ▲ Show 20 Lines • Show All 234 Lines • Show Last 20 Lines | |||||