Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
15 / 15 |
|
100.00% |
4 / 4 |
CRAP | |
100.00% |
1 / 1 |
| ForwardsIlluminateFilesystem | |
100.00% |
15 / 15 |
|
100.00% |
4 / 4 |
7 | |
100.00% |
1 / 1 |
| __callStatic | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getParameterNames | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
| qualifyArguments | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
3 | |||
| qualifyPathArgument | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace Hyde\Framework\Concerns\Internal; |
| 6 | |
| 7 | use Illuminate\Filesystem\Filesystem; |
| 8 | use Illuminate\Support\Collection; |
| 9 | use Illuminate\Support\LazyCollection; |
| 10 | use ReflectionMethod; |
| 11 | use ReflectionParameter; |
| 12 | use Symfony\Component\Finder\SplFileInfo; |
| 13 | |
| 14 | use function array_map; |
| 15 | use function collect; |
| 16 | use function in_array; |
| 17 | use function is_array; |
| 18 | use function is_int; |
| 19 | |
| 20 | /** |
| 21 | * Forwards calls to the Laravel File facade to the HydePHP Filesystem Facade, |
| 22 | * while turning all paths arguments into absolute project paths. |
| 23 | * |
| 24 | * @interal This trait is not covered by the backward compatibility promise. |
| 25 | * |
| 26 | * @see \Hyde\Facades\Filesystem |
| 27 | * |
| 28 | * @method static bool exists(string $path) |
| 29 | * @method static bool missing(string $path) |
| 30 | * @method static string get(string $path, bool $lock = false) |
| 31 | * @method static string sharedGet(string $path) |
| 32 | * @method static mixed getRequire(string $path, array $data = []) |
| 33 | * @method static mixed requireOnce(string $path, array $data = []) |
| 34 | * @method static LazyCollection lines(string $path) |
| 35 | * @method static string hash(string $path, string $algorithm = 'md5') |
| 36 | * @method static int|bool put(string $path, string $contents, bool $lock = false) |
| 37 | * @method static void replace(string $path, string $content) |
| 38 | * @method static void replaceInFile(array|string $search, array|string $replace, string $path) |
| 39 | * @method static int prepend(string $path, string $data) |
| 40 | * @method static int append(string $path, string $data) |
| 41 | * @method static mixed chmod(string $path, int|null $mode = null) |
| 42 | * @method static bool delete(string|array $paths) |
| 43 | * @method static bool move(string $path, string $target) |
| 44 | * @method static bool copy(string $path, string $target) |
| 45 | * @method static void link(string $target, string $link) |
| 46 | * @method static void relativeLink(string $target, string $link) |
| 47 | * @method static string name(string $path) |
| 48 | * @method static string basename(string $path) |
| 49 | * @method static string dirname(string $path) |
| 50 | * @method static string extension(string $path) |
| 51 | * @method static string|null guessExtension(string $path) |
| 52 | * @method static string type(string $path) |
| 53 | * @method static string|false mimeType(string $path) |
| 54 | * @method static int size(string $path) |
| 55 | * @method static int lastModified(string $path) |
| 56 | * @method static bool isDirectory(string $directory) |
| 57 | * @method static bool isEmptyDirectory(string $directory, bool $ignoreDotFiles = false) |
| 58 | * @method static bool isReadable(string $path) |
| 59 | * @method static bool isWritable(string $path) |
| 60 | * @method static bool hasSameHash(string $firstFile, string $secondFile) |
| 61 | * @method static bool isFile(string $file) |
| 62 | * @method static array<string> glob(string $pattern, int $flags = 0) |
| 63 | * @method static SplFileInfo[] files(string $directory, bool $hidden = false) |
| 64 | * @method static SplFileInfo[] allFiles(string $directory, bool $hidden = false) |
| 65 | * @method static array directories(string $directory) |
| 66 | * @method static void ensureDirectoryExists(string $path, int $mode = 0755, bool $recursive = true) |
| 67 | * @method static bool makeDirectory(string $path, int $mode = 0755, bool $recursive = false, bool $force = false) |
| 68 | * @method static bool moveDirectory(string $from, string $to, bool $overwrite = false) |
| 69 | * @method static bool copyDirectory(string $directory, string $destination, int|null $options = null) |
| 70 | * @method static bool deleteDirectory(string $directory, bool $preserve = false) |
| 71 | * @method static bool deleteDirectories(string $directory) |
| 72 | * @method static bool cleanDirectory(string $directory) |
| 73 | */ |
| 74 | trait ForwardsIlluminateFilesystem |
| 75 | { |
| 76 | public static function __callStatic(string $name, array $arguments): string|array|int|bool|null|LazyCollection |
| 77 | { |
| 78 | return self::filesystem()->{$name}(...self::qualifyArguments(self::getParameterNames($name), $arguments)); |
| 79 | } |
| 80 | |
| 81 | protected static function getParameterNames(string $name): array |
| 82 | { |
| 83 | return array_map(fn (ReflectionParameter $parameter): string => $parameter->getName(), |
| 84 | (new ReflectionMethod(Filesystem::class, $name))->getParameters() |
| 85 | ); |
| 86 | } |
| 87 | |
| 88 | /** @param string[] $parameterNames */ |
| 89 | protected static function qualifyArguments(array $parameterNames, array $arguments): Collection |
| 90 | { |
| 91 | return collect($arguments)->mapWithKeys(function (string|array|int|bool $argumentValue, int|string $key) use ($parameterNames): string|array|int|bool { |
| 92 | $argumentsToQualify = ['path', 'paths', 'file', 'target', 'directory', 'destination', 'firstFile', 'secondFile', 'pattern', 'link', 'from', 'to']; |
| 93 | |
| 94 | if (is_int($key)) { |
| 95 | // If the argument is not named, we'll retrieve it from the reflection data. |
| 96 | $key = $parameterNames[$key]; |
| 97 | } |
| 98 | |
| 99 | if (in_array($key, $argumentsToQualify)) { |
| 100 | $argumentValue = self::qualifyPathArgument($argumentValue); |
| 101 | } |
| 102 | |
| 103 | return [$key => $argumentValue]; |
| 104 | }); |
| 105 | } |
| 106 | |
| 107 | protected static function qualifyPathArgument(array|string $path): string|array |
| 108 | { |
| 109 | return is_array($path) |
| 110 | ? array_map(fn (string $path): string => self::qualifyPathArgument($path), $path) |
| 111 | : self::absolutePath($path); |
| 112 | } |
| 113 | } |