1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of the miBadger package. |
5
|
|
|
* |
6
|
|
|
* @author Michael Webbers <[email protected]> |
7
|
|
|
* @license http://opensource.org/licenses/Apache-2.0 Apache v2 License |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace miBadger\File; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* The file class. |
14
|
|
|
* |
15
|
|
|
* @since 1.0.0 |
16
|
|
|
*/ |
17
|
|
|
class File implements \Countable |
18
|
|
|
{ |
19
|
|
|
const DIRECTORY_SEPARATOR = \DIRECTORY_SEPARATOR; |
20
|
|
|
|
21
|
|
|
/** @var string the file path. */ |
22
|
|
|
protected $path; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Constructs a File object with the given path. |
26
|
|
|
* |
27
|
|
|
* @param string $path |
28
|
|
|
*/ |
29
|
35 |
|
public function __construct($path) |
30
|
|
|
{ |
31
|
35 |
|
if (mb_substr($path, -1) === static::DIRECTORY_SEPARATOR) { |
32
|
1 |
|
$this->path = mb_substr($path, 0, -1); |
33
|
|
|
} else { |
34
|
35 |
|
$this->path = $path; |
35
|
|
|
} |
36
|
35 |
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Returns the string representation of the File object. |
40
|
|
|
* |
41
|
|
|
* @return string the string representation of the File object. |
42
|
|
|
*/ |
43
|
2 |
|
public function __toString() |
44
|
|
|
{ |
45
|
2 |
|
return $this->getPath(); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Returns the path of the file. |
50
|
|
|
* |
51
|
|
|
* @return string the path of the file. |
52
|
|
|
*/ |
53
|
5 |
|
public function getPath() |
54
|
|
|
{ |
55
|
5 |
|
return $this->path; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Returns the parent directory of the file. |
60
|
|
|
* |
61
|
|
|
* @return string the parent directory of the file. |
62
|
|
|
*/ |
63
|
2 |
|
public function getDirectory() |
64
|
|
|
{ |
65
|
2 |
|
return dirname($this->path); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Returns the name of the file. |
70
|
|
|
* |
71
|
|
|
* @return string the name of the file. |
72
|
|
|
*/ |
73
|
1 |
|
public function getName() |
74
|
|
|
{ |
75
|
1 |
|
return basename($this->path); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Returns the extension of the file |
80
|
|
|
* |
81
|
|
|
* @return string the file extension |
82
|
|
|
*/ |
83
|
1 |
|
public function getExtension() |
84
|
|
|
{ |
85
|
1 |
|
return pathinfo($this->path, PATHINFO_EXTENSION); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Returns the mime-type as determined by information from php's magic.mime file, null on failure |
90
|
|
|
* |
91
|
|
|
* @return string|null the mime type |
92
|
|
|
*/ |
93
|
1 |
|
public function getMimeType() |
94
|
|
|
{ |
95
|
1 |
|
$mime = mime_content_type($this->path); |
96
|
1 |
|
if ($mime === false) { |
97
|
|
|
return null; |
98
|
|
|
} |
99
|
1 |
|
return $mime; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Returns true if the file exists. |
104
|
|
|
* |
105
|
|
|
* @return bool true if the file exists. |
106
|
|
|
*/ |
107
|
8 |
|
public function exists() |
108
|
|
|
{ |
109
|
8 |
|
return file_exists($this->path); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Returns true if you can execute the file. |
114
|
|
|
* |
115
|
|
|
* @return bool true if you can execute the file. |
116
|
|
|
*/ |
117
|
1 |
|
public function canExecute() |
118
|
|
|
{ |
119
|
1 |
|
return is_executable($this->path); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Returns true if you can read the file. |
124
|
|
|
* |
125
|
|
|
* @return bool true if you can read the file. |
126
|
|
|
*/ |
127
|
1 |
|
public function canRead() |
128
|
|
|
{ |
129
|
1 |
|
return is_readable($this->path); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Returns true if you can write the file. |
134
|
|
|
* |
135
|
|
|
* @return bool true if you can write the file. |
136
|
|
|
*/ |
137
|
1 |
|
public function canWrite() |
138
|
|
|
{ |
139
|
1 |
|
return is_writeable($this->path); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Returns true if the file is a file. |
144
|
|
|
* |
145
|
|
|
* @return bool true if the file is a file. |
146
|
|
|
*/ |
147
|
2 |
|
public function isFile() |
148
|
|
|
{ |
149
|
2 |
|
return is_file($this->path); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Returns true if the file is a directory. |
154
|
|
|
* |
155
|
|
|
* @return bool true if the file is a directory. |
156
|
|
|
*/ |
157
|
5 |
|
public function isDirectory() |
158
|
|
|
{ |
159
|
5 |
|
return is_dir($this->path); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* Returns the numer of bytes in the file, or -1 on failure. |
164
|
|
|
* |
165
|
|
|
* @return int the number of bytes in the file, or -1 on failure. |
166
|
|
|
*/ |
167
|
1 |
|
public function count() |
168
|
|
|
{ |
169
|
1 |
|
return $this->length(); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* Returns the numer of bytes in the file, or -1 on failure. |
174
|
|
|
* |
175
|
|
|
* @return int the number of bytes in the file, or -1 on failure. |
176
|
|
|
*/ |
177
|
|
|
public function size() |
178
|
|
|
{ |
179
|
|
|
return $this->length(); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* Returns the numer of bytes in the file, or -1 on failure. |
184
|
|
|
* |
185
|
|
|
* @return int the number of bytes in the file, or -1 on failure. |
186
|
|
|
*/ |
187
|
2 |
|
public function length() |
188
|
|
|
{ |
189
|
2 |
|
if (!$this->exists()) { |
190
|
2 |
|
return -1; |
191
|
|
|
} |
192
|
|
|
|
193
|
2 |
|
return ($result = filesize($this->path)) !== false ? $result : -1; |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* Returns the time of the last modification as a unixtimestap, or -1 on failure. |
198
|
|
|
* |
199
|
|
|
* @return int the time of the last modification as a unixtimestap, or -1 on failure. |
200
|
|
|
*/ |
201
|
1 |
|
public function lastModified() |
202
|
|
|
{ |
203
|
1 |
|
if (!$this->exists()) { |
204
|
1 |
|
return -1; |
205
|
|
|
} |
206
|
|
|
|
207
|
1 |
|
return ($result = filemtime($this->path)) !== false ? $result : -1; |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* Returns an iterator with the files and directories in the current directory. |
212
|
|
|
* |
213
|
|
|
* @param bool $recursive = false |
214
|
|
|
* @param bool $showHidden = false |
215
|
|
|
* @return \ArrayIterator|\FilesystemIterator|\RecursiveIteratorIterator an iterator with the files and directories in the current directory. |
216
|
|
|
*/ |
217
|
4 |
|
protected function listAllIterator($recursive = false, $showHidden = false) |
218
|
|
|
{ |
219
|
4 |
|
if (!$this->isDirectory()) { |
220
|
4 |
|
return new \ArrayIterator([]); |
221
|
|
|
} |
222
|
|
|
|
223
|
4 |
|
$flags = \FilesystemIterator::KEY_AS_PATHNAME | \FilesystemIterator::CURRENT_AS_FILEINFO; |
224
|
|
|
|
225
|
4 |
|
if (!$showHidden) { |
226
|
4 |
|
$flags = $flags | \FilesystemIterator::SKIP_DOTS; |
227
|
|
|
} |
228
|
|
|
|
229
|
4 |
|
if ($recursive) { |
230
|
1 |
|
return new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($this->path, $flags), \RecursiveIteratorIterator::SELF_FIRST); |
231
|
|
|
} |
232
|
|
|
|
233
|
3 |
|
return new \FilesystemIterator($this->path, $flags); |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
/** |
237
|
|
|
* Returns an array with the files and directories in the current directory. |
238
|
|
|
* |
239
|
|
|
* @param bool $recursive = false |
240
|
|
|
* @param bool $showHidden = false |
241
|
|
|
* @return string[] an array with the files and directories in the current directory. |
242
|
|
|
*/ |
243
|
2 |
|
public function listAll($recursive = false, $showHidden = false) |
244
|
|
|
{ |
245
|
2 |
|
$result = []; |
246
|
|
|
|
247
|
2 |
|
foreach ($this->listAllIterator($recursive, $showHidden) as $element) { |
248
|
2 |
|
$result[] = $element->getFilename(); |
249
|
|
|
} |
250
|
|
|
|
251
|
2 |
|
return $result; |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
/** |
255
|
|
|
* Returns an array with the directories in the current directory. |
256
|
|
|
* |
257
|
|
|
* @param bool $recursive = false |
258
|
|
|
* @param bool $showHidden = false |
259
|
|
|
* @return string[] an array with the directories in the current directory. |
260
|
|
|
*/ |
261
|
1 |
|
public function listDirectories($recursive = false, $showHidden = false) |
262
|
|
|
{ |
263
|
1 |
|
$result = []; |
264
|
|
|
|
265
|
1 |
|
foreach ($this->listAllIterator($recursive, $showHidden) as $element) { |
266
|
1 |
|
if ($element->isDir()) { |
267
|
1 |
|
$result[] = $element->getFilename(); |
268
|
|
|
} |
269
|
|
|
} |
270
|
|
|
|
271
|
1 |
|
return $result; |
272
|
|
|
} |
273
|
|
|
|
274
|
|
|
/** |
275
|
|
|
* Returns an array with the files in the current directory. |
276
|
|
|
* |
277
|
|
|
* @param bool $recursive = false |
278
|
|
|
* @param bool $showHidden = false |
279
|
|
|
* @return string[] an array with the files in the current directory. |
280
|
|
|
*/ |
281
|
1 |
|
public function listFiles($recursive = false, $showHidden = false) |
282
|
|
|
{ |
283
|
1 |
|
$result = []; |
284
|
|
|
|
285
|
1 |
|
foreach ($this->listAllIterator($recursive, $showHidden) as $element) { |
286
|
1 |
|
if ($element->isFile()) { |
287
|
1 |
|
$result[] = $element->getFilename(); |
288
|
|
|
} |
289
|
|
|
} |
290
|
|
|
|
291
|
1 |
|
return $result; |
292
|
|
|
} |
293
|
|
|
|
294
|
|
|
/** |
295
|
|
|
* Returns true if the file has been created. |
296
|
|
|
* |
297
|
|
|
* @param bool $override = false |
298
|
|
|
* @return bool true if the file has been created. |
299
|
|
|
*/ |
300
|
1 |
|
public function makeFile($override = false) |
301
|
|
|
{ |
302
|
1 |
|
if ($this->exists() && !$override) { |
303
|
1 |
|
return false; |
304
|
|
|
} |
305
|
|
|
|
306
|
1 |
|
return file_put_contents($this->path, '') !== false; |
307
|
|
|
} |
308
|
|
|
|
309
|
|
|
/** |
310
|
|
|
* Returns true if the directory has been created. |
311
|
|
|
* |
312
|
|
|
* @param bool $recursive = false |
313
|
|
|
* @param int $permissions = 0755 |
314
|
|
|
* @return bool true if the directory has been created. |
315
|
|
|
*/ |
316
|
1 |
|
public function makeDirectory($recursive = false, $permissions = 0775) |
317
|
|
|
{ |
318
|
1 |
|
if ($this->exists()) { |
319
|
1 |
|
return false; |
320
|
|
|
} |
321
|
|
|
|
322
|
1 |
|
$old = umask(0777 - $permissions); |
323
|
1 |
|
$result = mkdir($this->path, $permissions, $recursive); |
324
|
1 |
|
umask($old); |
325
|
|
|
|
326
|
1 |
|
return $result; |
327
|
|
|
} |
328
|
|
|
|
329
|
|
|
/** |
330
|
|
|
* Returns true if the file is succesfully moved. |
331
|
|
|
* |
332
|
|
|
* @param string $path |
333
|
|
|
* @param bool $override = false |
334
|
|
|
* @return bool true if the file is succesfully moved. |
335
|
|
|
*/ |
336
|
2 |
|
public function move($path, $override = false) |
337
|
|
|
{ |
338
|
2 |
|
if (!$this->exists()) { |
339
|
2 |
|
return false; |
340
|
|
|
} |
341
|
|
|
|
342
|
2 |
|
$file = new File($path); |
343
|
|
|
|
344
|
2 |
|
if (($file->exists() && !$override) || !rename($this->path, $file->getPath())) { |
345
|
1 |
|
return false; |
346
|
|
|
} |
347
|
|
|
|
348
|
2 |
|
$this->path = $file->getPath(); |
349
|
|
|
|
350
|
2 |
|
return true; |
351
|
|
|
} |
352
|
|
|
|
353
|
|
|
/** |
354
|
|
|
* Returns true if the file is succesfully renamed. |
355
|
|
|
* |
356
|
|
|
* @param string $file |
357
|
|
|
* @param bool $override = false |
358
|
|
|
* @return bool true if the file is succesfully renamed. |
359
|
|
|
*/ |
360
|
1 |
|
public function rename($file, $override = false) |
361
|
|
|
{ |
362
|
1 |
|
return $this->move($this->getDirectory() . static::DIRECTORY_SEPARATOR . basename($file), $override); |
363
|
|
|
} |
364
|
|
|
|
365
|
|
|
/** |
366
|
|
|
* Returns true if the directory is succesfully removed. |
367
|
|
|
* |
368
|
|
|
* @param bool $recursive = false |
369
|
|
|
* @return bool true if the directory is succesfully removed. |
370
|
|
|
*/ |
371
|
1 |
|
public function removeDirectory($recursive = false) |
372
|
|
|
{ |
373
|
1 |
|
if (!$recursive) { |
374
|
1 |
|
return rmdir($this->path); |
375
|
|
|
} |
376
|
|
|
|
377
|
1 |
|
foreach (new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($this->path, \FilesystemIterator::SKIP_DOTS), \RecursiveIteratorIterator::CHILD_FIRST) as $path) { |
378
|
1 |
|
$path->isFile() ? unlink($path->getPathname()) : rmdir($path->getPathname()); |
379
|
|
|
} |
380
|
|
|
|
381
|
1 |
|
return true; |
382
|
|
|
} |
383
|
|
|
|
384
|
|
|
/** |
385
|
|
|
* Returns true if the file is succesfully removed. |
386
|
|
|
* |
387
|
|
|
* @return bool true if the file is succesfully removed. |
388
|
|
|
*/ |
389
|
1 |
|
public function removeFile() |
390
|
|
|
{ |
391
|
1 |
|
if (!$this->isFile()) { |
392
|
1 |
|
return false; |
393
|
|
|
} |
394
|
|
|
|
395
|
1 |
|
return unlink($this->path); |
396
|
|
|
} |
397
|
|
|
|
398
|
|
|
/** |
399
|
|
|
* Returns the content of the file. |
400
|
|
|
* |
401
|
|
|
* @return string the content of the file. |
402
|
|
|
* @throws FileException on failure. |
403
|
|
|
*/ |
404
|
7 |
|
public function read() |
405
|
|
|
{ |
406
|
7 |
|
$result = file_get_contents($this->path); |
407
|
|
|
|
408
|
7 |
|
if ($result === false) { |
409
|
2 |
|
throw new FileException('Can\'t read the content.'); |
410
|
|
|
} |
411
|
|
|
|
412
|
5 |
|
return $result; |
413
|
|
|
} |
414
|
|
|
|
415
|
|
|
/** |
416
|
|
|
* Append the given content. |
417
|
|
|
* |
418
|
|
|
* @param string $content |
419
|
|
|
* @return null |
420
|
|
|
* @throws FileException on failure. |
421
|
|
|
*/ |
422
|
3 |
|
public function append($content) |
423
|
|
|
{ |
424
|
3 |
|
if (file_put_contents($this->path, $content, \FILE_APPEND) === false) { |
425
|
1 |
|
throw new FileException('Can\'t append the given content.'); |
426
|
|
|
} |
427
|
2 |
|
} |
428
|
|
|
|
429
|
|
|
/** |
430
|
|
|
* Write the given content. |
431
|
|
|
* |
432
|
|
|
* @param string $content |
433
|
|
|
* @return null |
434
|
|
|
* @throws FileException on failure. |
435
|
|
|
*/ |
436
|
4 |
|
public function write($content) |
437
|
|
|
{ |
438
|
4 |
|
if (file_put_contents($this->path, $content) === false) { |
439
|
1 |
|
throw new FileException('Can\'t write the given content.'); |
440
|
|
|
} |
441
|
3 |
|
} |
442
|
|
|
} |
443
|
|
|
|