-
Notifications
You must be signed in to change notification settings - Fork 3
/
Path.php
295 lines (262 loc) · 7.63 KB
/
Path.php
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
<?php
/**
* @package ryanve/slash
*/
namespace slash;
use \RecursiveIteratorIterator as RII;
use \RecursiveDirectoryIterator as RDI;
class Path extends Slash {
use traits\Mixin;
protected static $mixin = [# aliases
'exists' => [__CLASS__, 'isPath'],
'listPaths' => [__CLASS__, 'paths'],
'listFiles' => [__CLASS__, 'files'],
'listDirs' => [__CLASS__, 'dirs']
];
/**
* @return string
*/
public static function root($pathRelative) {
return $_SERVER['DOCUMENT_ROOT'] . static::lslash($pathRelative);
}
/**
* @return string
*/
public static function dir($pathRelative) {
return __DIR__ . static::lslash($pathRelative);
}
/**
* @return string|bool
*/
public static function ext($path, $add = null) {
# get basename, remove any query params, get chars starting at last dot:
if (null === $add) return strrchr(strtok(basename($path), '?'), '.');
$add = '.' . ltrim($add, '.'); # add to path if missing
return basename($path) === basename($path, $add) ? rtrim($path, '.') . $add : $path;
}
/**
* @return string
*/
public static function filename($path) {
return pathinfo($path, PATHINFO_FILENAME);
}
/**
* @return int
*/
public static function inc($path) {
$count = 0;
foreach ((array) $path as $n) static::isFile($n) and ++$count and include $n;
return $count;
}
/**
* @return bool
*/
public static function isPath($item) {
return is_scalar($item) && file_exists($item);
}
/**
* @return bool
*/
public static function isDir($item) {
return is_scalar($item) && is_dir($item);
}
/**
* @return bool
*/
public static function isFile($item) {
return is_scalar($item) && is_file($item);
}
/**
* @return bool
*/
public static function isDot($item) {
return in_array(basename($item), ['.', '..']);
}
/**
* @return bool
*/
public static function isAbs($item) {
return is_scalar($item) && realpath($item) === $item;
}
/**
* @return string|array|bool
*/
public static function toAbs($path) {
if (is_array($path)) return array_map([__CLASS__, __FUNCTION__], $path); # recurse
if (is_string($path) || is_numeric($path)) return realpath($path); # resolve relative path
return false;
}
/**
* @param string $path
* @param string $scheme defaults to none (protocol-relative)
* @return string
*/
public static function toUri($path = '', $scheme = null) {
$scheme = is_string($scheme) ? rtrim($scheme, ':') : null;
$uri = ($scheme ? $scheme . '://' : '//') . $_SERVER['SERVER_NAME'];
return $uri . static::lslash(str_replace($_SERVER['DOCUMENT_ROOT'], '', $path));
}
/**
* @param string $path
* @param string $scheme defaults to server type (https or http)
* @return string
*/
public static function toUrl($path = '', $scheme = null) {
return static::toUri($path, is_string($scheme) ? $scheme : (static::isHttps() ? 'https' : 'http'));
}
/**
* @return bool
*/
public static function isHttps() {
return !empty($_SERVER['HTTPS']) and 'off' !== strtolower($_SERVER['HTTPS'])
or !empty($_SERVER['SERVER_PORT']) and 443 == $_SERVER['SERVER_PORT'];
}
/**
* @param string $path
* @return array
*/
public static function scan($path = '.') {
$list = [];
foreach (scandir($path) as $n)
static::isDot($n) or $list[] = static::join($path, $n);
return $list; # shallow
}
/**
* @param string $path
* @return array
*/
public static function paths($path = '.') {
$list = [];
foreach (new RII(new RDI($path), RII::SELF_FIRST) as $splfileinfo)
static::isDot($path = $splfileinfo->getPathname()) or $list[] = $path;
return $list; # deep
}
/**
* @param string|array $path
* @return array
*/
public static function files($path = '.') {
if (static::isFile($path)) return [$path];
return array_filter(is_array($path) ? $path : static::paths($path), 'is_file');
}
/**
* @param string|array $path
* @return array
*/
public static function dirs($path = '.') {
return array_filter(is_array($path) ? $path : static::paths($path), 'is_dir');
}
/**
* @param string|array $path
* @return array associative array containing the dir structure
*/
public static function tree($path = '.') {
$list = [];
foreach (is_array($path) ? $path : static::scan($path) as $n)
is_dir($n) ? $list[$n] = static::tree($n) : $list[] = $n;
return $list;
}
/**
* @param string $path dir or file
* @return int modified time of file or most recent file in dir
*/
public static function mtime($path = '.') {
return max(array_map('filemtime', static::files($path)));
}
/**
* @param string $path dir or file
* @return int changed time of file or most recent file in dir
*/
public static function ctime($path = '.') {
return max(array_map('filectime', static::files($path)));
}
/**
* @param string $path dir or file
* @param string $format date string for use with date()
* @return int accessed time of file or most recent file in dir
*/
public static function atime($path = '.') {
return max(array_map('fileatime', static::files($path)));
}
/**
* @param string $path dir or file
* @return int
*/
public static function size($path = '.') {
return array_sum(array_map('filesize', static::files($path)));
}
/**
* @return array
*/
public static function affix(array $list, $prefix = '', $suffix = '') {
foreach ($list as &$n) $n = $prefix . $n . $suffix;
return $list;
}
/**
* @param string $path
* @param string $infix text to insert before file extension
* @return string
*/
public static function infix($path, $infix) {
return preg_replace('#(\.\w+)$#', "$infix$1", $path);
}
/**
* @return int
*/
public static function depth($path) {
return substr_count($path, '/') + substr_count($path, '\\');
}
/**
* @return array
*/
public static function tier(array $list) {
$levels = array_map(static::method('depth'), $list);
$groups = array_pad([], max($levels), []); # ordered and non-sparse
foreach ($list as $k => $v) $groups[$levels[$k]][] = $v;
return $groups;
}
/**
* @return array
*/
public static function sort(array $list) {
return call_user_func_array('array_merge', static::tier($list));
}
/**
* Get the first existent path from the supplied args.
* @param array|string $needles
*/
public static function locate($needles) {
return static::find(is_array($needles) ? $needles : func_get_args(), static::method('exists'));
}
/**
* @param string|array|object $haystack
* @param string $needle
* @return bool
*/
public static function contains($haystack, $needle) {
if (is_scalar($haystack)) return false !== strpos($haystack, $needle);
foreach ((array) $haystack as $v) if (self::contains($v, $needle)) return true;
return false;
}
/**
* @param string|array|object $path
* @param string|array $needles
* @return array
*/
public static function search($path, $needles) {
$result = [];
is_array($needles) or $needles = array_slice(func_get_args(), 1);
foreach (is_scalar($path) ? static::paths($path) : $path as $v)
foreach ($needles as $needle)
static::contains($v, $needle) and $result[] = $v;
return $result;
}
/**
* @param string|array|object $path
* @param callable $fn
*/
public static function find($path = '.', callable $fn) {
$trav = is_scalar($path) ? static::paths($path) : $path;
foreach ($trav as $k => $v) if (call_user_func($fn, $v, $k, $trav)) return $v;
}
}