|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace WendellAdriel\Virtue\Commands\Concerns; |
| 6 | + |
| 7 | +use Illuminate\Support\Collection; |
| 8 | +use ReflectionAttribute; |
| 9 | +use Symfony\Component\Console\Input\InputArgument; |
| 10 | +use Symfony\Component\Console\Input\InputOption; |
| 11 | +use WendellAdriel\Virtue\Commands\Attributes\Argument; |
| 12 | +use WendellAdriel\Virtue\Commands\Attributes\FlagOption; |
| 13 | +use WendellAdriel\Virtue\Commands\Attributes\Option; |
| 14 | +use WendellAdriel\Virtue\Commands\Attributes\OptionalArgument; |
| 15 | +use WendellAdriel\Virtue\Commands\Attributes\RequiredArgument; |
| 16 | +use WendellAdriel\Virtue\Commands\Attributes\ValueOption; |
| 17 | +use WendellAdriel\Virtue\Support\HasAttributesReflection; |
| 18 | + |
| 19 | +trait Virtue |
| 20 | +{ |
| 21 | + use HasAttributesReflection; |
| 22 | + |
| 23 | + /** |
| 24 | + * @return array<InputArgument> |
| 25 | + */ |
| 26 | + public function getArguments(): array |
| 27 | + { |
| 28 | + $arguments = []; |
| 29 | + $generalArguments = $this->buildArgumentsList(Argument::class); |
| 30 | + |
| 31 | + $requiredArguments = $this->buildArgumentsList(RequiredArgument::class) |
| 32 | + ->merge($generalArguments->where('mode', InputArgument::REQUIRED)); |
| 33 | + |
| 34 | + [$arrayArguments, $nonArrayArguments] = $requiredArguments->partition(fn (array $argument) => $argument['mode'] > InputArgument::REQUIRED); |
| 35 | + |
| 36 | + $optionalArguments = $this->buildArgumentsList(OptionalArgument::class) |
| 37 | + ->merge($generalArguments->filter(fn (array $argument) => $argument['mode'] === InputArgument::OPTIONAL || $argument['mode'] === InputArgument::IS_ARRAY)); |
| 38 | + |
| 39 | + foreach ($nonArrayArguments as $argument) { |
| 40 | + $arguments[] = $argument; |
| 41 | + } |
| 42 | + |
| 43 | + foreach ($optionalArguments as $argument) { |
| 44 | + $arguments[] = $argument; |
| 45 | + } |
| 46 | + |
| 47 | + foreach ($arrayArguments as $argument) { |
| 48 | + $arguments[] = $argument; |
| 49 | + } |
| 50 | + |
| 51 | + return $arguments; |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * @return array<InputOption> |
| 56 | + */ |
| 57 | + public function getOptions(): array |
| 58 | + { |
| 59 | + $options = []; |
| 60 | + $valueOptions = $this->buildOptionsList(ValueOption::class); |
| 61 | + $flagOptions = $this->buildOptionsList(FlagOption::class); |
| 62 | + |
| 63 | + foreach ($valueOptions as $option) { |
| 64 | + $options[] = $option; |
| 65 | + } |
| 66 | + |
| 67 | + foreach ($flagOptions as $option) { |
| 68 | + $options[] = $option; |
| 69 | + } |
| 70 | + |
| 71 | + return $options; |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * @param class-string $attribute |
| 76 | + */ |
| 77 | + private function buildArgumentsList(string $attribute): Collection |
| 78 | + { |
| 79 | + return $this->resolveMultipleAttributes($attribute)->map(function (ReflectionAttribute $argumentAttribute) { |
| 80 | + /** @var Argument $attribute */ |
| 81 | + $attribute = $argumentAttribute->newInstance(); |
| 82 | + |
| 83 | + return [ |
| 84 | + 'name' => $attribute->name, |
| 85 | + 'mode' => $this->resolveMode($attribute), |
| 86 | + 'description' => $attribute->description, |
| 87 | + 'default' => $attribute->default, |
| 88 | + 'suggestedValues' => $attribute->suggestedValues, |
| 89 | + ]; |
| 90 | + }); |
| 91 | + } |
| 92 | + |
| 93 | + private function resolveMode(Argument $attribute): int |
| 94 | + { |
| 95 | + return match (true) { |
| 96 | + $attribute->required && $attribute->array => InputArgument::IS_ARRAY | InputArgument::REQUIRED, |
| 97 | + $attribute->required && ! $attribute->array => InputArgument::REQUIRED, |
| 98 | + ! $attribute->required && $attribute->array => InputArgument::IS_ARRAY, |
| 99 | + ! $attribute->required && ! $attribute->array => InputArgument::OPTIONAL, |
| 100 | + default => InputArgument::REQUIRED, |
| 101 | + }; |
| 102 | + } |
| 103 | + |
| 104 | + /** |
| 105 | + * @param class-string $attribute |
| 106 | + */ |
| 107 | + private function buildOptionsList(string $attribute): Collection |
| 108 | + { |
| 109 | + return $this->resolveMultipleAttributes($attribute)->map(function (ReflectionAttribute $optionAttribute) { |
| 110 | + /** @var Option $attribute */ |
| 111 | + $attribute = $optionAttribute->newInstance(); |
| 112 | + |
| 113 | + return [ |
| 114 | + 'name' => $attribute->name, |
| 115 | + 'shortcut' => $attribute->shortcut, |
| 116 | + 'mode' => $attribute->mode, |
| 117 | + 'description' => $attribute->description, |
| 118 | + 'default' => $attribute->default, |
| 119 | + 'suggestedValues' => $attribute->suggestedValues, |
| 120 | + ]; |
| 121 | + }); |
| 122 | + } |
| 123 | +} |
0 commit comments