Lines
100.00%
18 / 18
Functions and Methods
100.00%
4 / 4
Classes and Traits
100.00%
1 / 1
| Name | Lines | Functions and Methods | CRAP | Classes and Traits | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| MakeAppCommand | 100.00% | 18 / 18 | 100.00% | 4 / 4 | 10 | 100.00% | 1 / 1 | |||
| __construct | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | |||||
| configure | 100.00% | 3 / 3 | 100.00% | 1 / 1 | 1 | |||||
| execute | 100.00% | 12 / 12 | 100.00% | 1 / 1 | 7 | |||||
| writeTemplateEntry | 100.00% | 2 / 2 | 100.00% | 1 / 1 | 1 | |||||
| 1 | <?php | |
| 2 | ||
| 3 | declare(strict_types=1); | |
| 4 | ||
| 5 | namespace Stewart\Runtime\Console; | |
| 6 | ||
| 7 | use Stewart\Contracts\Exception\StewartException; | |
| 8 | use Stewart\Runtime\Console\Scaffold\AppScaffold; | |
| 9 | use Stewart\Runtime\Console\Scaffold\AppScaffoldKind; | |
| 10 | use Stewart\Runtime\Console\Scaffold\ScaffoldedApp; | |
| 11 | use Symfony\Component\Console\Attribute\AsCommand; | |
| 12 | use Symfony\Component\Console\Command\Command; | |
| 13 | use Symfony\Component\Console\Input\InputArgument; | |
| 14 | use Symfony\Component\Console\Input\InputInterface; | |
| 15 | use Symfony\Component\Console\Input\InputOption; | |
| 16 | use Symfony\Component\Console\Output\ConsoleOutputInterface; | |
| 17 | use Symfony\Component\Console\Output\OutputInterface; | |
| 18 | ||
| 19 | #[AsCommand(name: self::NAME, description: 'Create an app in apps/ and its test in tests/')] | |
| 20 | final class MakeAppCommand extends Command | |
| 21 | { | |
| 22 | public const string NAME = 'make:app'; | |
| 23 | ||
| 24 | public function __construct(private readonly AppScaffold $scaffold) | |
| 25 | { | |
| 26 | parent::__construct(); | |
| 27 | } | |
| 28 | ||
| 29 | protected function configure(): void | |
| 30 | { | |
| 31 | $this->addArgument('name', InputArgument::REQUIRED, 'The class name, such as PorchLight'); | |
| 32 | $this->addOption('id', null, InputOption::VALUE_REQUIRED, 'The automation ID, or with --template the first entry\'s; derived from the name when left out, such as porch-light'); | |
| 33 | $this->addOption('template', null, InputOption::VALUE_NONE, 'Create an #[AppTemplate] class, which runs once per apps.<id>.class entry in stewart.yaml'); | |
| 34 | } | |
| 35 | ||
| 36 | protected function execute(InputInterface $input, OutputInterface $output): int | |
| 37 | { | |
| 38 | $name = $input->getArgument('name'); | |
| 39 | $id = $input->getOption('id'); | |
| 40 | ||
| 41 | try { | |
| 42 | $kind = $input->getOption('template') === true ? AppScaffoldKind::Template : AppScaffoldKind::Automation; | |
| 43 | $app = $this->scaffold->scaffoldApp(\is_string($name) ? $name : '', \is_string($id) ? $id : null, $kind); | |
| 44 | } catch (StewartException $e) { | |
| 45 | ($output instanceof ConsoleOutputInterface ? $output->getErrorOutput() : $output)->writeln('<error>' . $e->getMessage() . '</error>'); | |
| 46 | ||
| 47 | return Command::FAILURE; | |
| 48 | } | |
| 49 | ||
| 50 | $output->writeln(\sprintf('Created %s and %s.', $app->appFile, $app->testFile)); | |
| 51 | ||
| 52 | if ($app->kind === AppScaffoldKind::Template) { | |
| 53 | $this->writeTemplateEntry($output, $app); | |
| 54 | } else { | |
| 55 | $output->writeln(\sprintf('Its ID is %1$s; its options go under apps.%1$s in stewart.yaml.', $app->id->value)); | |
| 56 | } | |
| 57 | ||
| 58 | return Command::SUCCESS; | |
| 59 | } | |
| 60 | ||
| 61 | private function writeTemplateEntry(OutputInterface $output, ScaffoldedApp $app): void | |
| 62 | { | |
| 63 | $output->writeln('It runs once for each entry that names it. Add one to stewart.yaml, with an ID of its own:'); | |
| 64 | $output->writeln(['', 'apps:', \sprintf(' %s:', $app->id->value), \sprintf(' class: %s', $app->className), ' options:', ' entity: light.example']); | |
| 65 | } | |
| 66 | } |