generated from yiisoft/package-template
-
-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathFileHelper.php
More file actions
718 lines (632 loc) · 24.6 KB
/
FileHelper.php
File metadata and controls
718 lines (632 loc) · 24.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
<?php
declare(strict_types=1);
namespace Yiisoft\Files;
use FilesystemIterator;
use InvalidArgumentException;
use LogicException;
use RecursiveDirectoryIterator;
use RecursiveIteratorIterator;
use RuntimeException;
use Yiisoft\Files\PathMatcher\PathMatcherInterface;
use function array_key_exists;
use function array_pop;
use function chmod;
use function closedir;
use function copy;
use function dirname;
use function end;
use function explode;
use function file_exists;
use function filemtime;
use function fopen;
use function get_debug_type;
use function implode;
use function is_dir;
use function is_file;
use function is_link;
use function is_string;
use function is_writable;
use function opendir;
use function readdir;
use function realpath;
use function restore_error_handler;
use function rmdir;
use function rtrim;
use function set_error_handler;
use function sprintf;
use function str_contains;
use function str_starts_with;
use function strtr;
use function substr;
use const DIRECTORY_SEPARATOR;
/**
* Provides useful methods to manage files and directories.
*/
final class FileHelper
{
/**
* Opens a file or URL.
*
* This method is similar to the PHP {@see fopen()} function, except that it suppresses the {@see E_WARNING}
* level error and throws the {@see RuntimeException} exception if it can't open the file.
*
* @param string $filename The file or URL.
* @param string $mode The type of access.
* @param bool $useIncludePath Whether to search for a file in the include path.
* @param resource|null $context The stream context or `null`.
*
* @throws RuntimeException If the file could not be opened.
*
* @return resource The file pointer resource.
*
* @psalm-suppress PossiblyNullArgument
*/
public static function openFile(string $filename, string $mode, bool $useIncludePath = false, $context = null)
{
/** @psalm-suppress InvalidArgument, MixedArgumentTypeCoercion */
set_error_handler(static function (int $errorNumber, string $errorString) use ($filename): bool {
throw new RuntimeException(
sprintf('Failed to open a file "%s". ', $filename) . $errorString,
$errorNumber,
);
});
try {
$filePointer = fopen($filename, $mode, $useIncludePath, $context);
} finally {
restore_error_handler();
}
if ($filePointer === false) {
throw new RuntimeException(sprintf('Failed to open a file "%s". ', $filename));
}
return $filePointer;
}
/**
* Ensures directory exists and has specific permissions.
*
* This method is similar to the PHP {@see mkdir()} function with some differences:
*
* - It does not fail if directory already exists.
* - It uses {@see chmod()} to set the permission of the created directory in order to avoid the impact
* of the `umask` setting.
* - It throws exceptions instead of returning false and emitting {@see E_WARNING}.
*
* @param string $path Path of the directory to be created.
* @param int $mode The permission to be set for the created directory.
*/
public static function ensureDirectory(string $path, int $mode = 0775): void
{
$path = self::normalizePath($path);
if (!is_dir($path)) {
set_error_handler(static function (int $errorNumber, string $errorString) use ($path): bool {
// Handle race condition.
// See https://github.com/kalessil/phpinspectionsea/blob/master/docs/probable-bugs.md#mkdir-race-condition
if (!is_dir($path)) {
throw new RuntimeException(
sprintf('Failed to create directory "%s". ', $path) . $errorString,
$errorNumber,
);
}
return true;
});
try {
mkdir($path, $mode, true);
} finally {
restore_error_handler();
}
}
if (!chmod($path, $mode)) {
throw new RuntimeException(sprintf('Unable to set mode "%s" for "%s".', $mode, $path));
}
}
/**
* Normalizes a file/directory path.
*
* The normalization does the following work:
*
* - Convert all directory separators into `/` (e.g. "\a/b\c" becomes "/a/b/c")
* - Remove trailing directory separators (e.g. "/a/b/c/" becomes "/a/b/c")
* - Turn multiple consecutive slashes into a single one (e.g. "/a///b/c" becomes "/a/b/c")
* - Remove ".." and "." based on their meanings (e.g. "/a/./b/../c" becomes "/a/c")
*
* @param string $path The file/directory path to be normalized.
*
* @return string The normalized file/directory path.
*/
public static function normalizePath(string $path): string
{
$isWindowsShare = str_starts_with($path, '\\\\');
if ($isWindowsShare) {
$path = substr($path, 2);
}
$path = rtrim(strtr($path, '\\', '/'), '/');
if (!str_contains('/' . $path, '/.') && !str_contains($path, '//')) {
return $isWindowsShare ? "\\\\$path" : $path;
}
$parts = [];
foreach (explode('/', $path) as $part) {
if ($part === '..' && !empty($parts) && end($parts) !== '..') {
array_pop($parts);
} elseif ($part !== '.' && ($part !== '' || empty($parts))) {
$parts[] = $part;
}
}
$path = implode('/', $parts);
if ($isWindowsShare) {
$path = '\\\\' . $path;
}
return $path === '' ? '.' : $path;
}
/**
* Removes a directory (and all its content) recursively. Does nothing if directory does not exist.
*
* @param string $directory The directory to be deleted recursively.
* @param array $options Options for directory remove. Valid options are:
*
* - traverseSymlinks: boolean, whether symlinks to the directories should be traversed too.
* Defaults to `false`, meaning the content of the symlinked directory would not be deleted.
* Only symlink would be removed in that default case.
*
* @psalm-param array{
* traverseSymlinks?:bool,
* } $options
*
* @throw RuntimeException When unable to remove directory.
*/
public static function removeDirectory(string $directory, array $options = []): void
{
if (!file_exists($directory)) {
return;
}
self::clearDirectory(
$directory,
['traverseSymlinks' => $options['traverseSymlinks'] ?? false],
);
self::removeLinkOrEmptyDirectory($directory);
}
/**
* Clears all directory content.
*
* @param string $directory The directory to be cleared.
* @param array $options Options for directory clear. Valid options are:
*
* - traverseSymlinks: boolean, whether symlinks to the directories should be traversed too.
* Defaults to `false`, meaning the content of the symlinked directory would not be deleted.
* Only symlink would be removed in that default case.
* - filter: a filter to apply while deleting files. It should be an instance of {@see PathMatcherInterface}.
*
* @throws RuntimeException if unable to open directory.
*
* @psalm-param array{
* traverseSymlinks?:bool,
* filter?:PathMatcherInterface
* } $options
*/
public static function clearDirectory(string $directory, array $options = []): void
{
$filter = self::getFilter($options);
$handle = self::openDirectory($directory);
if (!empty($options['traverseSymlinks']) || !is_link($directory)) {
while (($file = readdir($handle)) !== false) {
if ($file === '.' || $file === '..') {
continue;
}
$path = $directory . '/' . $file;
if ($filter === null || $filter->match($path)) {
if (is_dir($path)) {
self::clearDirectory($path, $options);
if (is_link($path) || self::isEmptyDirectory($path)) {
self::removeLinkOrEmptyDirectory($path);
}
} else {
self::unlink($path);
}
}
}
closedir($handle);
}
}
/**
* Removes a file or symlink in a cross-platform way.
*
* @param string $path Path to unlink.
*/
public static function unlink(string $path): void
{
/** @psalm-suppress InvalidArgument, MixedArgumentTypeCoercion */
set_error_handler(static function (int $errorNumber, string $errorString) use ($path): bool {
throw new RuntimeException(
sprintf('Failed to unlink "%s". ', $path) . $errorString,
$errorNumber,
);
});
try {
$isWindows = DIRECTORY_SEPARATOR === '\\';
if ($isWindows) {
if (is_link($path)) {
try {
unlink($path);
} catch (RuntimeException) {
rmdir($path);
}
} else {
if (file_exists($path) && !is_writable($path)) {
chmod($path, 0777);
}
unlink($path);
}
} else {
unlink($path);
}
} finally {
restore_error_handler();
}
}
/**
* Tells whether the path is an empty directory.
*
* @param string $path Path to check for being an empty directory.
*/
public static function isEmptyDirectory(string $path): bool
{
if (!is_dir($path)) {
return false;
}
return !(new FilesystemIterator($path))->valid();
}
/**
* Copies a whole directory as another one.
*
* The files and subdirectories will also be copied over.
*
* @param string $source The source directory.
* @param string $destination The destination directory.
* @param array $options Options for directory copy. Valid options are:
*
* - dirMode: integer, the permission to be set for newly copied directories. Defaults to 0775.
* - fileMode: integer, the permission to be set for newly copied files. Defaults to the current environment
* setting.
* - filter: a filter to apply while copying files. It should be an instance of {@see PathMatcherInterface}.
* - recursive: boolean, whether the files under the subdirectories should also be copied. Defaults to true.
* - beforeCopy: callback, a PHP callback that is called before copying each subdirectory or file. If the callback
* returns false, the copy operation for the subdirectory or file will be cancelled. The signature of the
* callback should be: `function ($from, $to)`, where `$from` is the subdirectory or file to be copied from,
* while `$to` is the copy target.
* - afterCopy: callback, a PHP callback that is called after each subdirectory or file is successfully copied.
* The signature of the callback should be: `function ($from, $to)`, where `$from` is the subdirectory or file
* copied from, while `$to` is the copy target.
* - copyEmptyDirectories: boolean, whether to copy empty directories. Set this to false to avoid creating
* directories that do not contain files. This affects directories that do not contain files initially as well as
* directories that do not contain files at the target destination because files have been filtered via `only` or
* `except`. Defaults to true.
*
* @throws RuntimeException if unable to open directory
*
* @psalm-param array{
* dirMode?: int,
* fileMode?: int,
* filter?: PathMatcherInterface,
* recursive?: bool,
* beforeCopy?: callable,
* afterCopy?: callable,
* copyEmptyDirectories?: bool,
* } $options
*/
public static function copyDirectory(string $source, string $destination, array $options = []): void
{
$source = self::normalizePath($source);
$destination = self::normalizePath($destination);
self::assertNotSelfDirectory($source, $destination);
if (self::processCallback($options['beforeCopy'] ?? null, $source, $destination) === false) {
return;
}
$filter = self::getFilter($options);
$recursive = !array_key_exists('recursive', $options) || $options['recursive'];
$copyEmptyDirectories = !array_key_exists('copyEmptyDirectories', $options) || $options['copyEmptyDirectories'];
if (!isset($options['dirMode'])) {
$options['dirMode'] = 0755;
}
if ($copyEmptyDirectories && !is_dir($destination)) {
self::ensureDirectory($destination, $options['dirMode']);
}
$handle = self::openDirectory($source);
if (!array_key_exists('basePath', $options)) {
$options['basePath'] = realpath($source);
}
while (($file = readdir($handle)) !== false) {
if ($file === '.' || $file === '..') {
continue;
}
$from = $source . '/' . $file;
$to = $destination . '/' . $file;
if ($filter === null || $filter->match($from)) {
/** @psalm-suppress InvalidArgument $options is compatible with `copyFile()` and `copyDirectory()` */
if (is_file($from)) {
self::copyFile($from, $to, $options);
} elseif ($recursive) {
self::copyDirectory($from, $to, $options);
}
}
}
closedir($handle);
self::processCallback($options['afterCopy'] ?? null, $source, $destination);
}
/**
* Copies files with some options.
*
* - dirMode: integer or null, the permission to be set for newly copied directories. Defaults to null.
* When null - directory will be not created
* - fileMode: integer, the permission to be set for newly copied files. Defaults to the current environment
* setting.
* - beforeCopy: callback, a PHP callback that is called before copying file. If the callback
* returns false, the copy operation for file will be cancelled. The signature of the
* callback should be: `function ($from, $to)`, where `$from` is the file to be copied from,
* while `$to` is the copy target.
* - afterCopy: callback, a PHP callback that is called after file if successfully copied.
* The signature of the callback should be: `function ($from, $to)`, where `$from` is the file
* copied from, while `$to` is the copy target.
*
* @param string $source The source file
* @param string $destination The destination filename
*
* @psalm-param array{
* dirMode?: int,
* fileMode?: int,
* beforeCopy?: callable,
* afterCopy?: callable,
* } $options
*/
public static function copyFile(string $source, string $destination, array $options = []): void
{
if (!is_file($source)) {
throw new InvalidArgumentException('Argument $source must be an existing file.');
}
if (self::processCallback($options['beforeCopy'] ?? null, $source, $destination) === false) {
return;
}
$dirname = dirname($destination);
$dirMode = $options['dirMode'] ?? 0755;
$fileMode = $options['fileMode'] ?? null;
if (!is_dir($dirname)) {
self::ensureDirectory($dirname, $dirMode);
}
if (!copy($source, $destination)) {
throw new RuntimeException('Failed to copy the file.');
}
if ($fileMode !== null && !chmod($destination, $fileMode)) {
throw new RuntimeException(sprintf('Unable to set mode "%s" for "%s".', $fileMode, $destination));
}
self::processCallback($options['afterCopy'] ?? null, $source, $destination);
}
/**
* Returns the last modification time for the given paths.
*
* If the path is a directory, any nested files/directories will be checked as well.
*
* @param RecursiveDirectoryIterator[]|string[] $paths The directories to be checked.
*
* @throws LogicException If path is not set.
*
* @return int|null Unix timestamp representing the last modification time.
*/
public static function lastModifiedTime(string|RecursiveDirectoryIterator ...$paths): ?int
{
if (empty($paths)) {
throw new LogicException('Path is required.');
}
$time = null;
foreach ($paths as $path) {
if (is_string($path)) {
$timestamp = self::modifiedTime($path);
if ($timestamp > $time) {
$time = $timestamp;
}
if (is_file($path)) {
continue;
}
$path = new RecursiveDirectoryIterator($path, FilesystemIterator::SKIP_DOTS);
}
/** @var iterable<string, string> $iterator */
$iterator = new RecursiveIteratorIterator(
$path,
RecursiveIteratorIterator::SELF_FIRST,
);
foreach ($iterator as $path => $_info) {
$timestamp = self::modifiedTime($path);
if ($timestamp > $time) {
$time = $timestamp;
}
}
}
return $time;
}
/**
* Returns the directories found under the specified directory and subdirectories.
*
* @param string $directory The directory under which the files will be looked for.
* @param array $options Options for directory searching. Valid options are:
*
* - filter: a filter to apply while looked directories. It should be an instance of {@see PathMatcherInterface}.
* - recursive: boolean, whether the subdirectories should also be looked for. Defaults to `true`.
*
* @psalm-param array{
* filter?: PathMatcherInterface,
* recursive?: bool,
* } $options
*
* @throws InvalidArgumentException If the directory is invalid.
*
* @return string[] Directories found under the directory specified, in no particular order.
* Ordering depends on the file system used.
*/
public static function findDirectories(string $directory, array $options = []): array
{
$filter = self::getFilter($options);
$recursive = !array_key_exists('recursive', $options) || $options['recursive'];
$directory = self::normalizePath($directory);
$result = [];
$handle = self::openDirectory($directory);
while (false !== $file = readdir($handle)) {
if ($file === '.' || $file === '..') {
continue;
}
$path = $directory . '/' . $file;
if (is_file($path)) {
continue;
}
if ($filter === null || $filter->match($path)) {
$result[] = $path;
}
if ($recursive) {
$result = array_merge($result, self::findDirectories($path, $options));
}
}
closedir($handle);
return $result;
}
/**
* Returns the files found under the specified directory and subdirectories.
*
* @param string $directory The directory under which the files will be looked for.
* @param array $options Options for file searching. Valid options are:
*
* - filter: a filter to apply while looked files. It should be an instance of {@see PathMatcherInterface}.
* - recursive: boolean, whether the files under the subdirectories should also be looked for. Defaults to `true`.
*
* @psalm-param array{
* filter?: PathMatcherInterface,
* recursive?: bool,
* } $options
*
* @throws InvalidArgumentException If the directory is invalid.
*
* @return string[] Files found under the directory specified, in no particular order.
* Ordering depends on the files system used.
*/
public static function findFiles(string $directory, array $options = []): array
{
$filter = self::getFilter($options);
$recursive = !array_key_exists('recursive', $options) || $options['recursive'];
$directory = self::normalizePath($directory);
$result = [];
$handle = self::openDirectory($directory);
while (false !== $file = readdir($handle)) {
if ($file === '.' || $file === '..') {
continue;
}
$path = $directory . '/' . $file;
if (is_file($path)) {
if ($filter === null || $filter->match($path)) {
$result[] = $path;
}
continue;
}
if ($recursive) {
$result = array_merge($result, self::findFiles($path, $options));
}
}
closedir($handle);
return $result;
}
/**
* @throws InvalidArgumentException
*/
private static function processCallback(?callable $callback, mixed ...$arguments): mixed
{
return $callback ? $callback(...$arguments) : null;
}
private static function getFilter(array $options): ?PathMatcherInterface
{
if (!array_key_exists('filter', $options)) {
return null;
}
if (!$options['filter'] instanceof PathMatcherInterface) {
$type = get_debug_type($options['filter']);
throw new InvalidArgumentException(
sprintf('Filter should be an instance of PathMatcherInterface, %s given.', $type),
);
}
return $options['filter'];
}
/**
* Assert that destination is not within the source directory.
*
* @param string $source Path to source.
* @param string $destination Path to destination.
*
* @throws InvalidArgumentException
*/
private static function assertNotSelfDirectory(string $source, string $destination): void
{
if ($source === $destination || str_starts_with($destination, $source . '/')) {
throw new InvalidArgumentException('Trying to copy a directory to itself or a subdirectory.');
}
}
/**
* Open directory handle.
*
* @param string $directory Path to directory.
*
* @throws RuntimeException if unable to open directory.
* @throws InvalidArgumentException if argument is not a directory.
*
* @return resource
*/
private static function openDirectory(string $directory)
{
if (!file_exists($directory)) {
throw new InvalidArgumentException("\"$directory\" does not exist.");
}
if (!is_dir($directory)) {
throw new InvalidArgumentException("\"$directory\" is not a directory.");
}
/** @psalm-suppress InvalidArgument, MixedArgumentTypeCoercion */
set_error_handler(static function (int $errorNumber, string $errorString) use ($directory): bool {
throw new RuntimeException(
sprintf('Unable to open directory "%s". ', $directory) . $errorString,
$errorNumber,
);
});
try {
$handle = opendir($directory);
if ($handle === false) {
throw new RuntimeException(sprintf('Unable to open directory "%s". ', $directory));
}
} finally {
restore_error_handler();
}
return $handle;
}
/**
* @see https://www.php.net/manual/function.filemtime.php
*/
private static function modifiedTime(string $path): ?int
{
$timestamp = @filemtime($path);
return $timestamp === false ? null : $timestamp;
}
/**
* Removes a link or an empty directory.
*
* @param string $directory The empty directory or the link to be deleted.
*
* @throw RuntimeException When unable to remove directory or link.
*/
private static function removeLinkOrEmptyDirectory(string $directory): void
{
if (is_link($directory)) {
self::unlink($directory);
} else {
set_error_handler(static function (int $errorNumber, string $errorString) use ($directory): bool {
throw new RuntimeException(
sprintf('Failed to remove directory "%s". ', $directory) . $errorString,
$errorNumber,
);
});
try {
rmdir($directory);
} finally {
restore_error_handler();
}
}
}
}