Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
12 / 12 |
|
100.00% |
12 / 12 |
CRAP | |
100.00% |
1 / 1 |
| Filesystem | |
100.00% |
12 / 12 |
|
100.00% |
12 / 12 |
12 | |
100.00% |
1 / 1 |
| basePath | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| absolutePath | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| relativePath | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| smartGlob | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| findFiles | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| touch | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| unlink | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| unlinkIfExists | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getContents | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| putContents | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| filesystem | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| kernel | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace Hyde\Facades; |
| 6 | |
| 7 | use Hyde\Foundation\HydeKernel; |
| 8 | use Hyde\Framework\Concerns\Internal\ForwardsIlluminateFilesystem; |
| 9 | use Illuminate\Support\Collection; |
| 10 | |
| 11 | use function app; |
| 12 | |
| 13 | /** |
| 14 | * Proxies the Laravel File facade with extra features and helpers tailored for HydePHP. |
| 15 | * |
| 16 | * For maximum compatability and interoperability, all path references in HydePHP are relative to the root of the project. |
| 17 | * The helpers here will then prepend the project root to the path before actually interacting with the filesystem. |
| 18 | * |
| 19 | * @see \Hyde\Foundation\Kernel\Filesystem |
| 20 | * @see \Illuminate\Filesystem\Filesystem |
| 21 | */ |
| 22 | class Filesystem |
| 23 | { |
| 24 | use ForwardsIlluminateFilesystem; |
| 25 | |
| 26 | /** |
| 27 | * Get the base path of the HydePHP project. |
| 28 | * |
| 29 | * @return string |
| 30 | */ |
| 31 | public static function basePath(): string |
| 32 | { |
| 33 | return self::kernel()->path(); |
| 34 | } |
| 35 | |
| 36 | /** |
| 37 | * Format the given project path to be absolute. Already absolute paths are normalized. |
| 38 | * |
| 39 | * @param string $path |
| 40 | * @return string |
| 41 | */ |
| 42 | public static function absolutePath(string $path): string |
| 43 | { |
| 44 | return self::kernel()->pathToAbsolute(self::relativePath($path)); |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * Remove the absolute path from the given project path so that it becomes relative. |
| 49 | * |
| 50 | * @param string $path |
| 51 | * @return string |
| 52 | */ |
| 53 | public static function relativePath(string $path): string |
| 54 | { |
| 55 | return self::kernel()->pathToRelative($path); |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * A smarter glob function that will run the specified glob pattern a bit more intelligently. |
| 60 | * While this method will use the absolute path when interacting with the filesystem, |
| 61 | * the returned collection will only contain relative paths. |
| 62 | * |
| 63 | * @param string $pattern |
| 64 | * @param int $flags |
| 65 | * @return \Illuminate\Support\Collection<int, string> |
| 66 | */ |
| 67 | public static function smartGlob(string $pattern, int $flags = 0): Collection |
| 68 | { |
| 69 | return self::kernel()->filesystem()->smartGlob($pattern, $flags); |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Find files in the project's directory, with optional filtering by extension and recursion. |
| 74 | * |
| 75 | * The returned collection will be a list of paths relative to the project root. |
| 76 | * |
| 77 | * @param string $directory |
| 78 | * @param string|array<string>|false $matchExtensions The file extension(s) to match, or false to match all files. |
| 79 | * @param bool $recursive Whether to search recursively or not. |
| 80 | * @return \Illuminate\Support\Collection<int, string> |
| 81 | */ |
| 82 | public static function findFiles(string $directory, string|array|false $matchExtensions = false, bool $recursive = false): Collection |
| 83 | { |
| 84 | return self::kernel()->filesystem()->findFiles($directory, $matchExtensions, $recursive); |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Touch one or more files in the project's directory. |
| 89 | * |
| 90 | * @param string|array $path |
| 91 | * @return bool |
| 92 | */ |
| 93 | public static function touch(string|array $path): bool |
| 94 | { |
| 95 | return self::kernel()->filesystem()->touch($path); |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Unlink one or more files in the project's directory. |
| 100 | * |
| 101 | * @param string|array $path |
| 102 | * @return bool |
| 103 | */ |
| 104 | public static function unlink(string|array $path): bool |
| 105 | { |
| 106 | return self::kernel()->filesystem()->unlink($path); |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * Unlink a file in the project's directory, but only if it exists. |
| 111 | * |
| 112 | * @param string $path |
| 113 | * @return bool True if the file was unlinked, false if it did not exist or failed to unlink. |
| 114 | */ |
| 115 | public static function unlinkIfExists(string $path): bool |
| 116 | { |
| 117 | return self::kernel()->filesystem()->unlinkIfExists($path); |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * Get the contents of a file. |
| 122 | * |
| 123 | * @param string $path |
| 124 | * @param bool $lock |
| 125 | * @return string |
| 126 | * |
| 127 | * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException |
| 128 | */ |
| 129 | public static function getContents(string $path, bool $lock = false): string |
| 130 | { |
| 131 | return self::get(...func_get_args()); |
| 132 | } |
| 133 | |
| 134 | /** |
| 135 | * Write the contents of a file. |
| 136 | * |
| 137 | * @param string $path |
| 138 | * @param string $contents |
| 139 | * @param bool $lock |
| 140 | * @return int|bool |
| 141 | */ |
| 142 | public static function putContents(string $path, string $contents, bool $lock = false): bool|int |
| 143 | { |
| 144 | return self::put(...func_get_args()); |
| 145 | } |
| 146 | |
| 147 | protected static function filesystem(): \Illuminate\Filesystem\Filesystem |
| 148 | { |
| 149 | return app(\Illuminate\Filesystem\Filesystem::class); |
| 150 | } |
| 151 | |
| 152 | protected static function kernel(): HydeKernel |
| 153 | { |
| 154 | return HydeKernel::getInstance(); |
| 155 | } |
| 156 | } |