1 | <?php |
||
11 | class Path |
||
12 | { |
||
13 | /** |
||
14 | * Normalizes the provided file system path. |
||
15 | * |
||
16 | * Normalizing file system paths means that all forward and backward |
||
17 | * slashes in the path will be replaced with the system directory separator |
||
18 | * and multiple directory separators will be condensed into one. |
||
19 | * Additionally, all `.` and `..` directory references will be resolved in |
||
20 | * the returned path. |
||
21 | * |
||
22 | * Note that if the normalized path is not an absolute path, the resulting |
||
23 | * path may begin with `..` directory references if it is not possible to |
||
24 | * resolve them simply by using string handling. You should also note that |
||
25 | * if the resulting path would result in an empty string, this method will |
||
26 | * return `.` instead. |
||
27 | * |
||
28 | * If the `$prependDrive` option is enabled, the normalized path will be |
||
29 | * prepended with the drive name on Windows platforms using the current |
||
30 | * working directory, if the path is an absolute path that does not include |
||
31 | * a drive name. |
||
32 | * |
||
33 | * @param string $path File system path to normalize |
||
34 | * @param bool $prependDrive True to prepend drive name to absolute paths |
||
35 | * @return string The normalizes file system path |
||
36 | */ |
||
37 | 12 | public static function normalize($path, $prependDrive = true) |
|
47 | |||
48 | /** |
||
49 | * Joins the provided file systems paths together and normalizes the result. |
||
50 | * |
||
51 | * The paths can be provided either as multiple arguments to this method |
||
52 | * or as an array. The paths will be joined using the system directory |
||
53 | * separator and the result will be normalized similar to the normalization |
||
54 | * method (the drive letter will not be prepended however). |
||
55 | * |
||
56 | * Note that unless the first path in the list is an absolute path, the |
||
57 | * entire resulting path will be treated as a relative path. |
||
58 | * |
||
59 | * @param string[]|string $paths File system paths to join |
||
60 | * @return string The joined file system paths |
||
61 | */ |
||
62 | 45 | public static function join($paths) |
|
72 | |||
73 | /** |
||
74 | * Builds the final path from the root and path parts. |
||
75 | * @param string $root Root path |
||
76 | * @param string[] $parts The path parts |
||
77 | * @return string The final built path |
||
78 | */ |
||
79 | 39 | private static function buildPath($root, array $parts) |
|
87 | |||
88 | /** |
||
89 | * Merges the paths and returns the individual parts. |
||
90 | * @param string[] $paths Array of paths |
||
91 | * @return string[] Parts in the paths merged into a single array |
||
92 | * @throws \InvalidArgumentException If no paths have been provided |
||
93 | */ |
||
94 | 45 | private static function getParts(array $paths) |
|
102 | |||
103 | /** |
||
104 | * Tells if the path is an absolute path. |
||
105 | * @param string $path The file system path to test |
||
106 | * @return bool True if the path is an absolute path, false if not |
||
107 | */ |
||
108 | 42 | private static function isAbsolute($path) |
|
120 | |||
121 | /** |
||
122 | * Resolves parent directory references and removes redundant entries. |
||
123 | * @param string[] $parts List of parts in the the path |
||
124 | * @param bool $absolute Whether the path is an absolute path or not |
||
125 | * @return string[] Resolved list of parts in the path |
||
126 | */ |
||
127 | 42 | private static function resolve(array $parts, $absolute) |
|
141 | |||
142 | /** |
||
143 | * Tells if the part of the path is valid and not empty. |
||
144 | * @param string $path Part of the path to check for redundancy |
||
145 | * @return bool True if the path is valid and not empty, false if not |
||
146 | * @throws \InvalidArgumentException If the path contains invalid characters |
||
147 | */ |
||
148 | 42 | private static function isValidPath($path) |
|
156 | |||
157 | /** |
||
158 | * Resolves the relative parent directory for the path. |
||
159 | * @param string[] $parts Path parts to modify |
||
160 | * @param bool $absolute True if dealing with absolute path, false if not |
||
161 | */ |
||
162 | 12 | private static function resolveParent(array & $parts, $absolute) |
|
173 | } |
||
174 |