ArrayUtil.php
3 years ago
Config.php
3 years ago
ConfigCollection.php
3 years ago
ConfigCollectionFactory.php
3 years ago
ConfigCollectionInterface.php
3 years ago
ConfigFactory.php
3 years ago
ConfigInterface.php
3 years ago
Filesystem.php
3 years ago
FilesystemInterface.php
3 years ago
Imposter.php
3 years ago
ImposterFactory.php
3 years ago
ImposterInterface.php
3 years ago
ProjectConfig.php
3 years ago
ProjectConfigInterface.php
3 years ago
StringUtil.php
3 years ago
Transformer.php
3 years ago
TransformerInterface.php
3 years ago
Filesystem.php
95 lines
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace TypistTech\Imposter; |
| 6 | |
| 7 | use FilesystemIterator; |
| 8 | use RecursiveDirectoryIterator; |
| 9 | use RecursiveIteratorIterator; |
| 10 | use RuntimeException; |
| 11 | |
| 12 | class Filesystem implements FilesystemInterface |
| 13 | { |
| 14 | /** |
| 15 | * @param string $path |
| 16 | * |
| 17 | * @return \SplFileInfo[] |
| 18 | * @throws \UnexpectedValueException |
| 19 | */ |
| 20 | public function allFiles(string $path): array |
| 21 | { |
| 22 | $iterator = new RecursiveIteratorIterator( |
| 23 | new RecursiveDirectoryIterator($path, FilesystemIterator::SKIP_DOTS) |
| 24 | ); |
| 25 | |
| 26 | return iterator_to_array($iterator); |
| 27 | } |
| 28 | |
| 29 | /** |
| 30 | * Extract the parent directory from a file path. |
| 31 | * |
| 32 | * @param string $path |
| 33 | * |
| 34 | * @return string |
| 35 | */ |
| 36 | public function dirname(string $path): string |
| 37 | { |
| 38 | return pathinfo($path, PATHINFO_DIRNAME); |
| 39 | } |
| 40 | |
| 41 | /** |
| 42 | * Get the contents of a file. |
| 43 | * |
| 44 | * @param string $path |
| 45 | * |
| 46 | * @return string |
| 47 | * @throws \RuntimeException |
| 48 | */ |
| 49 | public function get(string $path): string |
| 50 | { |
| 51 | if (! $this->isFile($path)) { |
| 52 | throw new RuntimeException('File does not exist at path ' . $path); |
| 53 | } |
| 54 | |
| 55 | return file_get_contents($path); |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Determine if the given path is a file. |
| 60 | * |
| 61 | * @param string $path |
| 62 | * |
| 63 | * @return bool |
| 64 | */ |
| 65 | public function isFile(string $path): bool |
| 66 | { |
| 67 | return is_file($path); |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Determine if the given path is a directory. |
| 72 | * |
| 73 | * @param string $path |
| 74 | * |
| 75 | * @return bool |
| 76 | */ |
| 77 | public function isDir(string $path): bool |
| 78 | { |
| 79 | return is_dir($path); |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Write the contents of a file. |
| 84 | * |
| 85 | * @param string $path |
| 86 | * @param string $contents |
| 87 | * |
| 88 | * @return int|false |
| 89 | */ |
| 90 | public function put(string $path, string $contents) |
| 91 | { |
| 92 | return file_put_contents($path, $contents); |
| 93 | } |
| 94 | } |
| 95 |