1
1
<?php
2
+ declare (strict_types=1 );
2
3
/**
3
4
* MIT License
4
5
*
25
26
26
27
namespace doganoo \PHPUtil \FileSystem ;
27
28
29
+ use RecursiveDirectoryIterator ;
30
+ use SplFileInfo ;
31
+ use function fwrite ;
32
+ use function is_dir ;
33
+ use function is_readable ;
34
+ use function is_writable ;
35
+ use function mkdir ;
36
+ use function pathinfo ;
37
+ use function realpath ;
38
+ use function time ;
39
+ use function touch ;
40
+ use function unlink ;
41
+
28
42
/**
29
43
* Class DirHandler
30
44
*
31
45
* @package doganoo\PHPUtil\FileSystem
32
46
*/
33
47
class DirHandler {
48
+
34
49
public const DEFAULT_PERMISSION_MODE = 0770 ;
35
50
private $ path = null ;
36
51
@@ -48,7 +63,7 @@ public function __construct(string $path) {
48
63
* @return bool
49
64
*/
50
65
public function isReadable (): bool {
51
- return $ this ->isDir () && \ is_readable ($ this ->path );
66
+ return $ this ->isDir () && is_readable ($ this ->path );
52
67
}
53
68
54
69
/**
@@ -61,12 +76,12 @@ public function isDir(): bool {
61
76
}
62
77
63
78
/**
64
- * @param int $mode
79
+ * @param int $mode
65
80
* @param bool $recursive
66
81
* @return bool
67
82
*/
68
83
public function mkdir (int $ mode = DirHandler::DEFAULT_PERMISSION_MODE , bool $ recursive = true ): bool {
69
- return \ mkdir ($ this ->getPath (), $ mode , $ recursive );
84
+ return mkdir ($ this ->getPath (), $ mode , $ recursive );
70
85
}
71
86
72
87
/**
@@ -89,7 +104,7 @@ public function setPath(string $path) {
89
104
* @return bool
90
105
*/
91
106
public function isWritable (): bool {
92
- return $ this ->isDir () && \ is_writable ($ this ->path );
107
+ return $ this ->isDir () && is_writable ($ this ->path );
93
108
}
94
109
95
110
/**
@@ -111,7 +126,7 @@ public function list(): array {
111
126
*/
112
127
private function _list (string $ path ): array {
113
128
$ result = [];
114
- $ scan = glob ($ path . '/* ' );
129
+ $ scan = glob ($ path . '/* ' );
115
130
foreach ($ scan as $ item ) {
116
131
if (is_dir ($ item )) {
117
132
$ result [basename ($ item )] = $ this ->_list ($ item );
@@ -124,25 +139,25 @@ private function _list(string $path): array {
124
139
125
140
/**
126
141
* @param string $name
127
- * @param bool $override
142
+ * @param bool $override
128
143
* @param string $content
129
144
* @return bool
130
145
*/
131
146
public function createFile (string $ name , bool $ override = false , string $ content = null ): bool {
132
147
if (!$ this ->exists ()) return false ;
133
148
if (!$ override && $ this ->hasFile ($ name )) return true ;
134
- $ path = $ this ->toRealPath ();
149
+ $ path = $ this ->toRealPath ();
135
150
$ filePath = $ path . "/ " . $ name ;
136
- $ touched = \ touch ($ filePath , \ time (), \ time ());
151
+ $ touched = touch ($ filePath , time (), time ());
137
152
if (null === $ content ) return $ touched ;
138
153
if (false === $ touched ) return false ;
139
154
$ handle = fopen ($ filePath , "w+ " );
140
155
if (false === $ handle ) {
141
156
$ this ->deleteFile ($ filePath );
142
157
return false ;
143
158
}
144
- $ written = \ fwrite ($ handle ,$ content );
145
- if (false === $ written ){
159
+ $ written = fwrite ($ handle , $ content );
160
+ if (false === $ written ) {
146
161
$ this ->deleteFile ($ written );
147
162
return false ;
148
163
}
@@ -155,14 +170,14 @@ public function createFile(string $name, bool $override = false, string $content
155
170
public function exists (): bool {
156
171
$ path = $ this ->toRealPath ();
157
172
158
- return null !== $ path && true === \ is_dir ($ path );
173
+ return null !== $ path && true === is_dir ($ path );
159
174
}
160
175
161
176
/**
162
177
* @return string|null
163
178
*/
164
179
public function toRealPath (): ?string {
165
- $ realpath = \ realpath ($ this ->path );
180
+ $ realpath = realpath ($ this ->path );
166
181
if (false === $ realpath ) return null ;
167
182
return $ realpath ;
168
183
}
@@ -188,15 +203,15 @@ public function findFile(string $fileName): ?FileHandler {
188
203
*
189
204
* @param $dirName
190
205
* @param $fileName
191
- * @return string
206
+ * @return FileHandler
192
207
*/
193
208
private function _findFile (string $ dirName , string $ fileName ): ?FileHandler {
194
209
$ dirs = glob ($ dirName . '* ' );
195
210
$ file = null ;
196
211
foreach ($ dirs as $ d ) {
197
212
if (is_file ($ d )) {
198
- $ pathInfo = \ pathinfo ($ d );
199
- $ pathInfo2 = \ pathinfo ($ fileName );
213
+ $ pathInfo = pathinfo ($ d );
214
+ $ pathInfo2 = pathinfo ($ fileName );
200
215
201
216
if (isset ($ pathInfo2 ["extension " ])) {
202
217
$ condition = $ pathInfo ["basename " ] === $ pathInfo2 ["basename " ];
@@ -207,10 +222,12 @@ private function _findFile(string $dirName, string $fileName): ?FileHandler {
207
222
if ($ condition ) {
208
223
return new FileHandler ($ dirName . "/ " . $ pathInfo ["basename " ]);
209
224
}
210
- } else if (is_dir ($ d )) {
211
- $ tmp = $ this ->_findFile ($ d . "/ " , $ fileName );
212
- if (null !== $ tmp ) {
213
- $ file = $ tmp ;
225
+ } else {
226
+ if (is_dir ($ d )) {
227
+ $ tmp = $ this ->_findFile ($ d . "/ " , $ fileName );
228
+ if (null !== $ tmp ) {
229
+ $ file = $ tmp ;
230
+ }
214
231
}
215
232
}
216
233
}
@@ -225,7 +242,30 @@ public function deleteFile(string $name): bool {
225
242
if (false === $ this ->exists ()) return false ;
226
243
if (false === $ this ->hasFile ($ name )) return false ;
227
244
$ path = $ this ->toRealPath ();
228
- return \unlink ($ path . "/ " . $ name );
245
+ return unlink ($ path . "/ " . $ name );
246
+ }
247
+
248
+ /**
249
+ * removes a directory in the base directory
250
+ *
251
+ * @param string|null $name
252
+ * @return bool
253
+ */
254
+ public function rmdir (string $ name = null ): bool {
255
+ $ iterator = new RecursiveDirectoryIterator (
256
+ $ this ->getPath ()
257
+ );
258
+
259
+ /** @var SplFileInfo $item */
260
+ foreach ($ iterator as $ item ) {
261
+ if (false === $ item ->isDir ()) continue ;
262
+ if ($ item ->getBasename () === $ name ) {
263
+ unlink ($ item ->getRealPath ());
264
+ return true ;
265
+ }
266
+ }
267
+
268
+ return false ;
229
269
}
230
270
231
271
}
0 commit comments