-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathFileSystem.php
More file actions
79 lines (65 loc) · 2.07 KB
/
Copy pathFileSystem.php
File metadata and controls
79 lines (65 loc) · 2.07 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
<?php
declare(strict_types=1);
namespace Codeception\Util;
use RecursiveDirectoryIterator;
use RecursiveIteratorIterator;
use FilesystemIterator;
class FileSystem
{
public static function doEmptyDir(string $path): void
{
self::clearDir($path, ['.gitignore', '.gitkeep']);
}
public static function deleteDir(string $dir): bool
{
if (!file_exists($dir)) {
return true;
}
if (!is_dir($dir) || is_link($dir)) {
return @unlink($dir);
}
if (strncasecmp(PHP_OS, 'WIN', 3) === 0) {
$winPath = str_replace('/', '\\', $dir);
exec(sprintf('rd /s /q "%s"', $winPath));
return true;
}
self::clearDir($dir);
return @rmdir($dir);
}
public static function copyDir(string $src, string $dst): void
{
if (!is_dir($src)) {
return;
}
$src = rtrim($src, DIRECTORY_SEPARATOR);
@mkdir($dst, 0777, true);
$baseLen = strlen($src) + 1;
foreach (self::createIterator($src, RecursiveIteratorIterator::SELF_FIRST) as $item) {
$target = $dst . DIRECTORY_SEPARATOR . substr($item->getPathname(), $baseLen);
if ($item->isDir()) {
@mkdir($target, 0777, true);
} else {
copy($item->getPathname(), $target);
}
}
}
/**
* @param string[] $preserve
*/
private static function clearDir(string $path, array $preserve = []): void
{
foreach (self::createIterator($path, RecursiveIteratorIterator::CHILD_FIRST) as $item) {
if (in_array($item->getFilename(), $preserve, true)) {
continue;
}
$item->isDir() ? @rmdir($item->getPathname()) : @unlink($item->getPathname());
}
}
private static function createIterator(string $path, int $mode): RecursiveIteratorIterator
{
return new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($path, FilesystemIterator::SKIP_DOTS),
$mode
);
}
}