|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types = 1); |
|
3
|
|
|
|
|
4
|
|
|
namespace Yiisoft\Files; |
|
5
|
|
|
|
|
6
|
|
|
use Yiisoft\Strings\StringHelper; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* FileHelper provides useful methods to manage files and directories |
|
10
|
|
|
*/ |
|
11
|
|
|
class FileHelper |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* @var int PATTERN_NO_DIR |
|
15
|
|
|
*/ |
|
16
|
|
|
private const PATTERN_NO_DIR = 1; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @var int PATTERN_ENDS_WITH |
|
20
|
|
|
*/ |
|
21
|
|
|
private const PATTERN_ENDS_WITH = 4; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @var int PATTERN_MUST_BE_DIR |
|
25
|
|
|
*/ |
|
26
|
|
|
private const PATTERN_MUST_BE_DIR = 8; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @var int PATTERN_NEGATIVE |
|
30
|
|
|
*/ |
|
31
|
|
|
private const PATTERN_NEGATIVE = 16; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @var int PATTERN_CASE_INSENSITIVE |
|
35
|
|
|
*/ |
|
36
|
|
|
private const PATTERN_CASE_INSENSITIVE = 32; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Creates a new directory. |
|
40
|
|
|
* |
|
41
|
|
|
* This method is similar to the PHP `mkdir()` function except that it uses `chmod()` to set the permission of the |
|
42
|
|
|
* created directory in order to avoid the impact of the `umask` setting. |
|
43
|
|
|
* |
|
44
|
|
|
* @param string $path path of the directory to be created. |
|
45
|
|
|
* @param int $mode the permission to be set for the created directory. |
|
46
|
|
|
* |
|
47
|
|
|
* @return bool whether the directory is created successfully. |
|
48
|
|
|
*/ |
|
49
|
17 |
|
public static function createDirectory(string $path, int $mode = 0775): bool |
|
50
|
|
|
{ |
|
51
|
17 |
|
$path = static::normalizePath($path); |
|
52
|
|
|
|
|
53
|
|
|
try { |
|
54
|
17 |
|
if (!mkdir($path, $mode, true) && !is_dir($path)) { |
|
55
|
17 |
|
return false; |
|
56
|
|
|
} |
|
57
|
1 |
|
} catch (\Exception $e) { |
|
58
|
1 |
|
if (!is_dir($path)) { |
|
59
|
|
|
throw new \RuntimeException( |
|
60
|
|
|
"Failed to create directory \"$path\": " . $e->getMessage(), |
|
61
|
|
|
$e->getCode(), |
|
62
|
|
|
$e |
|
63
|
|
|
); |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
17 |
|
return static::chmod($path, $mode); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Set permissions directory. |
|
72
|
|
|
* |
|
73
|
|
|
* @param string $path |
|
74
|
|
|
* @param integer $mode |
|
75
|
|
|
* |
|
76
|
|
|
* @throws \RuntimeException |
|
77
|
|
|
* |
|
78
|
|
|
* @return boolean|null |
|
79
|
|
|
*/ |
|
80
|
17 |
|
private static function chmod(string $path, int $mode): ?bool |
|
81
|
|
|
{ |
|
82
|
|
|
try { |
|
83
|
17 |
|
return chmod($path, $mode); |
|
84
|
|
|
} catch (\Exception $e) { |
|
85
|
|
|
throw new \RuntimeException( |
|
86
|
|
|
"Failed to change permissions for directory \"$path\": " . $e->getMessage(), |
|
87
|
|
|
$e->getCode(), |
|
88
|
|
|
$e |
|
89
|
|
|
); |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Normalizes a file/directory path. |
|
95
|
|
|
* |
|
96
|
|
|
* The normalization does the following work: |
|
97
|
|
|
* |
|
98
|
|
|
* - Convert all directory separators into `/` (e.g. "\a/b\c" becomes "/a/b/c") |
|
99
|
|
|
* - Remove trailing directory separators (e.g. "/a/b/c/" becomes "/a/b/c") |
|
100
|
|
|
* - Turn multiple consecutive slashes into a single one (e.g. "/a///b/c" becomes "/a/b/c") |
|
101
|
|
|
* - Remove ".." and "." based on their meanings (e.g. "/a/./b/../c" becomes "/a/c") |
|
102
|
|
|
* |
|
103
|
|
|
* @param string $path the file/directory path to be normalized |
|
104
|
|
|
* |
|
105
|
|
|
* @return string the normalized file/directory path |
|
106
|
|
|
*/ |
|
107
|
17 |
|
public static function normalizePath(string $path): string |
|
108
|
|
|
{ |
|
109
|
17 |
|
$isWindowsShare = strpos($path, '\\\\') === 0; |
|
110
|
|
|
|
|
111
|
17 |
|
if ($isWindowsShare) { |
|
112
|
1 |
|
$path = substr($path, 2); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
17 |
|
$path = rtrim(strtr($path, '/\\', '//'), '/'); |
|
116
|
|
|
|
|
117
|
17 |
|
if (strpos('/' . $path, '/.') === false && strpos($path, '//') === false) { |
|
118
|
17 |
|
return $isWindowsShare ? "\\\\$path" : $path; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
1 |
|
$parts = []; |
|
122
|
|
|
|
|
123
|
1 |
|
foreach (explode('/', $path) as $part) { |
|
124
|
1 |
|
if ($part === '..' && !empty($parts) && end($parts) !== '..') { |
|
125
|
1 |
|
array_pop($parts); |
|
126
|
1 |
|
} elseif ($part !== '.' && ($part !== '' || empty($parts))) { |
|
127
|
1 |
|
$parts[] = $part; |
|
128
|
|
|
} |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
1 |
|
$path = implode('/', $parts); |
|
132
|
|
|
|
|
133
|
1 |
|
if ($isWindowsShare) { |
|
134
|
1 |
|
$path = '\\\\' . $path; |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
1 |
|
return $path === '' ? '.' : $path; |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* Removes a directory (and all its content) recursively. |
|
142
|
|
|
* |
|
143
|
|
|
* @param string $directory the directory to be deleted recursively. |
|
144
|
|
|
* @param array $options options for directory remove. Valid options are: |
|
145
|
|
|
* |
|
146
|
|
|
* - traverseSymlinks: boolean, whether symlinks to the directories should be traversed too. |
|
147
|
|
|
* Defaults to `false`, meaning the content of the symlinked directory would not be deleted. |
|
148
|
|
|
* Only symlink would be removed in that default case. |
|
149
|
|
|
* |
|
150
|
|
|
* @return void |
|
151
|
|
|
*/ |
|
152
|
17 |
|
public static function removeDirectory(string $directory, array $options = []): void |
|
153
|
|
|
{ |
|
154
|
17 |
|
if (!empty($options['traverseSymlinks']) || !is_link($directory)) { |
|
155
|
17 |
|
if (!($handle = @opendir($directory))) { |
|
156
|
1 |
|
return; |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
17 |
|
while (($file = readdir($handle)) !== false) { |
|
160
|
17 |
|
if ($file === '.' || $file === '..') { |
|
161
|
17 |
|
continue; |
|
162
|
|
|
} |
|
163
|
16 |
|
$path = $directory . '/' . $file; |
|
164
|
16 |
|
if (is_dir($path)) { |
|
165
|
16 |
|
self::removeDirectory($path, $options); |
|
166
|
|
|
} else { |
|
167
|
10 |
|
self::unlink($path); |
|
168
|
|
|
} |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
17 |
|
closedir($handle); |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
17 |
|
if (is_link($directory)) { |
|
175
|
2 |
|
self::unlink($directory); |
|
176
|
|
|
} else { |
|
177
|
17 |
|
rmdir($directory); |
|
178
|
|
|
} |
|
179
|
17 |
|
} |
|
180
|
|
|
|
|
181
|
|
|
/** |
|
182
|
|
|
* Removes a file or symlink in a cross-platform way. |
|
183
|
|
|
* |
|
184
|
|
|
* @param string $path |
|
185
|
|
|
* |
|
186
|
|
|
* @return bool |
|
187
|
|
|
*/ |
|
188
|
10 |
|
public static function unlink(string $path): bool |
|
189
|
|
|
{ |
|
190
|
10 |
|
$isWindows = DIRECTORY_SEPARATOR === '\\'; |
|
191
|
|
|
|
|
192
|
10 |
|
if (!$isWindows) { |
|
193
|
10 |
|
return unlink($path); |
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
|
|
if (is_link($path) && is_dir($path)) { |
|
197
|
|
|
return rmdir($path); |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
|
return unlink($path); |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
|
|
/** |
|
204
|
|
|
* Copies a whole directory as another one. |
|
205
|
|
|
* |
|
206
|
|
|
* The files and sub-directories will also be copied over. |
|
207
|
|
|
* |
|
208
|
|
|
* @param string $source the source directory. |
|
209
|
|
|
* @param string $destination the destination directory. |
|
210
|
|
|
* @param array $options options for directory copy. Valid options are: |
|
211
|
|
|
* |
|
212
|
|
|
* - dirMode: integer, the permission to be set for newly copied directories. Defaults to 0775. |
|
213
|
|
|
* - fileMode: integer, the permission to be set for newly copied files. Defaults to the current environment |
|
214
|
|
|
* setting. |
|
215
|
|
|
* - filter: callback, a PHP callback that is called for each directory or file. |
|
216
|
|
|
* The signature of the callback should be: `function ($path)`, where `$path` refers the full path to be filtered. |
|
217
|
|
|
* The callback can return one of the following values: |
|
218
|
|
|
* |
|
219
|
|
|
* * true: the directory or file will be copied (the "only" and "except" options will be ignored). |
|
220
|
|
|
* * false: the directory or file will NOT be copied (the "only" and "except" options will be ignored). |
|
221
|
|
|
* * null: the "only" and "except" options will determine whether the directory or file should be copied. |
|
222
|
|
|
* |
|
223
|
|
|
* - only: array, list of patterns that the file paths should match if they want to be copied. A path matches a |
|
224
|
|
|
* pattern if it contains the pattern string at its end. For example, '.php' matches all file paths ending with |
|
225
|
|
|
* '.php'. |
|
226
|
|
|
* Note, the '/' characters in a pattern matches both '/' and '\' in the paths. If a file path matches a pattern |
|
227
|
|
|
* in both "only" and "except", it will NOT be copied. |
|
228
|
|
|
* - except: array, list of patterns that the files or directories should match if they want to be excluded from |
|
229
|
|
|
* being copied. A path matches a pattern if it contains the pattern string at its end. Patterns ending with '/' |
|
230
|
|
|
* apply to directory paths only, and patterns not ending with '/' apply to file paths only. For example, '/a/b' |
|
231
|
|
|
* matches all file paths ending with '/a/b'; and '.svn/' matches directory paths ending with '.svn'. Note, the |
|
232
|
|
|
* '/' characters in a pattern matches both '/' and '\' in the paths. |
|
233
|
|
|
* - caseSensitive: boolean, whether patterns specified at "only" or "except" should be case sensitive. Defaults to |
|
234
|
|
|
* true. |
|
235
|
|
|
* - recursive: boolean, whether the files under the subdirectories should also be copied. Defaults to true. |
|
236
|
|
|
* - beforeCopy: callback, a PHP callback that is called before copying each sub-directory or file. If the callback |
|
237
|
|
|
* returns false, the copy operation for the sub-directory or file will be cancelled. The signature of the |
|
238
|
|
|
* callback should be: `function ($from, $to)`, where `$from` is the sub-directory or file to be copied from, |
|
239
|
|
|
* while `$to` is the copy target. |
|
240
|
|
|
* - afterCopy: callback, a PHP callback that is called after each sub-directory or file is successfully copied. |
|
241
|
|
|
* The signature of the callback should be: `function ($from, $to)`, where `$from` is the sub-directory or file |
|
242
|
|
|
* copied from, while `$to` is the copy target. |
|
243
|
|
|
* - copyEmptyDirectories: boolean, whether to copy empty directories. Set this to false to avoid creating |
|
244
|
|
|
* directories that do not contain files. This affects directories that do not contain files initially as well as |
|
245
|
|
|
* directories that do not contain files at the target destination because files have been filtered via `only` or |
|
246
|
|
|
* `except`. Defaults to true. |
|
247
|
|
|
* |
|
248
|
|
|
* @throws \InvalidArgumentException if unable to open directory |
|
249
|
|
|
* @throws \Exception |
|
250
|
|
|
* |
|
251
|
|
|
* @return void |
|
252
|
|
|
*/ |
|
253
|
11 |
|
public static function copyDirectory(string $source, string $destination, array $options = []): void |
|
254
|
|
|
{ |
|
255
|
11 |
|
$source = static::normalizePath($source); |
|
256
|
11 |
|
$destination = static::normalizePath($destination); |
|
257
|
|
|
|
|
258
|
11 |
|
static::isSelfDirectory($source, $destination); |
|
259
|
|
|
|
|
260
|
9 |
|
$destinationExists = static::setDestination($destination, $options); |
|
261
|
|
|
|
|
262
|
9 |
|
$handle = static::isSourceDirectory($source); |
|
263
|
|
|
|
|
264
|
9 |
|
$options = static::setBasePath($source, $options); |
|
265
|
|
|
|
|
266
|
9 |
|
while (($file = readdir($handle)) !== false) { |
|
267
|
9 |
|
if ($file === '.' || $file === '..') { |
|
268
|
9 |
|
continue; |
|
269
|
|
|
} |
|
270
|
|
|
|
|
271
|
7 |
|
$from = $source . '/' . $file; |
|
272
|
7 |
|
$to = $destination . '/' . $file; |
|
273
|
|
|
|
|
274
|
7 |
|
if (static::filterPath($from, $options)) { |
|
275
|
7 |
|
if (is_file($from)) { |
|
276
|
7 |
|
if (!$destinationExists) { |
|
277
|
2 |
|
static::createDirectory($destination, $options['dirMode'] ?? 0775); |
|
278
|
2 |
|
$destinationExists = true; |
|
279
|
|
|
} |
|
280
|
7 |
|
copy($from, $to); |
|
281
|
7 |
|
if (isset($options['fileMode'])) { |
|
282
|
7 |
|
static::chmod($to, $options['fileMode']); |
|
283
|
|
|
} |
|
284
|
6 |
|
} elseif (!isset($options['recursive']) || $options['recursive']) { |
|
285
|
5 |
|
static::copyDirectory($from, $to, $options); |
|
286
|
|
|
} |
|
287
|
|
|
} |
|
288
|
|
|
} |
|
289
|
|
|
|
|
290
|
9 |
|
closedir($handle); |
|
291
|
9 |
|
} |
|
292
|
|
|
|
|
293
|
|
|
/** |
|
294
|
|
|
* Check copy it self directory. |
|
295
|
|
|
* |
|
296
|
|
|
* @param string $source |
|
297
|
|
|
* @param string $destination |
|
298
|
|
|
* |
|
299
|
|
|
* @throws \InvalidArgumentException |
|
300
|
|
|
* |
|
301
|
|
|
* @return boolean |
|
302
|
|
|
*/ |
|
303
|
11 |
|
private static function isSelfDirectory(string $source, string $destination) |
|
304
|
|
|
{ |
|
305
|
11 |
|
if ($source === $destination || strpos($destination, $source . '/') === 0) { |
|
306
|
2 |
|
throw new \InvalidArgumentException('Trying to copy a directory to itself or a subdirectory.'); |
|
307
|
|
|
} |
|
308
|
9 |
|
} |
|
309
|
|
|
|
|
310
|
|
|
/** |
|
311
|
|
|
* Check if exist source directory. |
|
312
|
|
|
* |
|
313
|
|
|
* @param string $source |
|
314
|
|
|
* |
|
315
|
|
|
* @throws \InvalidArgumentException |
|
316
|
|
|
* |
|
317
|
|
|
* @return resource |
|
318
|
|
|
*/ |
|
319
|
9 |
|
private static function isSourceDirectory(string $source) |
|
320
|
|
|
{ |
|
321
|
9 |
|
$handle = @opendir($source); |
|
322
|
|
|
|
|
323
|
9 |
|
if ($handle === false) { |
|
324
|
|
|
throw new \InvalidArgumentException("Unable to open directory: $source"); |
|
325
|
|
|
} |
|
326
|
|
|
|
|
327
|
9 |
|
return $handle; |
|
328
|
|
|
} |
|
329
|
|
|
|
|
330
|
|
|
/** |
|
331
|
|
|
* Set base path directory. |
|
332
|
|
|
* |
|
333
|
|
|
* @param string $source |
|
334
|
|
|
* @param array $options |
|
335
|
|
|
* |
|
336
|
|
|
* @return array |
|
337
|
|
|
*/ |
|
338
|
9 |
|
private static function setBasePath(string $source, array $options): array |
|
339
|
|
|
{ |
|
340
|
9 |
|
if (!isset($options['basePath'])) { |
|
341
|
|
|
// this should be done only once |
|
342
|
9 |
|
$options['basePath'] = realpath($source); |
|
343
|
9 |
|
$options = static::normalizeOptions($options); |
|
344
|
|
|
} |
|
345
|
|
|
|
|
346
|
9 |
|
return $options; |
|
347
|
|
|
} |
|
348
|
|
|
|
|
349
|
|
|
/** |
|
350
|
|
|
* Set destination directory. |
|
351
|
|
|
* |
|
352
|
|
|
* @param string $destination |
|
353
|
|
|
* @param array $options |
|
354
|
|
|
* |
|
355
|
|
|
* @return bool |
|
356
|
|
|
*/ |
|
357
|
9 |
|
private static function setDestination(string $destination, array $options): bool |
|
358
|
|
|
{ |
|
359
|
9 |
|
$destinationExists = is_dir($destination); |
|
360
|
|
|
|
|
361
|
9 |
|
if (!$destinationExists && (!isset($options['copyEmptyDirectories']) || $options['copyEmptyDirectories'])) { |
|
362
|
5 |
|
static::createDirectory($destination, $options['dirMode'] ?? 0775); |
|
363
|
5 |
|
$destinationExists = true; |
|
364
|
|
|
} |
|
365
|
|
|
|
|
366
|
9 |
|
return $destinationExists; |
|
367
|
|
|
} |
|
368
|
|
|
|
|
369
|
|
|
/** |
|
370
|
|
|
* Normalize options. |
|
371
|
|
|
* |
|
372
|
|
|
* @param array $options raw options. |
|
373
|
|
|
* |
|
374
|
|
|
* @return array normalized options. |
|
375
|
|
|
*/ |
|
376
|
9 |
|
protected static function normalizeOptions(array $options): array |
|
377
|
|
|
{ |
|
378
|
9 |
|
$options = static::setCaseSensitive($options); |
|
379
|
9 |
|
$options = static::setExcept($options); |
|
380
|
9 |
|
$options = static::setOnly($options); |
|
381
|
|
|
|
|
382
|
9 |
|
return $options; |
|
383
|
|
|
} |
|
384
|
|
|
|
|
385
|
|
|
/** |
|
386
|
|
|
* Set options case sensitive. |
|
387
|
|
|
* |
|
388
|
|
|
* @param array $options |
|
389
|
|
|
* |
|
390
|
|
|
* @return array |
|
391
|
|
|
*/ |
|
392
|
9 |
|
private static function setCaseSensitive(array $options): array |
|
393
|
|
|
{ |
|
394
|
9 |
|
if (!array_key_exists('caseSensitive', $options)) { |
|
395
|
9 |
|
$options['caseSensitive'] = true; |
|
396
|
|
|
} |
|
397
|
|
|
|
|
398
|
9 |
|
return $options; |
|
399
|
|
|
} |
|
400
|
|
|
|
|
401
|
|
|
/** |
|
402
|
|
|
* Set options except. |
|
403
|
|
|
* |
|
404
|
|
|
* @param array $options |
|
405
|
|
|
* |
|
406
|
|
|
* @return array |
|
407
|
|
|
*/ |
|
408
|
9 |
|
private static function setExcept(array $options): array |
|
409
|
|
|
{ |
|
410
|
9 |
|
if (isset($options['except'])) { |
|
411
|
1 |
|
foreach ($options['except'] as $key => $value) { |
|
412
|
1 |
|
if (\is_string($value)) { |
|
413
|
1 |
|
$options['except'][$key] = self::parseExcludePattern($value, $options['caseSensitive']); |
|
414
|
|
|
} |
|
415
|
|
|
} |
|
416
|
|
|
} |
|
417
|
|
|
|
|
418
|
9 |
|
return $options; |
|
419
|
|
|
} |
|
420
|
|
|
|
|
421
|
|
|
/** |
|
422
|
|
|
* Set options only. |
|
423
|
|
|
* |
|
424
|
|
|
* @param array $options |
|
425
|
|
|
* |
|
426
|
|
|
* @return array |
|
427
|
|
|
*/ |
|
428
|
9 |
|
private static function setOnly(array $options): array |
|
429
|
|
|
{ |
|
430
|
9 |
|
if (isset($options['only'])) { |
|
431
|
2 |
|
foreach ($options['only'] as $key => $value) { |
|
432
|
2 |
|
if (\is_string($value)) { |
|
433
|
2 |
|
$options['only'][$key] = self::parseExcludePattern($value, $options['caseSensitive']); |
|
434
|
|
|
} |
|
435
|
|
|
} |
|
436
|
|
|
} |
|
437
|
|
|
|
|
438
|
9 |
|
return $options; |
|
439
|
|
|
} |
|
440
|
|
|
|
|
441
|
|
|
/** |
|
442
|
|
|
* Checks if the given file path satisfies the filtering options. |
|
443
|
|
|
* |
|
444
|
|
|
* @param string $path the path of the file or directory to be checked. |
|
445
|
|
|
* @param array $options the filtering options. |
|
446
|
|
|
* |
|
447
|
|
|
* @return bool whether the file or directory satisfies the filtering options. |
|
448
|
|
|
*/ |
|
449
|
7 |
|
public static function filterPath(string $path, array $options): bool |
|
450
|
|
|
{ |
|
451
|
7 |
|
$path = str_replace('\\', '/', $path); |
|
452
|
|
|
|
|
453
|
7 |
|
if (!empty($options['except'])) { |
|
454
|
1 |
|
if ((self::lastExcludeMatchingFromList($options['basePath'], $path, $options['except'])) !== null) { |
|
455
|
1 |
|
return false; |
|
456
|
|
|
} |
|
457
|
|
|
} |
|
458
|
|
|
|
|
459
|
7 |
|
if (!empty($options['only']) && !is_dir($path)) { |
|
460
|
|
|
// don't check PATTERN_NEGATIVE since those entries are not prefixed with ! |
|
461
|
2 |
|
return self::lastExcludeMatchingFromList($options['basePath'], $path, $options['only']) !== null; |
|
462
|
|
|
} |
|
463
|
|
|
|
|
464
|
7 |
|
return true; |
|
465
|
|
|
} |
|
466
|
|
|
|
|
467
|
|
|
/** |
|
468
|
|
|
* Searches for the first wildcard character in the pattern. |
|
469
|
|
|
* |
|
470
|
|
|
* @param string $pattern the pattern to search in. |
|
471
|
|
|
* |
|
472
|
|
|
* @return int|bool position of first wildcard character or false if not found. |
|
473
|
|
|
*/ |
|
474
|
2 |
|
private static function firstWildcardInPattern(string $pattern) |
|
475
|
|
|
{ |
|
476
|
2 |
|
$wildcards = ['*', '?', '[', '\\']; |
|
477
|
|
|
$wildcardSearch = function ($carry, $item) use ($pattern) { |
|
478
|
2 |
|
$position = strpos($pattern, $item); |
|
479
|
2 |
|
if ($position === false) { |
|
480
|
2 |
|
return $carry === false ? $position : $carry; |
|
481
|
|
|
} |
|
482
|
2 |
|
return $carry === false ? $position : min($carry, $position); |
|
483
|
2 |
|
}; |
|
484
|
2 |
|
return array_reduce($wildcards, $wildcardSearch, false); |
|
485
|
|
|
} |
|
486
|
|
|
|
|
487
|
|
|
|
|
488
|
|
|
/** |
|
489
|
|
|
* Scan the given exclude list in reverse to see whether pathname should be ignored. |
|
490
|
|
|
* |
|
491
|
|
|
* The first match (i.e. the last on the list), if any, determines the fate. Returns the element which matched, |
|
492
|
|
|
* or null for undecided. |
|
493
|
|
|
* |
|
494
|
|
|
* Based on last_exclude_matching_from_list() from dir.c of git 1.8.5.3 sources. |
|
495
|
|
|
* |
|
496
|
|
|
* @param string $basePath. |
|
497
|
|
|
* @param string $path. |
|
498
|
|
|
* @param array $excludes list of patterns to match $path against. |
|
499
|
|
|
* |
|
500
|
|
|
* @return null|array null or one of $excludes item as an array with keys: 'pattern', 'flags'. |
|
501
|
|
|
* |
|
502
|
|
|
* @throws \InvalidArgumentException if any of the exclude patterns is not a string or an array with keys: pattern, |
|
503
|
|
|
* flags, firstWildcard. |
|
504
|
|
|
*/ |
|
505
|
2 |
|
private static function lastExcludeMatchingFromList(string $basePath, string $path, array $excludes): ?array |
|
506
|
|
|
{ |
|
507
|
2 |
|
foreach (array_reverse($excludes) as $exclude) { |
|
508
|
2 |
|
if (\is_string($exclude)) { |
|
509
|
|
|
$exclude = self::parseExcludePattern($exclude, false); |
|
510
|
|
|
} |
|
511
|
|
|
|
|
512
|
2 |
|
if (!isset($exclude['pattern'], $exclude['flags'], $exclude['firstWildcard'])) { |
|
513
|
|
|
throw new \InvalidArgumentException( |
|
514
|
|
|
'If exclude/include pattern is an array it must contain the pattern, flags and firstWildcard keys.' |
|
515
|
|
|
); |
|
516
|
|
|
} |
|
517
|
|
|
|
|
518
|
2 |
|
if (($exclude['flags'] & self::PATTERN_MUST_BE_DIR) && !is_dir($path)) { |
|
519
|
|
|
continue; |
|
520
|
|
|
} |
|
521
|
|
|
|
|
522
|
2 |
|
if ($exclude['flags'] & self::PATTERN_NO_DIR) { |
|
523
|
|
|
if (self::matchBasename(basename($path), $exclude['pattern'], $exclude['firstWildcard'], $exclude['flags'])) { |
|
524
|
|
|
return $exclude; |
|
525
|
|
|
} |
|
526
|
|
|
continue; |
|
527
|
|
|
} |
|
528
|
|
|
|
|
529
|
2 |
|
if (self::matchPathname($path, $basePath, $exclude['pattern'], $exclude['firstWildcard'], $exclude['flags'])) { |
|
530
|
2 |
|
return $exclude; |
|
531
|
|
|
} |
|
532
|
|
|
} |
|
533
|
|
|
|
|
534
|
2 |
|
return null; |
|
535
|
|
|
} |
|
536
|
|
|
|
|
537
|
|
|
/** |
|
538
|
|
|
* Performs a simple comparison of file or directory names. |
|
539
|
|
|
* |
|
540
|
|
|
* Based on match_basename() from dir.c of git 1.8.5.3 sources. |
|
541
|
|
|
* |
|
542
|
|
|
* @param string $baseName file or directory name to compare with the pattern. |
|
543
|
|
|
* @param string $pattern the pattern that $baseName will be compared against. |
|
544
|
|
|
* @param int|bool $firstWildcard location of first wildcard character in the $pattern. |
|
545
|
|
|
* @param int $flags pattern flags |
|
546
|
|
|
* |
|
547
|
|
|
* @return bool whether the name matches against pattern |
|
548
|
|
|
*/ |
|
549
|
|
|
private static function matchBasename(string $baseName, string $pattern, $firstWildcard, int $flags): bool |
|
550
|
|
|
{ |
|
551
|
|
|
if ($firstWildcard === false) { |
|
552
|
|
|
if ($pattern === $baseName) { |
|
553
|
|
|
return true; |
|
554
|
|
|
} |
|
555
|
|
|
} elseif ($flags & self::PATTERN_ENDS_WITH) { |
|
556
|
|
|
/* "*literal" matching against "fooliteral" */ |
|
557
|
|
|
$n = StringHelper::byteLength($pattern); |
|
558
|
|
|
if (StringHelper::byteSubstr($pattern, 1, $n) === StringHelper::byteSubstr($baseName, -$n, $n)) { |
|
559
|
|
|
return true; |
|
560
|
|
|
} |
|
561
|
|
|
} |
|
562
|
|
|
|
|
563
|
|
|
$matchOptions = []; |
|
564
|
|
|
|
|
565
|
|
|
if ($flags & self::PATTERN_CASE_INSENSITIVE) { |
|
566
|
|
|
$matchOptions['caseSensitive'] = false; |
|
567
|
|
|
} |
|
568
|
|
|
|
|
569
|
|
|
return StringHelper::matchWildcard($pattern, $baseName, $matchOptions); |
|
570
|
|
|
} |
|
571
|
|
|
|
|
572
|
|
|
/** |
|
573
|
|
|
* Compares a path part against a pattern with optional wildcards. |
|
574
|
|
|
* |
|
575
|
|
|
* Based on match_pathname() from dir.c of git 1.8.5.3 sources. |
|
576
|
|
|
* |
|
577
|
|
|
* @param string $path full path to compare |
|
578
|
|
|
* @param string $basePath base of path that will not be compared |
|
579
|
|
|
* @param string $pattern the pattern that path part will be compared against |
|
580
|
|
|
* @param int|bool $firstWildcard location of first wildcard character in the $pattern |
|
581
|
|
|
* @param int $flags pattern flags |
|
582
|
|
|
* |
|
583
|
|
|
* @return bool whether the path part matches against pattern |
|
584
|
|
|
*/ |
|
585
|
2 |
|
private static function matchPathname(string $path, string $basePath, string $pattern, $firstWildcard, int $flags): bool |
|
586
|
|
|
{ |
|
587
|
|
|
// match with FNM_PATHNAME; the pattern has base implicitly in front of it. |
|
588
|
2 |
|
if (strpos($pattern, '/') === 0) { |
|
589
|
|
|
$pattern = StringHelper::byteSubstr($pattern, 1, StringHelper::byteLength($pattern)); |
|
590
|
|
|
if ($firstWildcard !== false && $firstWildcard !== 0) { |
|
591
|
|
|
$firstWildcard--; |
|
592
|
|
|
} |
|
593
|
|
|
} |
|
594
|
|
|
|
|
595
|
2 |
|
$namelen = StringHelper::byteLength($path) - (empty($basePath) ? 0 : StringHelper::byteLength($basePath) + 1); |
|
596
|
2 |
|
$name = StringHelper::byteSubstr($path, -$namelen, $namelen); |
|
597
|
|
|
|
|
598
|
2 |
|
if ($firstWildcard !== 0) { |
|
599
|
2 |
|
if ($firstWildcard === false) { |
|
600
|
1 |
|
$firstWildcard = StringHelper::byteLength($pattern); |
|
601
|
|
|
} |
|
602
|
|
|
|
|
603
|
|
|
// if the non-wildcard part is longer than the remaining pathname, surely it cannot match. |
|
604
|
2 |
|
if ($firstWildcard > $namelen) { |
|
605
|
1 |
|
return false; |
|
606
|
|
|
} |
|
607
|
|
|
|
|
608
|
2 |
|
if (strncmp($pattern, $name, $firstWildcard)) { |
|
|
|
|
|
|
609
|
2 |
|
return false; |
|
610
|
|
|
} |
|
611
|
|
|
|
|
612
|
2 |
|
$pattern = StringHelper::byteSubstr($pattern, $firstWildcard, StringHelper::byteLength($pattern)); |
|
|
|
|
|
|
613
|
2 |
|
$name = StringHelper::byteSubstr($name, $firstWildcard, $namelen); |
|
614
|
|
|
|
|
615
|
|
|
// If the whole pattern did not have a wildcard, then our prefix match is all we need; we do not need to call fnmatch at all. |
|
616
|
2 |
|
if (empty($pattern) && empty($name)) { |
|
617
|
1 |
|
return true; |
|
618
|
|
|
} |
|
619
|
|
|
} |
|
620
|
|
|
|
|
621
|
|
|
$matchOptions = [ |
|
622
|
2 |
|
'filePath' => true |
|
623
|
|
|
]; |
|
624
|
|
|
|
|
625
|
2 |
|
if ($flags & self::PATTERN_CASE_INSENSITIVE) { |
|
626
|
|
|
$matchOptions['caseSensitive'] = false; |
|
627
|
|
|
} |
|
628
|
|
|
|
|
629
|
2 |
|
return StringHelper::matchWildcard($pattern, $name, $matchOptions); |
|
630
|
|
|
} |
|
631
|
|
|
|
|
632
|
|
|
/** |
|
633
|
|
|
* Processes the pattern, stripping special characters like / and ! from the beginning and settings flags instead. |
|
634
|
|
|
* |
|
635
|
|
|
* @param string $pattern |
|
636
|
|
|
* @param bool $caseSensitive |
|
637
|
|
|
* |
|
638
|
|
|
* @return array with keys: (string) pattern, (int) flags, (int|bool) firstWildcard |
|
639
|
|
|
*/ |
|
640
|
2 |
|
private static function parseExcludePattern(string $pattern, bool $caseSensitive): array |
|
641
|
|
|
{ |
|
642
|
|
|
$result = [ |
|
643
|
2 |
|
'pattern' => $pattern, |
|
644
|
2 |
|
'flags' => 0, |
|
645
|
|
|
'firstWildcard' => false, |
|
646
|
|
|
]; |
|
647
|
|
|
|
|
648
|
2 |
|
$result = static::isCaseInsensitive($caseSensitive, $result); |
|
649
|
|
|
|
|
650
|
2 |
|
if (!isset($pattern[0])) { |
|
651
|
|
|
return $result; |
|
652
|
|
|
} |
|
653
|
|
|
|
|
654
|
2 |
|
if (strpos($pattern, '!') === 0) { |
|
655
|
|
|
$result['flags'] |= self::PATTERN_NEGATIVE; |
|
656
|
|
|
$pattern = StringHelper::byteSubstr($pattern, 1, StringHelper::byteLength($pattern)); |
|
657
|
|
|
} |
|
658
|
|
|
|
|
659
|
2 |
|
if (StringHelper::byteLength($pattern) && StringHelper::byteSubstr($pattern, -1, 1) === '/') { |
|
660
|
|
|
$pattern = StringHelper::byteSubstr($pattern, 0, -1); |
|
661
|
|
|
$result['flags'] |= self::PATTERN_MUST_BE_DIR; |
|
662
|
|
|
} |
|
663
|
|
|
|
|
664
|
2 |
|
$result = static::isPatternNoDir($pattern, $result); |
|
665
|
|
|
|
|
666
|
2 |
|
$result['firstWildcard'] = self::firstWildcardInPattern($pattern); |
|
667
|
|
|
|
|
668
|
2 |
|
$result = static::isPatternEndsWith($pattern, $result); |
|
669
|
|
|
|
|
670
|
2 |
|
$result['pattern'] = $pattern; |
|
671
|
|
|
|
|
672
|
2 |
|
return $result; |
|
673
|
|
|
} |
|
674
|
|
|
|
|
675
|
|
|
/** |
|
676
|
|
|
* Check isCaseInsensitive. |
|
677
|
|
|
* |
|
678
|
|
|
* @param boolean $caseSensitive |
|
679
|
|
|
* @param array $result |
|
680
|
|
|
* |
|
681
|
|
|
* @return array |
|
682
|
|
|
*/ |
|
683
|
2 |
|
private static function isCaseInsensitive(bool $caseSensitive, array $result): array |
|
684
|
|
|
{ |
|
685
|
2 |
|
if (!$caseSensitive) { |
|
686
|
|
|
$result['flags'] |= self::PATTERN_CASE_INSENSITIVE; |
|
687
|
|
|
} |
|
688
|
|
|
|
|
689
|
2 |
|
return $result; |
|
690
|
|
|
} |
|
691
|
|
|
|
|
692
|
|
|
/** |
|
693
|
|
|
* Check pattern no directory. |
|
694
|
|
|
* |
|
695
|
|
|
* @param string $pattern |
|
696
|
|
|
* @param array $result |
|
697
|
|
|
* |
|
698
|
|
|
* @return array |
|
699
|
|
|
*/ |
|
700
|
2 |
|
private static function isPatternNoDir(string $pattern, array $result): array |
|
701
|
|
|
{ |
|
702
|
2 |
|
if (strpos($pattern, '/') === false) { |
|
703
|
|
|
$result['flags'] |= self::PATTERN_NO_DIR; |
|
704
|
|
|
} |
|
705
|
|
|
|
|
706
|
2 |
|
return $result; |
|
707
|
|
|
} |
|
708
|
|
|
|
|
709
|
|
|
/** |
|
710
|
|
|
* Check pattern ends with |
|
711
|
|
|
* |
|
712
|
|
|
* @param string $pattern |
|
713
|
|
|
* @param array $result |
|
714
|
|
|
* |
|
715
|
|
|
* @return array |
|
716
|
|
|
*/ |
|
717
|
2 |
|
private static function isPatternEndsWith(string $pattern, array $result): array |
|
718
|
|
|
{ |
|
719
|
2 |
|
if (strpos($pattern, '*') === 0 && self::firstWildcardInPattern(StringHelper::byteSubstr($pattern, 1, StringHelper::byteLength($pattern))) === false) { |
|
720
|
|
|
$result['flags'] |= self::PATTERN_ENDS_WITH; |
|
721
|
|
|
} |
|
722
|
|
|
|
|
723
|
2 |
|
return $result; |
|
724
|
|
|
} |
|
725
|
|
|
} |
|
726
|
|
|
|