-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPath.hh
336 lines (280 loc) · 8.95 KB
/
Path.hh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
<?hh // strict
/**
* @copyright 2010-2015, The Titon Project
* @license http://opensource.org/licenses/bsd-license.php
* @link http://titon.io
*/
namespace Titon\Utility;
use InvalidArgumentException;
/**
* Provides convenience functions for inflecting notation paths, namespace paths and file system paths.
*
* @package Titon\Utility
*/
class Path {
/**
* Directory separator.
*/
const string SEPARATOR = DIRECTORY_SEPARATOR;
/**
* Include path separator.
*/
const string DELIMITER = PATH_SEPARATOR;
/**
* Namespace package separator.
*/
const string PACKAGE = '\\';
/**
* Parse the file path to remove absolute paths and replace with a constant name.
*
* @param string $file
* @param Map<string, string> $paths
* @return string
*/
public static function alias(string $file, Map<string, string> $paths = Map {}): string {
if (!$file) {
return '[internal]';
}
$file = static::ds($file);
// Inherit titon constants
foreach (Vector {'vendor', 'app', 'modules', 'resources', 'temp', 'views', 'web'} as $type) {
$constant = strtoupper($type) . '_DIR';
if (!$paths->contains($type) && defined($constant)) {
$paths[$type] = constant($constant);
}
}
// Define source locations
foreach (Vector {'src', 'lib', 'tests'} as $source) {
if (!$paths->contains($source) && strpos($file, $source) !== false) {
$parts = explode($source, $file);
$paths[$source] = $parts[0] . $source;
}
}
foreach ($paths as $key => $path) {
$path = trim(static::ds($path), '/') . '/';
if (mb_strpos($file, $path) !== false) {
$file = trim(str_replace($path, '[' . $key . ']', $file), '/');
break;
}
}
return $file;
}
/**
* Strips the namespace to return the base class name.
*
* @param string $class
* @param string $separator
* @return string
*/
public static function className(string $class, string $separator = self::PACKAGE): string {
return trim(strrchr($class, $separator), $separator);
}
/**
* Converts OS directory separators to the standard forward slash.
*
* @param string $path
* @param bool $endSlash
* @return string
*/
public static function ds(string $path, bool $endSlash = false): string {
$path = str_replace('\\', '/', $path);
if ($endSlash && substr($path, -1) !== '/') {
$path .= '/';
}
return $path;
}
/**
* Return the extension from a file path.
*
* @param string $path
* @return string
*/
public static function ext(string $path): string {
return strtolower(pathinfo($path, PATHINFO_EXTENSION));
}
/**
* Define additional include paths for PHP to detect within.
*
* @param Vector<string> $paths
* @return string
*/
public static function includePath(Vector<string> $paths): string {
$current = Vector {get_include_path()};
$current->addAll($paths);
$path = implode(self::DELIMITER, $current);
set_include_path($path);
return $path;
}
/**
* Verify a path is absolute by checking the first path part.
*
* @param string $path
* @return bool
*/
public static function isAbsolute(string $path): bool {
return ($path[0] === '/' || $path[0] === '\\' || preg_match('/^[a-zA-Z0-9]+:/', $path));
}
/**
* Verify a path is relative.
*
* @param string $path
* @return bool
*/
public static function isRelative(string $path): bool {
return !static::isAbsolute($path);
}
/**
* Join all path parts and return a normalized path.
*
* @param Vector<string> $paths
* @param bool $above - Go above the root path if .. is used
* @param bool $join - Join all the path parts into a string
* @return string|Vector<string>
*/
public static function join(Vector<string> $paths, bool $above = true, bool $join = true): mixed {
$clean = Vector {};
$parts = Vector {};
$up = 0;
// First pass expands sub-paths
foreach ($paths as $path) {
$path = trim(static::ds($path), '/');
if (strpos($path, '/') !== false) {
$clean->addAll(new Vector(explode('/', $path)));
} else {
$clean[] = $path;
}
}
// Second pass flattens dot paths
$clean->reverse();
foreach ($clean as $path) {
if ($path === '.' || !$path) {
continue;
} else if ($path === '..') {
$up++;
} else if ($up) {
$up--;
} else {
$parts[] = $path;
}
}
// Append double dots above root
if ($above) {
while ($up) {
$parts[] = '..';
$up--;
}
}
$parts->reverse();
if ($join) {
return implode('/', $parts);
}
return $parts;
}
/**
* Normalize a string by resolving "." and "..". When multiple slashes are found, they're replaced by a single one;
* when the path contains a trailing slash, it is preserved. On Windows backslashes are used.
*
* @param string $path
* @return string
*/
public static function normalize(string $path): string {
return realpath($path);
}
/**
* Returns a namespace with only the base package and not the class name.
*
* @param string $class
* @param string $separator
* @return string
*/
public static function packageName(string $class, string $separator = self::PACKAGE): string {
return trim(mb_substr($class, 0, mb_strrpos($class, $separator)), $separator);
}
/**
* Determine the relative path between two absolute paths.
*
* @param string $from
* @param string $to
* @return string
* @throws \InvalidArgumentException
*/
public static function relativeTo(string $from, string $to): string {
if (static::isRelative($from) || static::isRelative($to)) {
throw new InvalidArgumentException('Cannot determine relative path without two absolute paths');
}
$from = explode('/', static::ds($from, true));
$to = explode('/', static::ds($to, true));
$relative = $to;
foreach ($from as $depth => $dir) {
// Find first non-matching dir and ignore it
if ($dir === $to[$depth]) {
array_shift($relative);
// Get count of remaining dirs to $from
} else {
$remaining = count($from) - $depth;
// Add traversals up to first matching dir
if ($remaining > 1) {
$padLength = (count($relative) + $remaining - 1) * -1;
$relative = array_pad($relative, $padLength, '..');
break;
} else {
$relative[0] = './' . $relative[0];
}
}
}
if (!$relative) {
return './';
}
return implode('/', $relative);
}
/**
* Strip off the extension if it exists.
*
* @param string $path
* @return string
*/
public static function stripExt(string $path): string {
if (mb_strpos($path, '.') !== false) {
$path = mb_substr($path, 0, mb_strrpos($path, '.'));
}
return $path;
}
/**
* Converts a path to a namespace package.
*
* @param string $path
* @return string
*/
public static function toNamespace(string $path): string {
$path = static::ds(static::stripExt($path));
// Attempt to split path at source folder
foreach (Vector {'lib', 'src'} as $folder) {
if (mb_strpos($path, $folder . '/') !== false) {
$paths = explode($folder . '/', $path);
$path = $paths[1];
}
}
return trim(str_replace('/', self::PACKAGE, $path), self::PACKAGE);
}
/**
* Converts a namespace to a relative or absolute file system path.
*
* @param string $path
* @param string $ext
* @param string $root
* @return string
*/
public static function toPath(string $path, string $ext = 'php', string $root = ''): string {
$path = static::ds($path);
$dirs = explode('/', $path);
$file = array_pop($dirs);
$path = implode('/', $dirs) . '/' . str_replace('_', '/', $file);
if ($ext && mb_substr($path, -mb_strlen($ext)) !== $ext) {
$path .= '.' . $ext;
}
if ($root) {
$path = $root . $path;
}
return $path;
}
}