1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ElegantMedia\PHPToolkit; |
4
|
|
|
|
5
|
|
|
use ElegantMedia\PHPToolkit\Exceptions\FileSystem\DirectoryMissingException; |
6
|
|
|
use ElegantMedia\PHPToolkit\Exceptions\FileSystem\DirectoryNotCreatedException; |
7
|
|
|
|
8
|
|
|
class Dir |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* Create a directory if it doesn't exist. |
12
|
|
|
* |
13
|
|
|
* @param $dirPath |
14
|
|
|
* @param int $permissions |
15
|
|
|
* @param bool $recursive |
16
|
|
|
* |
17
|
|
|
* @return bool |
18
|
|
|
* |
19
|
|
|
* @throws DirectoryNotCreatedException |
20
|
|
|
*/ |
21
|
|
|
public static function makeDirectoryIfNotExists($dirPath, $permissions = 0775, $recursive = true): bool |
22
|
6 |
|
{ |
23
|
|
|
if (is_dir($dirPath)) { |
24
|
6 |
|
return true; |
25
|
3 |
|
} |
26
|
|
|
|
27
|
|
|
if (!mkdir($dirPath, $permissions, $recursive) && !is_dir($dirPath)) { |
28
|
3 |
|
throw new DirectoryNotCreatedException(sprintf('Directory "%s" was not created', $dirPath)); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
if (is_dir($dirPath)) { |
32
|
3 |
|
return true; |
33
|
3 |
|
} |
34
|
|
|
|
35
|
|
|
return false; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Delete a directory. |
40
|
|
|
* |
41
|
|
|
* @param $dirPath |
42
|
|
|
* |
43
|
|
|
* @return bool |
44
|
|
|
* |
45
|
|
|
* @throws DirectoryMissingException |
46
|
|
|
*/ |
47
|
9 |
|
public static function deleteDirectory($dirPath): bool |
48
|
|
|
{ |
49
|
9 |
|
$invalidDirecotry = !is_readable($dirPath); |
50
|
9 |
|
if ($invalidDirecotry) { |
51
|
3 |
|
throw new DirectoryMissingException(sprintf("Directory '%s' does not exist or is not readable", $dirPath)); |
52
|
|
|
} |
53
|
|
|
|
54
|
6 |
|
$isFile = !is_dir($dirPath); |
55
|
6 |
|
if ($isFile) { |
56
|
6 |
|
return unlink($dirPath); |
57
|
|
|
} |
58
|
|
|
|
59
|
3 |
|
$isEmpty = count(scandir($dirPath)) === 0; |
60
|
3 |
|
if ($isEmpty) { |
61
|
|
|
return rmdir($dirPath); |
62
|
|
|
} |
63
|
|
|
|
64
|
3 |
|
foreach (scandir($dirPath) as $item) { |
65
|
3 |
|
if ($item == '.' || $item == '..') { |
66
|
3 |
|
continue; |
67
|
|
|
} |
68
|
|
|
|
69
|
3 |
|
if (!static::deleteDirectory($dirPath . DIRECTORY_SEPARATOR . $item)) { |
70
|
|
|
return false; |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|
74
|
3 |
|
return rmdir($dirPath); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Delete files in a directory by wildcard. |
79
|
|
|
* |
80
|
|
|
* @param $dir |
81
|
|
|
* @param $wildcard |
82
|
|
|
* |
83
|
|
|
* @return int |
84
|
|
|
*/ |
85
|
|
|
public static function cleanDirectoryByWildcard($dir, $wildcard): int |
86
|
|
|
{ |
87
|
|
|
// get the files |
88
|
|
|
$files = glob($dir . DIRECTORY_SEPARATOR . $wildcard); |
89
|
|
|
|
90
|
|
|
$count = 0; |
91
|
|
|
foreach ($files as $file) { |
92
|
|
|
if (is_file($file)) { |
93
|
|
|
unlink($file); |
94
|
|
|
$count++; |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
return $count; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Delete files in a directory by it's file extensions. |
103
|
|
|
* |
104
|
|
|
* @param $dir |
105
|
|
|
* @param $extension |
106
|
|
|
* |
107
|
|
|
* @return false|int |
108
|
|
|
*/ |
109
|
|
|
public static function cleanDirectoryByExtension($dir, $extension) |
110
|
|
|
{ |
111
|
|
|
$dir = rtrim($dir, DIRECTORY_SEPARATOR); |
112
|
|
|
|
113
|
|
|
if (!is_dir($dir)) { |
114
|
|
|
return false; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
if (empty($extension)) { |
118
|
|
|
return false; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
return self::cleanDirectoryByWildcard($dir, "*.$extension"); |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|