Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
18 / 18 |
|
100.00% |
4 / 4 |
CRAP | |
100.00% |
1 / 1 |
| FileFinder | |
100.00% |
18 / 18 |
|
100.00% |
4 / 4 |
7 | |
100.00% |
1 / 1 |
| handle | |
100.00% |
10 / 10 |
|
100.00% |
1 / 1 |
4 | |||
| buildFileExtensionPattern | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| expandCommaSeparatedValues | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
| normalizeExtensionForRegexPattern | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace Hyde\Framework\Actions\Internal; |
| 6 | |
| 7 | use Hyde\Facades\Filesystem; |
| 8 | use Hyde\Hyde; |
| 9 | use Illuminate\Support\Collection; |
| 10 | use SplFileInfo; |
| 11 | use Symfony\Component\Finder\Finder; |
| 12 | |
| 13 | /** |
| 14 | * @interal This class is used internally by the framework and is not part of the public API, unless that is requested on GitHub with a valid use case. |
| 15 | */ |
| 16 | class FileFinder |
| 17 | { |
| 18 | /** |
| 19 | * @param array<string>|string|false $matchExtensions |
| 20 | * @return \Illuminate\Support\Collection<int, string> |
| 21 | */ |
| 22 | public static function handle(string $directory, array|string|false $matchExtensions = false, bool $recursive = false): Collection |
| 23 | { |
| 24 | if (! Filesystem::isDirectory($directory)) { |
| 25 | return collect(); |
| 26 | } |
| 27 | |
| 28 | $finder = Finder::create()->files()->in(Hyde::path($directory)); |
| 29 | |
| 30 | if ($recursive === false) { |
| 31 | $finder->depth('== 0'); |
| 32 | } |
| 33 | |
| 34 | if ($matchExtensions !== false) { |
| 35 | $finder->name(static::buildFileExtensionPattern((array) $matchExtensions)); |
| 36 | } |
| 37 | |
| 38 | return collect($finder)->map(function (SplFileInfo $file): string { |
| 39 | return Hyde::pathToRelative($file->getPathname()); |
| 40 | })->sort()->values(); |
| 41 | } |
| 42 | |
| 43 | /** @param array<string> $extensions */ |
| 44 | protected static function buildFileExtensionPattern(array $extensions): string |
| 45 | { |
| 46 | $extensions = self::expandCommaSeparatedValues($extensions); |
| 47 | |
| 48 | return '/\.('.self::normalizeExtensionForRegexPattern($extensions).')$/i'; |
| 49 | } |
| 50 | |
| 51 | /** @param array<string> $extensions */ |
| 52 | private static function expandCommaSeparatedValues(array $extensions): array |
| 53 | { |
| 54 | return array_merge(...array_map(function (string $item): array { |
| 55 | return array_map(fn (string $item): string => trim($item), explode(',', $item)); |
| 56 | }, $extensions)); |
| 57 | } |
| 58 | |
| 59 | /** @param array<string> $extensions */ |
| 60 | private static function normalizeExtensionForRegexPattern(array $extensions): string |
| 61 | { |
| 62 | return implode('|', array_map(function (string $extension): string { |
| 63 | return preg_quote(ltrim($extension, '.'), '/'); |
| 64 | }, $extensions)); |
| 65 | } |
| 66 | } |