1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by PhpStorm. |
4
|
|
|
* User: yiranzai |
5
|
|
|
* Date: 19-3-6 |
6
|
|
|
* Time: 下午6:32 |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Yiranzai\Dht; |
10
|
|
|
|
11
|
|
|
use FilesystemIterator; |
12
|
|
|
|
13
|
|
|
class Filesystem |
14
|
|
|
{ |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Determine if a file or directory exists. |
18
|
|
|
* |
19
|
|
|
* @param string $path |
20
|
|
|
* @return bool |
21
|
|
|
*/ |
22
|
33 |
|
public function exists($path): bool |
23
|
|
|
{ |
24
|
33 |
|
return file_exists($path); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Get the contents of a file. |
29
|
|
|
* |
30
|
|
|
* @param string $path |
31
|
|
|
* @param bool $lock |
32
|
|
|
* @return string |
33
|
|
|
*/ |
34
|
9 |
|
public function get($path, $lock = false): string |
35
|
|
|
{ |
36
|
9 |
|
if ($this->isFile($path)) { |
37
|
6 |
|
return $lock ? $this->sharedGet($path) : file_get_contents($path); |
38
|
|
|
} |
39
|
|
|
|
40
|
3 |
|
throw new \RuntimeException("File does not exist at path {$path}"); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Get contents of a file with shared access. |
45
|
|
|
* |
46
|
|
|
* @param string $path |
47
|
|
|
* @return string |
48
|
|
|
*/ |
49
|
3 |
|
public function sharedGet($path): string |
50
|
|
|
{ |
51
|
3 |
|
$contents = ''; |
52
|
|
|
|
53
|
3 |
|
$handle = fopen($path, 'rb'); |
54
|
|
|
|
55
|
3 |
|
if ($handle !== false) { |
56
|
|
|
try { |
57
|
3 |
|
if (flock($handle, LOCK_SH)) { |
58
|
3 |
|
clearstatcache(true, $path); |
59
|
|
|
|
60
|
3 |
|
$contents = fread($handle, $this->size($path) ?: 1); |
61
|
|
|
|
62
|
3 |
|
flock($handle, LOCK_UN); |
63
|
|
|
} |
64
|
3 |
|
} finally { |
65
|
3 |
|
fclose($handle); |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|
69
|
3 |
|
return $contents; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Get the MD5 hash of the file at the given path. |
74
|
|
|
* |
75
|
|
|
* @param string $path |
76
|
|
|
* @return string |
77
|
|
|
*/ |
78
|
3 |
|
public function hash($path): string |
79
|
|
|
{ |
80
|
3 |
|
return md5_file($path); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Write the contents of a file. |
85
|
|
|
* |
86
|
|
|
* @param string $path |
87
|
|
|
* @param string $contents |
88
|
|
|
* @param bool $lock |
89
|
|
|
* @return int |
90
|
|
|
*/ |
91
|
6 |
|
public function put($path, $contents, $lock = false): int |
92
|
|
|
{ |
93
|
6 |
|
return file_put_contents($path, $contents, $lock ? LOCK_EX : 0); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Prepend to a file. |
98
|
|
|
* |
99
|
|
|
* @param string $path |
100
|
|
|
* @param string $data |
101
|
|
|
* @return int |
102
|
|
|
*/ |
103
|
3 |
|
public function prepend($path, $data): int |
104
|
|
|
{ |
105
|
3 |
|
if ($this->exists($path)) { |
106
|
3 |
|
return $this->put($path, $data . $this->get($path)); |
107
|
|
|
} |
108
|
|
|
|
109
|
3 |
|
return $this->put($path, $data); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Append to a file. |
114
|
|
|
* |
115
|
|
|
* @param string $path |
116
|
|
|
* @param string $data |
117
|
|
|
* @return int |
118
|
|
|
*/ |
119
|
3 |
|
public function append($path, $data): int |
120
|
|
|
{ |
121
|
3 |
|
return file_put_contents($path, $data, FILE_APPEND); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Get or set UNIX mode of a file or directory. |
126
|
|
|
* |
127
|
|
|
* @param string $path |
128
|
|
|
* @param int $mode |
129
|
|
|
* @return mixed |
130
|
|
|
*/ |
131
|
9 |
|
public function chmod($path, $mode = null) |
132
|
|
|
{ |
133
|
9 |
|
if ($mode !== null) { |
134
|
9 |
|
return chmod($path, $mode); |
135
|
|
|
} |
136
|
|
|
|
137
|
3 |
|
return substr(sprintf('%o', fileperms($path)), -4); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Delete the file at a given path. |
142
|
|
|
* |
143
|
|
|
* @param string|array $paths |
144
|
|
|
* @return bool |
145
|
|
|
*/ |
146
|
6 |
|
public function delete($paths): bool |
147
|
|
|
{ |
148
|
6 |
|
$paths = is_array($paths) ? $paths : func_get_args(); |
149
|
|
|
|
150
|
6 |
|
$success = true; |
151
|
|
|
|
152
|
6 |
|
foreach ($paths as $path) { |
153
|
|
|
try { |
154
|
6 |
|
if (!@unlink($path)) { |
155
|
6 |
|
$success = false; |
156
|
|
|
} |
157
|
|
|
} catch (\Exception $e) { |
158
|
2 |
|
$success = false; |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
|
162
|
6 |
|
return $success; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Move a file to a new location. |
167
|
|
|
* |
168
|
|
|
* @param string $path |
169
|
|
|
* @param string $target |
170
|
|
|
* @return bool |
171
|
|
|
*/ |
172
|
3 |
|
public function move($path, $target): bool |
173
|
|
|
{ |
174
|
3 |
|
return rename($path, $target); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* Copy a file to a new location. |
179
|
|
|
* |
180
|
|
|
* @param string $path |
181
|
|
|
* @param string $target |
182
|
|
|
* @return bool |
183
|
|
|
*/ |
184
|
3 |
|
public function copy($path, $target): bool |
185
|
|
|
{ |
186
|
3 |
|
return copy($path, $target); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* Extract the file name from a file path. |
191
|
|
|
* |
192
|
|
|
* @param string $path |
193
|
|
|
* @return string |
194
|
|
|
*/ |
195
|
3 |
|
public function name($path): string |
196
|
|
|
{ |
197
|
3 |
|
return pathinfo($path, PATHINFO_FILENAME); |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* Extract the trailing name component from a file path. |
202
|
|
|
* |
203
|
|
|
* @param string $path |
204
|
|
|
* @return string |
205
|
|
|
*/ |
206
|
3 |
|
public function basename($path): string |
207
|
|
|
{ |
208
|
3 |
|
return pathinfo($path, PATHINFO_BASENAME); |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* Extract the parent directory from a file path. |
213
|
|
|
* |
214
|
|
|
* @param string $path |
215
|
|
|
* @return string |
216
|
|
|
*/ |
217
|
3 |
|
public function dirname($path): string |
218
|
|
|
{ |
219
|
3 |
|
return pathinfo($path, PATHINFO_DIRNAME); |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* Extract the file extension from a file path. |
224
|
|
|
* |
225
|
|
|
* @param string $path |
226
|
|
|
* @return string |
227
|
|
|
*/ |
228
|
3 |
|
public function extension($path): string |
229
|
|
|
{ |
230
|
3 |
|
return pathinfo($path, PATHINFO_EXTENSION); |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
/** |
234
|
|
|
* Get the file type of a given file. |
235
|
|
|
* |
236
|
|
|
* @param string $path |
237
|
|
|
* @return string |
238
|
|
|
*/ |
239
|
3 |
|
public function type($path): string |
240
|
|
|
{ |
241
|
3 |
|
return filetype($path); |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
/** |
245
|
|
|
* Get the mime-type of a given file. |
246
|
|
|
* |
247
|
|
|
* @param string $path |
248
|
|
|
* @return string|false |
249
|
|
|
*/ |
250
|
3 |
|
public function mimeType($path) |
251
|
|
|
{ |
252
|
3 |
|
return finfo_file(finfo_open(FILEINFO_MIME_TYPE), $path); |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
/** |
256
|
|
|
* Get the file size of a given file. |
257
|
|
|
* |
258
|
|
|
* @param string $path |
259
|
|
|
* @return int |
260
|
|
|
*/ |
261
|
3 |
|
public function size($path): int |
262
|
|
|
{ |
263
|
3 |
|
return filesize($path); |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
/** |
267
|
|
|
* Get the file's last modification time. |
268
|
|
|
* |
269
|
|
|
* @param string $path |
270
|
|
|
* @return int |
271
|
|
|
*/ |
272
|
3 |
|
public function lastModified($path): int |
273
|
|
|
{ |
274
|
3 |
|
return filemtime($path); |
275
|
|
|
} |
276
|
|
|
|
277
|
|
|
/** |
278
|
|
|
* Determine if the given path is a directory. |
279
|
|
|
* |
280
|
|
|
* @param string $directory |
281
|
|
|
* @return bool |
282
|
|
|
*/ |
283
|
12 |
|
public function isDirectory($directory): bool |
284
|
|
|
{ |
285
|
12 |
|
return is_dir($directory); |
286
|
|
|
} |
287
|
|
|
|
288
|
|
|
/** |
289
|
|
|
* Determine if the given path is readable. |
290
|
|
|
* |
291
|
|
|
* @param string $path |
292
|
|
|
* @return bool |
293
|
|
|
*/ |
294
|
9 |
|
public function isReadable($path): bool |
295
|
|
|
{ |
296
|
9 |
|
return is_readable($path); |
297
|
|
|
} |
298
|
|
|
|
299
|
|
|
/** |
300
|
|
|
* Determine if the given path is writable. |
301
|
|
|
* |
302
|
|
|
* @param string $path |
303
|
|
|
* @return bool |
304
|
|
|
*/ |
305
|
9 |
|
public function isWritable($path): bool |
306
|
|
|
{ |
307
|
9 |
|
return is_writable($path); |
308
|
|
|
} |
309
|
|
|
|
310
|
|
|
/** |
311
|
|
|
* Determine if the given path is a file. |
312
|
|
|
* |
313
|
|
|
* @param string $file |
314
|
|
|
* @return bool |
315
|
|
|
*/ |
316
|
9 |
|
public function isFile($file): bool |
317
|
|
|
{ |
318
|
9 |
|
return is_file($file); |
319
|
|
|
} |
320
|
|
|
|
321
|
|
|
/** |
322
|
|
|
* Create a directory. |
323
|
|
|
* |
324
|
|
|
* @param string $path |
325
|
|
|
* @param int $mode |
326
|
|
|
* @param bool $recursive |
327
|
|
|
* @param bool $force |
328
|
|
|
* @return bool |
329
|
|
|
*/ |
330
|
12 |
|
public function makeDirectory($path, $mode = 0755, $recursive = false, $force = false): bool |
331
|
|
|
{ |
332
|
12 |
|
if ($force) { |
333
|
3 |
|
return @mkdir($path, $mode, $recursive); |
334
|
|
|
} |
335
|
|
|
|
336
|
12 |
|
return mkdir($path, $mode, $recursive); |
337
|
|
|
} |
338
|
|
|
|
339
|
|
|
/** |
340
|
|
|
* Move a directory. |
341
|
|
|
* |
342
|
|
|
* @param string $from |
343
|
|
|
* @param string $to |
344
|
|
|
* @param bool $overwrite |
345
|
|
|
* @return bool |
346
|
|
|
*/ |
347
|
6 |
|
public function moveDirectory($from, $to, $overwrite = false): bool |
348
|
|
|
{ |
349
|
6 |
|
if ($overwrite && $this->isDirectory($to) && !$this->deleteDirectory($to)) { |
350
|
|
|
return false; |
351
|
|
|
} |
352
|
|
|
|
353
|
6 |
|
return @rename($from, $to) === true; |
354
|
|
|
} |
355
|
|
|
|
356
|
|
|
/** |
357
|
|
|
* Copy a directory from one location to another. |
358
|
|
|
* |
359
|
|
|
* @param string $directory |
360
|
|
|
* @param string $destination |
361
|
|
|
* @param int $options |
362
|
|
|
* @return bool |
363
|
|
|
*/ |
364
|
6 |
|
public function copyDirectory($directory, $destination, $options = null): bool |
365
|
|
|
{ |
366
|
6 |
|
if (!$this->isDirectory($directory)) { |
367
|
|
|
return false; |
368
|
|
|
} |
369
|
|
|
|
370
|
6 |
|
$options = $options ?: FilesystemIterator::SKIP_DOTS; |
371
|
|
|
|
372
|
6 |
|
if (!$this->isDirectory($destination)) { |
373
|
6 |
|
$this->makeDirectory($destination, 0777, true); |
374
|
|
|
} |
375
|
|
|
|
376
|
6 |
|
$items = new FilesystemIterator($directory, $options); |
377
|
|
|
|
378
|
6 |
|
foreach ($items as $item) { |
379
|
3 |
|
$target = $destination . '/' . $item->getBasename(); |
380
|
|
|
|
381
|
3 |
|
if (!$item->isDir()) { |
382
|
|
|
if (!$this->copy($item->getPathname(), $target)) { |
383
|
|
|
return false; |
384
|
|
|
} |
385
|
|
|
} else { |
386
|
3 |
|
$path = $item->getPathname(); |
387
|
|
|
|
388
|
3 |
|
if (!$this->copyDirectory($path, $target, $options)) { |
389
|
1 |
|
return false; |
390
|
|
|
} |
391
|
|
|
} |
392
|
|
|
} |
393
|
|
|
|
394
|
6 |
|
return true; |
395
|
|
|
} |
396
|
|
|
|
397
|
|
|
/** |
398
|
|
|
* Recursively delete a directory. |
399
|
|
|
* |
400
|
|
|
* The directory itself may be optionally preserved. |
401
|
|
|
* |
402
|
|
|
* @param string $directory |
403
|
|
|
* @param bool $preserve |
404
|
|
|
* @return bool |
405
|
|
|
*/ |
406
|
12 |
|
public function deleteDirectory($directory, $preserve = false): bool |
407
|
|
|
{ |
408
|
12 |
|
if (!$this->isDirectory($directory)) { |
409
|
3 |
|
return false; |
410
|
|
|
} |
411
|
|
|
|
412
|
12 |
|
$items = new FilesystemIterator($directory); |
413
|
|
|
|
414
|
12 |
|
foreach ($items as $item) { |
415
|
6 |
|
if ($item->isDir() && !$item->isLink()) { |
416
|
3 |
|
$this->deleteDirectory($item->getPathname()); |
417
|
|
|
} else { |
418
|
4 |
|
$this->delete($item->getPathname()); |
419
|
|
|
} |
420
|
|
|
} |
421
|
|
|
|
422
|
12 |
|
if (!$preserve) { |
423
|
12 |
|
/** @scrutinizer ignore-unhandled */ @rmdir($directory); |
424
|
|
|
} |
425
|
|
|
|
426
|
12 |
|
return true; |
427
|
|
|
} |
428
|
|
|
|
429
|
|
|
/** |
430
|
|
|
* Empty the specified directory of all files and folders. |
431
|
|
|
* |
432
|
|
|
* @param string $directory |
433
|
|
|
* @return bool |
434
|
|
|
*/ |
435
|
3 |
|
public function cleanDirectory($directory): bool |
436
|
|
|
{ |
437
|
3 |
|
return $this->deleteDirectory($directory, true); |
438
|
|
|
} |
439
|
|
|
|
440
|
|
|
/** |
441
|
|
|
* Determine whether the current environment is Windows based. |
442
|
|
|
* |
443
|
|
|
* @return bool |
444
|
|
|
*/ |
445
|
3 |
|
public function windowsOs(): bool |
446
|
|
|
{ |
447
|
3 |
|
return stripos(PHP_OS, 'win') === 0; |
448
|
|
|
} |
449
|
|
|
} |
450
|
|
|
|