|
7 | 7 | use Illuminate\Container\Container; |
8 | 8 | use InvalidArgumentException; |
9 | 9 | use Laravel\Boost\Install\Contracts\DetectionStrategy; |
10 | | -use Laravel\Boost\Install\Enums\DetectionType; |
11 | 10 |
|
12 | 11 | class DetectionStrategyFactory |
13 | 12 | { |
14 | 13 | public function __construct(private readonly Container $container) |
15 | 14 | { |
16 | 15 | } |
17 | 16 |
|
18 | | - public function make(DetectionType|string|array $type): DetectionStrategy |
| 17 | + public function make(string|array $type, array $config = []): DetectionStrategy |
19 | 18 | { |
20 | 19 | if (is_array($type)) { |
21 | 20 | return new CompositeDetectionStrategy( |
22 | | - array_map(fn ($singleType) => $this->make($singleType), $type) |
| 21 | + array_map(fn ($singleType) => $this->make($singleType, $config), $type) |
23 | 22 | ); |
24 | 23 | } |
25 | 24 |
|
26 | | - $detectionType = $type instanceof DetectionType ? $type : DetectionType::from($type); |
27 | | - |
28 | | - return match ($detectionType) { |
29 | | - DetectionType::Directory => $this->container->make(DirectoryDetectionStrategy::class), |
30 | | - DetectionType::Command => $this->container->make(CommandDetectionStrategy::class), |
31 | | - DetectionType::File => $this->container->make(FileDetectionStrategy::class), |
32 | | - default => throw new InvalidArgumentException("Unknown detection type: {$detectionType->value}"), |
| 25 | + return match ($type) { |
| 26 | + 'directory' => $this->container->make(DirectoryDetectionStrategy::class), |
| 27 | + 'command' => $this->container->make(CommandDetectionStrategy::class), |
| 28 | + 'file' => $this->container->make(FileDetectionStrategy::class), |
| 29 | + default => throw new InvalidArgumentException("Unknown detection type: {$type}"), |
33 | 30 | }; |
34 | 31 | } |
| 32 | + |
| 33 | + public function makeFromConfig(array $config): DetectionStrategy |
| 34 | + { |
| 35 | + $type = $this->inferTypeFromConfig($config); |
| 36 | + |
| 37 | + return $this->make($type, $config); |
| 38 | + } |
| 39 | + |
| 40 | + private function inferTypeFromConfig(array $config): string|array |
| 41 | + { |
| 42 | + $types = []; |
| 43 | + |
| 44 | + if (isset($config['files'])) { |
| 45 | + $types[] = 'file'; |
| 46 | + } |
| 47 | + |
| 48 | + if (isset($config['paths'])) { |
| 49 | + $types[] = 'directory'; |
| 50 | + } |
| 51 | + |
| 52 | + if (isset($config['command'])) { |
| 53 | + $types[] = 'command'; |
| 54 | + } |
| 55 | + |
| 56 | + if (empty($types)) { |
| 57 | + throw new InvalidArgumentException('Cannot infer detection type from config keys. Expected one of: files, paths, command'); |
| 58 | + } |
| 59 | + |
| 60 | + return count($types) === 1 ? $types[0] : $types; |
| 61 | + } |
35 | 62 | } |
0 commit comments