1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
/** |
3
|
|
|
* Copyright MediaCT. All rights reserved. |
4
|
|
|
* https://www.mediact.nl |
5
|
|
|
*/ |
6
|
|
|
|
7
|
|
|
namespace Mediact\CodingStandard\PhpStorm; |
8
|
|
|
|
9
|
|
|
use RecursiveDirectoryIterator; |
10
|
|
|
use RecursiveIteratorIterator; |
11
|
|
|
use RuntimeException; |
12
|
|
|
use SplFileInfo; |
13
|
|
|
|
14
|
|
|
class Filesystem implements FilesystemInterface |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var string |
18
|
|
|
*/ |
19
|
|
|
private $root = ''; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Constructor. |
23
|
|
|
* |
24
|
|
|
* @param string $root |
25
|
|
|
*/ |
26
|
1 |
|
public function __construct($root) |
27
|
|
|
{ |
28
|
1 |
|
if (! empty($root)) { |
29
|
1 |
|
$this->root = rtrim($root, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR; |
30
|
|
|
} |
31
|
1 |
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Check whether a file exists. |
35
|
|
|
* |
36
|
|
|
* @param string $path |
37
|
|
|
* |
38
|
|
|
* @return bool |
39
|
|
|
*/ |
40
|
1 |
|
public function has(string $path): bool |
41
|
|
|
{ |
42
|
1 |
|
return file_exists($this->getPath($path)); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Read a path. |
47
|
|
|
* |
48
|
|
|
* @param string $path |
49
|
|
|
* |
50
|
|
|
* @return string |
51
|
|
|
* @throws RuntimeException When the path is not readable. |
52
|
|
|
*/ |
53
|
3 |
|
public function read(string $path): string |
54
|
|
|
{ |
55
|
3 |
|
$path = $this->getPath($path); |
56
|
3 |
|
if (!is_readable($path) || !is_file($path)) { |
57
|
2 |
|
throw new RuntimeException($path . ' is not readable file'); |
58
|
|
|
} |
59
|
|
|
|
60
|
1 |
|
return file_get_contents($path); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Write contents to a path. |
65
|
|
|
* |
66
|
|
|
* @param string $path |
67
|
|
|
* @param string $contents |
68
|
|
|
* |
69
|
|
|
* @return bool |
70
|
|
|
* @throws RuntimeException When the path is not writable. |
71
|
|
|
*/ |
72
|
3 |
|
public function put(string $path, string $contents): bool |
73
|
|
|
{ |
74
|
3 |
|
$directory = dirname($path); |
75
|
3 |
|
$this->createDir($directory); |
76
|
3 |
|
$path = $this->getPath($path); |
77
|
3 |
|
if (!file_exists($path) && !is_writable(dirname($path))) { |
78
|
1 |
|
throw new RuntimeException(dirname($path) . ' is not writable'); |
79
|
|
|
} |
80
|
|
|
|
81
|
2 |
|
if (file_exists($path) && !is_writable($path)) { |
82
|
1 |
|
throw new RuntimeException($path . ' is not writable'); |
83
|
|
|
} |
84
|
|
|
|
85
|
1 |
|
return (bool) file_put_contents($path, $contents); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Create a directory if it does not exist. |
90
|
|
|
* |
91
|
|
|
* @param string $path |
92
|
|
|
* |
93
|
|
|
* @return bool |
94
|
|
|
* @throws RuntimeException When the directory can not be created. |
95
|
|
|
*/ |
96
|
3 |
|
public function createDir(string $path): bool |
97
|
|
|
{ |
98
|
3 |
|
$directory = $this->getPath($path); |
99
|
3 |
|
if (!is_dir($directory)) { |
100
|
3 |
|
if (file_exists($directory)) { |
101
|
1 |
|
throw new RuntimeException( |
102
|
1 |
|
$directory . ' is not a directory.' |
103
|
|
|
); |
104
|
|
|
} |
105
|
|
|
|
106
|
2 |
|
if (!mkdir($directory, 0777, true)) { |
107
|
1 |
|
throw new RuntimeException( |
108
|
1 |
|
$directory . ' can not be created.' |
109
|
|
|
); |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|
113
|
1 |
|
return true; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* List contents of a directory. |
118
|
|
|
* |
119
|
|
|
* @param string $path |
120
|
|
|
* |
121
|
|
|
* @return array |
122
|
|
|
* @throws RuntimeException When the path is not a directory. |
123
|
|
|
*/ |
124
|
2 |
|
public function listFiles(string $path = ''): array |
125
|
|
|
{ |
126
|
2 |
|
$directory = $this->getPath($path); |
127
|
2 |
|
if (!is_dir($directory)) { |
128
|
1 |
|
throw new RuntimeException( |
129
|
1 |
|
$directory . ' is not a directory.' |
130
|
|
|
); |
131
|
|
|
} |
132
|
|
|
|
133
|
1 |
|
$iterator = new RecursiveIteratorIterator( |
134
|
1 |
|
new RecursiveDirectoryIterator( |
135
|
1 |
|
$this->getPath($path), |
136
|
1 |
|
RecursiveDirectoryIterator::SKIP_DOTS |
137
|
|
|
), |
138
|
1 |
|
RecursiveIteratorIterator::SELF_FIRST |
139
|
|
|
); |
140
|
|
|
|
141
|
1 |
|
$files = []; |
142
|
|
|
|
143
|
|
|
/** @var SplFileInfo $fileInfo */ |
144
|
1 |
|
foreach ($iterator as $fileInfo) { |
145
|
1 |
|
if ($fileInfo->isDir()) { |
146
|
1 |
|
continue; |
147
|
|
|
} |
148
|
|
|
|
149
|
1 |
|
$files[] = preg_replace( |
150
|
1 |
|
sprintf('/^%s/', preg_quote($this->root, '/')), |
151
|
1 |
|
'', |
152
|
1 |
|
$fileInfo->getPathname() |
153
|
|
|
); |
154
|
|
|
} |
155
|
|
|
|
156
|
1 |
|
return $files; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Get the full path. |
161
|
|
|
* |
162
|
|
|
* @param string $path |
163
|
|
|
* |
164
|
|
|
* @return string |
165
|
|
|
*/ |
166
|
1 |
|
private function getPath(string $path): string |
167
|
|
|
{ |
168
|
1 |
|
return $this->root . |
169
|
1 |
|
ltrim( |
170
|
1 |
|
preg_replace( |
171
|
1 |
|
sprintf('|%s{2,}|', preg_quote(DIRECTORY_SEPARATOR)), |
172
|
1 |
|
DIRECTORY_SEPARATOR, |
173
|
1 |
|
$path |
174
|
|
|
), |
175
|
1 |
|
DIRECTORY_SEPARATOR |
176
|
|
|
); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* Get root |
181
|
|
|
* |
182
|
|
|
* @return string |
183
|
|
|
*/ |
184
|
|
|
public function getRoot(): string |
185
|
|
|
{ |
186
|
|
|
return $this->root; |
187
|
|
|
} |
188
|
|
|
} |
189
|
|
|
|