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