Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
98.61% |
71 / 72 |
|
95.83% |
23 / 24 |
CRAP | |
0.00% |
0 / 1 |
Local | |
98.61% |
71 / 72 |
|
95.83% |
23 / 24 |
51 | |
0.00% |
0 / 1 |
mkdir | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
rmdir | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
listDirs | |
100.00% |
10 / 10 |
|
100.00% |
1 / 1 |
5 | |||
listFiles | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
6 | |||
putFile | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
3 | |||
putFileContents | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
uploadFile | |
80.00% |
4 / 5 |
|
0.00% |
0 / 1 |
4.13 | |||
copyFile | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
copyFileToExternal | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
copyFileFromExternal | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
moveFileToExternal | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
moveFileFromExternal | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
renameFile | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
replaceFileContents | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
deleteFile | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
fetchFile | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
2 | |||
fetchFileInfo | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
fileExists | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
isDir | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
isFile | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getFileSize | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
2 | |||
getFileType | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
2 | |||
getFileMTime | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
2 | |||
md5File | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
2 |
1 | <?php |
2 | /** |
3 | * Pop PHP Framework (http://www.popphp.org/) |
4 | * |
5 | * @link https://github.com/popphp/popphp-framework |
6 | * @author Nick Sagona, III <dev@nolainteractive.com> |
7 | * @copyright Copyright (c) 2009-2024 NOLA Interactive, LLC. (http://www.nolainteractive.com) |
8 | * @license http://www.popphp.org/license New BSD License |
9 | */ |
10 | |
11 | /** |
12 | * @namespace |
13 | */ |
14 | namespace Pop\Storage\Adapter; |
15 | |
16 | use Pop\Dir\Dir; |
17 | use Pop\Utils\File; |
18 | |
19 | /** |
20 | * Storage adapter local class |
21 | * |
22 | * @category Pop |
23 | * @package Pop\Storage |
24 | * @author Nick Sagona, III <dev@nolainteractive.com> |
25 | * @copyright Copyright (c) 2009-2024 NOLA Interactive, LLC. (http://www.nolainteractive.com) |
26 | * @license http://www.popphp.org/license New BSD License |
27 | * @version 2.0.0 |
28 | */ |
29 | class Local extends AbstractAdapter |
30 | { |
31 | |
32 | /** |
33 | * Make directory |
34 | * |
35 | * @param string $directory |
36 | * @return void |
37 | */ |
38 | public function mkdir(string $directory): void |
39 | { |
40 | mkdir($this->directory . DIRECTORY_SEPARATOR . $this->scrub($directory)); |
41 | } |
42 | |
43 | /** |
44 | * Remove a directory |
45 | * |
46 | * @param string $directory |
47 | * @throws \Pop\Dir\Exception |
48 | * @return void |
49 | */ |
50 | public function rmdir(string $directory): void |
51 | { |
52 | $dir = new Dir($this->directory . DIRECTORY_SEPARATOR . $this->scrub($directory)); |
53 | $dir->emptyDir(true); |
54 | } |
55 | |
56 | /** |
57 | * List directories |
58 | * |
59 | * @param ?string $search |
60 | * @return array |
61 | */ |
62 | public function listDirs(?string $search = null): array |
63 | { |
64 | $directory = $this->directory; |
65 | $directories = array_map(function($value) { |
66 | return $value. DIRECTORY_SEPARATOR; |
67 | }, array_values(array_filter(scandir($directory), function($value) use ($directory) { |
68 | return (($value != '.') && ($value != '..') && file_exists($directory . DIRECTORY_SEPARATOR . $value) && |
69 | is_dir($directory . DIRECTORY_SEPARATOR . $value)); |
70 | }))); |
71 | |
72 | if ($search !== null) { |
73 | $directories = $this->searchFilter($directories, $search); |
74 | } |
75 | |
76 | return $directories; |
77 | } |
78 | |
79 | /** |
80 | * List files |
81 | * |
82 | * @param ?string $search |
83 | * @return array |
84 | */ |
85 | public function listFiles(?string $search = null): array |
86 | { |
87 | $directory = $this->directory; |
88 | $files = array_values(array_filter(scandir($directory), function($value) use ($directory) { |
89 | return (($value != '.') && ($value != '..') && file_exists($directory . DIRECTORY_SEPARATOR . $value) && |
90 | !is_dir($directory . DIRECTORY_SEPARATOR . $value) && is_file($directory . DIRECTORY_SEPARATOR . $value)); |
91 | })); |
92 | |
93 | if ($search !== null) { |
94 | $files = $this->searchFilter($files, $search); |
95 | } |
96 | |
97 | return $files; |
98 | } |
99 | |
100 | /** |
101 | * Put file |
102 | * |
103 | * @param string $fileFrom |
104 | * @param bool $copy |
105 | * @return void |
106 | */ |
107 | public function putFile(string $fileFrom, bool $copy = true): void |
108 | { |
109 | if (file_exists($fileFrom)) { |
110 | if ($copy) { |
111 | copy($fileFrom, $this->directory . DIRECTORY_SEPARATOR . basename($fileFrom)); |
112 | } else { |
113 | rename($fileFrom, $this->directory . DIRECTORY_SEPARATOR . basename($fileFrom)); |
114 | } |
115 | } |
116 | } |
117 | |
118 | /** |
119 | * Put file contents |
120 | * |
121 | * @param string $filename |
122 | * @param string $fileContents |
123 | * @return void |
124 | */ |
125 | public function putFileContents(string $filename, string $fileContents): void |
126 | { |
127 | file_put_contents($this->directory . DIRECTORY_SEPARATOR . $this->scrub($filename), $fileContents); |
128 | } |
129 | |
130 | /** |
131 | * Upload file from server request $_FILES['files'] |
132 | * |
133 | * @param array $file |
134 | * @throws Exception |
135 | * @return void |
136 | */ |
137 | public function uploadFile(array $file): void |
138 | { |
139 | if (!isset($file['tmp_name']) || !isset($file['name'])) { |
140 | throw new Exception('Error: The uploaded file array was not valid'); |
141 | } |
142 | |
143 | if (is_uploaded_file($file['tmp_name'])) { |
144 | move_uploaded_file($file['tmp_name'], $this->directory . DIRECTORY_SEPARATOR . $file['name']); |
145 | } else { |
146 | rename($file['tmp_name'], $this->directory . DIRECTORY_SEPARATOR . $file['name']); |
147 | } |
148 | } |
149 | |
150 | /** |
151 | * Copy file |
152 | * |
153 | * @param string $sourceFile |
154 | * @param string $destFile |
155 | * @return void |
156 | */ |
157 | public function copyFile(string $sourceFile, string $destFile): void |
158 | { |
159 | $sourceFile = $this->directory . DIRECTORY_SEPARATOR . $this->scrub($sourceFile); |
160 | $destFile = $this->directory . DIRECTORY_SEPARATOR . $this->scrub($destFile); |
161 | if (file_exists($sourceFile)) { |
162 | copy($sourceFile, $destFile); |
163 | } |
164 | } |
165 | |
166 | /** |
167 | * Copy file to a location external to the current location |
168 | * |
169 | * @param string $sourceFile |
170 | * @param string $externalFile |
171 | * @return void |
172 | */ |
173 | public function copyFileToExternal(string $sourceFile, string $externalFile): void |
174 | { |
175 | $sourceFile = $this->directory . DIRECTORY_SEPARATOR . $this->scrub($sourceFile); |
176 | if (file_exists($sourceFile)) { |
177 | copy($sourceFile, $externalFile); |
178 | } |
179 | } |
180 | |
181 | /** |
182 | * Copy file from a location external to the current location |
183 | * |
184 | * @param string $externalFile |
185 | * @param string $destFile |
186 | * @return void |
187 | */ |
188 | public function copyFileFromExternal(string $externalFile, string $destFile): void |
189 | { |
190 | $destFile = $this->directory . DIRECTORY_SEPARATOR . $this->scrub($destFile); |
191 | if (file_exists($externalFile)) { |
192 | copy($externalFile, $destFile); |
193 | } |
194 | } |
195 | |
196 | /** |
197 | * Move file to a location external to the current location |
198 | * |
199 | * @param string $sourceFile |
200 | * @param string $externalFile |
201 | * @return void |
202 | */ |
203 | public function moveFileToExternal(string $sourceFile, string $externalFile): void |
204 | { |
205 | $sourceFile = $this->directory . DIRECTORY_SEPARATOR . $this->scrub($sourceFile); |
206 | if (file_exists($sourceFile)) { |
207 | rename($sourceFile, $externalFile); |
208 | } |
209 | } |
210 | |
211 | /** |
212 | * Move file from a location external to the current location |
213 | * |
214 | * @param string $externalFile |
215 | * @param string $destFile |
216 | * @return void |
217 | */ |
218 | public function moveFileFromExternal(string $externalFile, string $destFile): void |
219 | { |
220 | $destFile = $this->directory . DIRECTORY_SEPARATOR . $this->scrub($destFile); |
221 | if (file_exists($externalFile)) { |
222 | rename($externalFile, $destFile); |
223 | } |
224 | } |
225 | |
226 | /** |
227 | * Rename file |
228 | * |
229 | * @param string $oldFile |
230 | * @param string $newFile |
231 | * @return void |
232 | */ |
233 | public function renameFile(string $oldFile, string $newFile): void |
234 | { |
235 | $oldFile = $this->directory . DIRECTORY_SEPARATOR . $this->scrub($oldFile); |
236 | $newFile = $this->directory . DIRECTORY_SEPARATOR . $this->scrub($newFile); |
237 | if (file_exists($oldFile)) { |
238 | rename($oldFile, $newFile); |
239 | } |
240 | } |
241 | |
242 | /** |
243 | * Replace file |
244 | * |
245 | * @param string $filename |
246 | * @param string $fileContents |
247 | * @return void |
248 | */ |
249 | public function replaceFileContents(string $filename, string $fileContents): void |
250 | { |
251 | $filename = $this->directory . DIRECTORY_SEPARATOR . $this->scrub($filename); |
252 | if (file_exists($filename)) { |
253 | file_put_contents($filename, $fileContents); |
254 | } |
255 | } |
256 | |
257 | /** |
258 | * Delete file |
259 | * |
260 | * @param string $filename |
261 | * @return void |
262 | */ |
263 | public function deleteFile(string $filename): void |
264 | { |
265 | $filename = $this->directory . DIRECTORY_SEPARATOR . $this->scrub($filename); |
266 | if (file_exists($filename)) { |
267 | unlink($filename); |
268 | } |
269 | } |
270 | |
271 | /** |
272 | * Fetch file |
273 | * |
274 | * @param string $filename |
275 | * @return mixed |
276 | */ |
277 | public function fetchFile(string $filename): mixed |
278 | { |
279 | $filename = $this->directory . DIRECTORY_SEPARATOR . $this->scrub($filename); |
280 | return (file_exists($filename)) ? file_get_contents($filename) : false; |
281 | } |
282 | |
283 | /** |
284 | * Fetch file info |
285 | * |
286 | * @param string $filename |
287 | * @return array |
288 | */ |
289 | public function fetchFileInfo(string $filename): array |
290 | { |
291 | $filename = $this->directory . DIRECTORY_SEPARATOR . $this->scrub($filename); |
292 | return (new File($filename))->toArray(); |
293 | } |
294 | |
295 | /** |
296 | * File exists |
297 | * |
298 | * @param string $filename |
299 | * @return bool |
300 | */ |
301 | public function fileExists(string $filename): bool |
302 | { |
303 | return file_exists($this->directory . DIRECTORY_SEPARATOR . $this->scrub($filename)); |
304 | } |
305 | |
306 | /** |
307 | * Check if is a dir |
308 | * |
309 | * @param string $directory |
310 | * @return bool |
311 | */ |
312 | public function isDir(string $directory): bool |
313 | { |
314 | return is_dir($this->directory . DIRECTORY_SEPARATOR . $this->scrub($directory)); |
315 | } |
316 | |
317 | /** |
318 | * Check if is a file |
319 | * |
320 | * @param string $filename |
321 | * @return bool |
322 | */ |
323 | public function isFile(string $filename): bool |
324 | { |
325 | return is_file($this->directory . DIRECTORY_SEPARATOR . $this->scrub($filename)); |
326 | } |
327 | |
328 | /** |
329 | * Get file size |
330 | * |
331 | * @param string $filename |
332 | * @return int|bool |
333 | */ |
334 | public function getFileSize(string $filename): int|bool |
335 | { |
336 | $filename = $this->directory . DIRECTORY_SEPARATOR . $this->scrub($filename); |
337 | return (file_exists($filename)) ? filesize($filename) : false; |
338 | } |
339 | |
340 | /** |
341 | * Get file type |
342 | * |
343 | * @param string $filename |
344 | * @return string|bool |
345 | */ |
346 | public function getFileType(string $filename): string|bool |
347 | { |
348 | $filename = $this->directory . DIRECTORY_SEPARATOR . $this->scrub($filename); |
349 | return (file_exists($filename)) ? filetype($filename) : false; |
350 | } |
351 | |
352 | /** |
353 | * Get file modified time |
354 | * |
355 | * @param string $filename |
356 | * @return int|string|bool |
357 | */ |
358 | public function getFileMTime(string $filename): int|string|bool |
359 | { |
360 | $filename = $this->directory . DIRECTORY_SEPARATOR . $this->scrub($filename); |
361 | return (file_exists($filename)) ? filemtime($filename) : false; |
362 | } |
363 | |
364 | /** |
365 | * Create MD5 checksum of the file |
366 | * |
367 | * @param string $filename |
368 | * @return string|bool |
369 | */ |
370 | public function md5File(string $filename): string|bool |
371 | { |
372 | $filename = $this->directory . DIRECTORY_SEPARATOR . $this->scrub($filename); |
373 | return (file_exists($filename)) ? md5_file($filename) : false; |
374 | } |
375 | |
376 | } |