1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @package Cadmium\Framework\Explorer |
5
|
|
|
* @author Anton Romanov |
6
|
|
|
* @copyright Copyright (c) 2015-2017, Anton Romanov |
7
|
|
|
* @link http://cadmium-cms.com |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace { |
11
|
|
|
|
12
|
|
|
abstract class Explorer { |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Get list of directory items |
16
|
|
|
*/ |
17
|
|
|
|
18
|
|
|
private static function getList(string $dir_name, string $type = null) : Generator { |
19
|
|
|
|
20
|
|
|
if (false !== ($handler = @opendir($dir_name))) { |
21
|
|
|
|
22
|
|
|
while (false !== ($name = readdir($handler))) { |
23
|
|
|
|
24
|
|
|
if (($name === '.') || ($name === '..')) continue; |
25
|
|
|
|
26
|
|
|
if ((null === $type) || (@filetype($dir_name . $name) === $type)) yield $name; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
closedir($handler); |
30
|
|
|
} |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Get information about a file |
35
|
|
|
* |
36
|
|
|
* @return string|false : the string or false if $check_exists is true and the file does not actually exists |
37
|
|
|
*/ |
38
|
|
|
|
39
|
|
|
private static function getInfo(string $file_name, int $param, bool $check_exists = true) { |
40
|
|
|
|
41
|
|
|
if ($check_exists && !self::isFile($file_name)) return false; |
42
|
|
|
|
43
|
|
|
return pathinfo($file_name, $param); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Check if a file (or a directory) exists |
48
|
|
|
*/ |
49
|
|
|
|
50
|
|
|
public static function exists(string $file_name) : bool { |
51
|
|
|
|
52
|
|
|
return @file_exists($file_name); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Check if a directory exists |
57
|
|
|
*/ |
58
|
|
|
|
59
|
|
|
public static function isDir(string $dir_name) : bool { |
60
|
|
|
|
61
|
|
|
return (@file_exists($dir_name) && @is_dir($dir_name)); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Check if a file exists |
66
|
|
|
*/ |
67
|
|
|
|
68
|
|
|
public static function isFile(string $file_name) : bool { |
69
|
|
|
|
70
|
|
|
return (@file_exists($file_name) && @is_file($file_name)); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Create a directory |
75
|
|
|
* |
76
|
|
|
* @return bool : true on success or false on failure |
77
|
|
|
*/ |
78
|
|
|
|
79
|
|
|
public static function createDir(string $dir_name, int $mode = 0755) : bool { |
80
|
|
|
|
81
|
|
|
return @mkdir($dir_name, $mode, true); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Create a file |
86
|
|
|
* |
87
|
|
|
* @return bool : true on success or false on failure |
88
|
|
|
*/ |
89
|
|
|
|
90
|
|
|
public static function createFile(string $file_name) : bool { |
91
|
|
|
|
92
|
|
|
return @touch($file_name); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Rename a file (or a directory) |
97
|
|
|
* |
98
|
|
|
* @return bool : true on success or false on failure |
99
|
|
|
*/ |
100
|
|
|
|
101
|
|
|
public static function rename(string $file_name, string $new_file_name) { |
102
|
|
|
|
103
|
|
|
return @rename($file_name, $new_file_name); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Remove a directory |
108
|
|
|
* |
109
|
|
|
* @return bool : true on success or false on failure |
110
|
|
|
*/ |
111
|
|
|
|
112
|
|
|
public static function removeDir(string $dir_name, bool $recursive = false) : bool { |
113
|
|
|
|
114
|
|
|
if ($recursive && (false !== ($list = @scandir($dir_name)))) { |
115
|
|
|
|
116
|
|
|
foreach (array_diff($list, ['.', '..']) as $name) { |
117
|
|
|
|
118
|
|
|
$name = ($dir_name . '/' . $name); |
119
|
|
|
|
120
|
|
|
if (@is_dir($name)) self::removeDir($name, true); |
121
|
|
|
|
122
|
|
|
else if (@is_file($name)) self::removeFile($name); |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
# ------------------------ |
127
|
|
|
|
128
|
|
|
return @rmdir($dir_name); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Remove a file |
133
|
|
|
* |
134
|
|
|
* @return bool : true on success or false on failure |
135
|
|
|
*/ |
136
|
|
|
|
137
|
|
|
public static function removeFile(string $file_name) : bool { |
138
|
|
|
|
139
|
|
|
return @unlink($file_name); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Iterate over files and directories within a given directory |
144
|
|
|
*/ |
145
|
|
|
|
146
|
|
|
public static function iterate(string $dir_name) : Generator { |
147
|
|
|
|
148
|
|
|
foreach (self::getList($dir_name) as $name) yield $name; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Iterate over directories within a given directory |
153
|
|
|
*/ |
154
|
|
|
|
155
|
|
|
public static function iterateDirs(string $dir_name) : Generator { |
156
|
|
|
|
157
|
|
|
foreach (self::getList($dir_name, 'dir') as $name) yield $name; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Iterate over files within a given directory |
162
|
|
|
*/ |
163
|
|
|
|
164
|
|
|
public static function iterateFiles(string $dir_name) : Generator { |
165
|
|
|
|
166
|
|
|
foreach (self::getList($dir_name, 'file') as $name) yield $name; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* Get a list of files and directories within a given directory |
171
|
|
|
*/ |
172
|
|
|
|
173
|
|
|
public static function list(string $dir_name) : array { |
174
|
|
|
|
175
|
|
|
return iterator_to_array(self::getList($dir_name)); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* Get a list of directories within a given directory |
180
|
|
|
*/ |
181
|
|
|
|
182
|
|
|
public static function listDirs(string $dir_name) : array { |
183
|
|
|
|
184
|
|
|
return iterator_to_array(self::getList($dir_name, 'dir')); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* Get a list of files within a given directory |
189
|
|
|
*/ |
190
|
|
|
|
191
|
|
|
public static function listFiles(string $dir_name) : array { |
192
|
|
|
|
193
|
|
|
return iterator_to_array(self::getList($dir_name, 'file')); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* Get a parent directory name |
198
|
|
|
* |
199
|
|
|
* @return string|false : the name or false if $check_exists is true and the file does not actually exists |
200
|
|
|
*/ |
201
|
|
|
|
202
|
|
|
public static function getDirname(string $file_name, bool $check_exists = true) { |
203
|
|
|
|
204
|
|
|
return self::getInfo($file_name, PATHINFO_DIRNAME, $check_exists); |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* Get a basename of a file |
209
|
|
|
* |
210
|
|
|
* @return string|false : the basename or false if $check_exists is true and the file does not actually exists |
211
|
|
|
*/ |
212
|
|
|
|
213
|
|
|
public static function getBasename(string $file_name, bool $check_exists = true) { |
214
|
|
|
|
215
|
|
|
return self::getInfo($file_name, PATHINFO_BASENAME, $check_exists); |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
/** |
219
|
|
|
* Get a filename of a file |
220
|
|
|
* |
221
|
|
|
* @return string|false : the filename or false if $check_exists is true and the file does not actually exists |
222
|
|
|
*/ |
223
|
|
|
|
224
|
|
|
public static function getFilename(string $file_name, bool $check_exists = true) { |
225
|
|
|
|
226
|
|
|
return self::getInfo($file_name, PATHINFO_FILENAME, $check_exists); |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
/** |
230
|
|
|
* Get an extnesion of a file |
231
|
|
|
* |
232
|
|
|
* @return string|false : the extension or false if $check_exists is true and the file does not actually exists |
233
|
|
|
*/ |
234
|
|
|
|
235
|
|
|
public static function getExtension(string $file_name, bool $check_exists = true) { |
236
|
|
|
|
237
|
|
|
return self::getInfo($file_name, PATHINFO_EXTENSION, $check_exists); |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
/** |
241
|
|
|
* Get a file type |
242
|
|
|
* |
243
|
|
|
* @return string|false : one of the following values: 'dir', 'file', 'fifo', 'char', 'block', 'link', 'socket', 'unknown', |
244
|
|
|
* or false on failure |
245
|
|
|
*/ |
246
|
|
|
|
247
|
|
|
public static function getType(string $file_name) { |
248
|
|
|
|
249
|
|
|
return @filetype($file_name); |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
/** |
253
|
|
|
* Get a file (or a directory) creation time |
254
|
|
|
* |
255
|
|
|
* @return int|false : the time or false on failure |
256
|
|
|
*/ |
257
|
|
|
|
258
|
|
|
public static function getCreated(string $file_name) { |
259
|
|
|
|
260
|
|
|
return @filectime($file_name); |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
/** |
264
|
|
|
* Get a file (or a directory) access time |
265
|
|
|
* |
266
|
|
|
* @return int|false : the time or false on failure |
267
|
|
|
*/ |
268
|
|
|
|
269
|
|
|
public static function getAccessed(string $file_name) { |
270
|
|
|
|
271
|
|
|
return @fileatime($file_name); |
272
|
|
|
} |
273
|
|
|
|
274
|
|
|
/** |
275
|
|
|
* Get a file (or a directory) modification time |
276
|
|
|
* |
277
|
|
|
* @return int|false : the time or false on failure |
278
|
|
|
*/ |
279
|
|
|
|
280
|
|
|
public static function getModified(string $file_name) { |
281
|
|
|
|
282
|
|
|
return @filemtime($file_name); |
283
|
|
|
} |
284
|
|
|
|
285
|
|
|
/** |
286
|
|
|
* Get a file (or a directory) permissions |
287
|
|
|
* |
288
|
|
|
* @return int|false : the permissions or false on failure |
289
|
|
|
*/ |
290
|
|
|
|
291
|
|
|
public static function getPermissions(string $file_name) { |
292
|
|
|
|
293
|
|
|
return @fileperms($file_name); |
294
|
|
|
} |
295
|
|
|
|
296
|
|
|
/** |
297
|
|
|
* Get a file (or a directory) size |
298
|
|
|
* |
299
|
|
|
* @return int|false : the size or false on failure |
300
|
|
|
*/ |
301
|
|
|
|
302
|
|
|
public static function getSize(string $file_name) { |
303
|
|
|
|
304
|
|
|
return @filesize($file_name); |
305
|
|
|
} |
306
|
|
|
|
307
|
|
|
/** |
308
|
|
|
* Get file contents |
309
|
|
|
* |
310
|
|
|
* @return string|false : the read data or false on failure |
311
|
|
|
*/ |
312
|
|
|
|
313
|
|
|
public static function getContents(string $file_name) { |
314
|
|
|
|
315
|
|
|
return @file_get_contents($file_name); |
316
|
|
|
} |
317
|
|
|
|
318
|
|
|
/** |
319
|
|
|
* Save data into a file |
320
|
|
|
* |
321
|
|
|
* @return int|false : the number of bytes that were written to the file or false on failure |
322
|
|
|
*/ |
323
|
|
|
|
324
|
|
|
public static function putContents(string $file_name, string $contents) { |
325
|
|
|
|
326
|
|
|
return @file_put_contents($file_name, $contents); |
327
|
|
|
} |
328
|
|
|
|
329
|
|
|
/** |
330
|
|
|
* Include a PHP file |
331
|
|
|
* |
332
|
|
|
* @return mixed|false : the file return data or false on failure |
333
|
|
|
*/ |
334
|
|
|
|
335
|
|
|
public static function include(string $file_name) { |
336
|
|
|
|
337
|
|
|
if ((strtolower(self::getExtension($file_name)) !== 'php')) return false; |
338
|
|
|
|
339
|
|
|
return @include $file_name; |
340
|
|
|
} |
341
|
|
|
} |
342
|
|
|
} |
343
|
|
|
|