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