|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Codelabmw\InfiniteScroll\Console\Commands; |
| 6 | + |
| 7 | +use Codelabmw\InfiniteScroll\Contracts\Stack; |
| 8 | +use Codelabmw\InfiniteScroll\SupportedStacks; |
| 9 | +use Illuminate\Console\Command; |
| 10 | +use Illuminate\Support\Collection; |
| 11 | +use Illuminate\Support\Facades\App; |
| 12 | + |
| 13 | +use function Laravel\Prompts\error; |
| 14 | +use function Laravel\Prompts\info; |
| 15 | +use function Laravel\Prompts\select; |
| 16 | +use function Laravel\Prompts\spin; |
| 17 | +use function Laravel\Prompts\text; |
| 18 | + |
| 19 | +final class InstallCommand extends Command |
| 20 | +{ |
| 21 | + /** |
| 22 | + * The name and signature of the console command. |
| 23 | + * |
| 24 | + * @var string |
| 25 | + */ |
| 26 | + protected $signature = 'install:infinite-scroll'; |
| 27 | + |
| 28 | + /** |
| 29 | + * The console command description. |
| 30 | + * |
| 31 | + * @var string |
| 32 | + */ |
| 33 | + protected $description = 'Install Infinite Scrolling components for Inertia.'; |
| 34 | + |
| 35 | + /** |
| 36 | + * List of registered supported stacks. |
| 37 | + * |
| 38 | + * @var Collection<string, class-string<Stack>> |
| 39 | + */ |
| 40 | + private readonly Collection $supportedStacks; |
| 41 | + |
| 42 | + /** |
| 43 | + * Creates a new InstallCommand instance. |
| 44 | + */ |
| 45 | + public function __construct(SupportedStacks $supportedStacksService) |
| 46 | + { |
| 47 | + parent::__construct(); |
| 48 | + $this->supportedStacks = collect($supportedStacksService->get()); |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * Execute the console command. |
| 53 | + */ |
| 54 | + public function handle(): ?int |
| 55 | + { |
| 56 | + /** @var string */ |
| 57 | + $stack = select( |
| 58 | + label: 'Which stack are you using?', |
| 59 | + options: $this->supportedStacks->keys(), // @phpstan-ignore-line |
| 60 | + required: true, |
| 61 | + ); |
| 62 | + |
| 63 | + /** @var Stack */ |
| 64 | + $stack = App::make((string) $this->supportedStacks->get($stack)); |
| 65 | + |
| 66 | + $installationPath = text( |
| 67 | + label: 'Where do you want to install components?', |
| 68 | + placeholder: $stack->getDefaultInstallationPath(), |
| 69 | + required: true, |
| 70 | + ); |
| 71 | + |
| 72 | + $error = spin(fn (): ?string => $this->install($stack->getStubs()), 'Installing infinite scroll components for '.$stack->getLabel().' in '.$installationPath.'.'); |
| 73 | + |
| 74 | + if ($error) { |
| 75 | + error('Error occurred while installing components. '.$error); |
| 76 | + |
| 77 | + return 1; |
| 78 | + } |
| 79 | + |
| 80 | + // @codeCoverageIgnoreStart |
| 81 | + info('Successfully installed components.'); |
| 82 | + |
| 83 | + return null; |
| 84 | + // @codeCoverageIgnoreEnd |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * Installs infinite scrolling frontend components. |
| 89 | + * |
| 90 | + * @param Collection<int, string> $stubs |
| 91 | + */ |
| 92 | + private function install(Collection $stubs): ?string |
| 93 | + { |
| 94 | + if ($stubs->isEmpty()) { |
| 95 | + return 'Installation files were not found.'; |
| 96 | + } |
| 97 | + |
| 98 | + $error = null; |
| 99 | + $stubs->each(function (string $file) use (&$error): void { |
| 100 | + if (! file_exists($file)) { |
| 101 | + $error = 'The file: '.$file.' does not exists.'; |
| 102 | + |
| 103 | + return; |
| 104 | + } |
| 105 | + }); |
| 106 | + |
| 107 | + return $error; |
| 108 | + } |
| 109 | +} |
0 commit comments