1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Hgraca\FileSystem; |
4
|
|
|
|
5
|
|
|
use DirectoryIterator; |
6
|
|
|
use RecursiveDirectoryIterator; |
7
|
|
|
use RecursiveIteratorIterator; |
8
|
|
|
|
9
|
|
|
class LocalFileSystem extends FileSystemAbstract |
10
|
|
|
{ |
11
|
70 |
|
protected function dirExistsRaw(string $path): bool |
12
|
|
|
{ |
13
|
70 |
|
return file_exists($path) && is_dir($path); |
14
|
|
|
} |
15
|
|
|
|
16
|
35 |
|
protected function linkExistsRaw(string $path): bool |
17
|
|
|
{ |
18
|
35 |
|
return file_exists($path) && is_link($path); |
19
|
|
|
} |
20
|
|
|
|
21
|
38 |
|
protected function fileExistsRaw(string $path): bool |
22
|
|
|
{ |
23
|
38 |
|
return file_exists($path) && is_file($path); |
24
|
|
|
} |
25
|
|
|
|
26
|
1 |
|
protected function getFileCreationTimestampRaw(string $path): int |
27
|
|
|
{ |
28
|
1 |
|
return filectime($path); |
29
|
|
|
} |
30
|
|
|
|
31
|
8 |
|
protected function readFileRaw(string $path): string |
32
|
|
|
{ |
33
|
8 |
|
return file_get_contents($path); |
34
|
|
|
} |
35
|
|
|
|
36
|
4 |
|
protected function writeFileRaw(string $path, string $content) |
37
|
|
|
{ |
38
|
4 |
|
file_put_contents($path, $content, LOCK_EX); |
39
|
4 |
|
} |
40
|
|
|
|
41
|
4 |
|
protected function copyFileRaw(string $sourcePath, string $destinationPath) |
42
|
|
|
{ |
43
|
4 |
|
copy($sourcePath, $destinationPath); |
44
|
4 |
|
} |
45
|
|
|
|
46
|
2 |
|
protected function deleteFileRaw(string $path) |
47
|
|
|
{ |
48
|
2 |
|
unlink($path); |
49
|
2 |
|
} |
50
|
|
|
|
51
|
3 |
|
protected function createLinkRaw(string $path, string $targetPath) |
52
|
|
|
{ |
53
|
3 |
|
symlink($targetPath, $path); |
54
|
3 |
|
} |
55
|
|
|
|
56
|
7 |
|
protected function getLinkTargetRaw(string $path): string |
57
|
|
|
{ |
58
|
7 |
|
return readlink($path); |
59
|
|
|
} |
60
|
|
|
|
61
|
5 |
|
public function createDirRaw(string $path) |
62
|
|
|
{ |
63
|
5 |
|
$oldUmask = umask(0); |
64
|
5 |
|
mkdir($path, 0777, true); |
65
|
5 |
|
umask($oldUmask); |
66
|
5 |
|
} |
67
|
|
|
|
68
|
70 |
|
public function deleteDirRaw(string $path) |
69
|
|
|
{ |
70
|
70 |
|
$it = new RecursiveDirectoryIterator($path, RecursiveDirectoryIterator::SKIP_DOTS); |
71
|
70 |
|
$files = new RecursiveIteratorIterator($it, RecursiveIteratorIterator::CHILD_FIRST); |
72
|
|
|
|
73
|
70 |
|
foreach ($files as $file) { |
74
|
70 |
|
if ($file->isDir()) { |
75
|
70 |
|
rmdir($file->getRealPath()); |
76
|
|
|
} else { |
77
|
70 |
|
unlink($file->getPathname()); |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
81
|
70 |
|
rmdir($path); |
82
|
70 |
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @return string[] The array with the file and dir names |
86
|
|
|
*/ |
87
|
5 |
|
public function readDirRaw(string $path): array |
88
|
|
|
{ |
89
|
5 |
|
$iterator = new DirectoryIterator($path); |
90
|
|
|
|
91
|
5 |
|
$contents = []; |
92
|
5 |
|
foreach ($iterator as $fileInfo) { |
93
|
5 |
|
$contents[] = $fileInfo->getFilename(); |
94
|
|
|
} |
95
|
|
|
|
96
|
5 |
|
return $contents; |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|