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