Lines 100.00% 40 / 40
Functions and Methods 100.00% 7 / 7
Classes and Traits 100.00% 1 / 1
Name Lines Functions and Methods CRAP Classes and Traits
CheckCommand 100.00% 40 / 40 100.00% 7 / 7 19 100.00% 1 / 1
 __construct 100.00% 1 / 1 100.00% 1 / 1 1
 execute 100.00% 17 / 17 100.00% 1 / 1 4
 writeHttpListenerNotice 100.00% 2 / 2 100.00% 1 / 1 2
 writeUnmarkedAppNotices 100.00% 2 / 2 100.00% 1 / 1 2
 writeUnusedTemplateNotices 100.00% 2 / 2 100.00% 1 / 1 2
 writeUnmatchedOverridesNotice 100.00% 6 / 6 100.00% 1 / 1 3
 writeAppTable 100.00% 10 / 10 100.00% 1 / 1 5
1<?php
2
3declare(strict_types=1);
4
5namespace Stewart\Runtime\Console;
6
7use Stewart\Runtime\App\AppCatalog;
8use Stewart\Runtime\App\Collection\UnmarkedAppCollection;
9use Stewart\Runtime\App\DiscoveredApp;
10use Stewart\Runtime\App\DiscoveryResult;
11use Stewart\Runtime\Broker\Collection\WorkerSlotCollection;
12use Stewart\Runtime\Config\ConfigLoader;
13use Stewart\Runtime\Console\Check\ProjectCheck;
14use Symfony\Component\Console\Attribute\AsCommand;
15use Symfony\Component\Console\Command\Command;
16use Symfony\Component\Console\Helper\Table;
17use Symfony\Component\Console\Input\InputInterface;
18use Symfony\Component\Console\Output\ConsoleOutputInterface;
19use Symfony\Component\Console\Output\OutputInterface;
20
21#[AsCommand(name: self::NAME, description: 'Check that the configuration loads and every app can be built')]
22final class CheckCommand extends StewartCommand
23{
24    public const string NAME = 'check';
25
26    public function __construct(
27        private readonly ConfigLoader $config,
28        private readonly ProjectCheck $projects,
29    ) {
30        parent::__construct();
31    }
32
33    protected function execute(InputInterface $input, OutputInterface $output): int
34    {
35        $configFile = $this->findConfigOption($input);
36        $result = $this->projects->checkProject($configFile);
37
38        if (!$result->hasPassed()) {
39            $errors = $output instanceof ConsoleOutputInterface ? $output->getErrorOutput() : $output;
40
41            foreach ($result->errors as $error) {
42                $errors->writeln('<error>' . $error . '</error>');
43            }
44
45            return Command::FAILURE;
46        }
47
48        $discovered = $result->discovered;
49        $apps = $result->catalog;
50
51        $this->writeMissingConfigFileNotice($output, $this->config->findMissingDefaultFile($configFile));
52        $this->writeHttpListenerNotice($output, $result->httpListenerError);
53        $this->writeUnmatchedOverridesNotice($output, $apps);
54        $this->writeUnmarkedAppNotices($output, $discovered->unmarked);
55        $this->writeUnusedTemplateNotices($output, $discovered->listUnusedTemplateClasses());
56        $this->writeAppTable($output, $discovered, $apps, $result->workerSlots);
57        $output->writeln(\sprintf('Configuration and %d automations load; %d enabled.', $discovered->apps->count(), $apps->enabled->count()));
58
59        return Command::SUCCESS;
60    }
61
62    private function writeHttpListenerNotice(OutputInterface $output, ?string $error): void
63    {
64        if ($error !== null) {
65            $this->writeNotice($output, $error . ' The daemon refuses to start until it is fixed.');
66        }
67    }
68
69    private function writeUnmarkedAppNotices(OutputInterface $output, UnmarkedAppCollection $unmarked): void
70    {
71        foreach ($unmarked as $app) {
72            $this->writeNotice($output, \sprintf('%s implements App but is not marked #[Automation] or #[AppTemplate], so it will not run.', $app->class));
73        }
74    }
75
76    /** @param list<class-string> $templates */
77    private function writeUnusedTemplateNotices(OutputInterface $output, array $templates): void
78    {
79        foreach ($templates as $template) {
80            $this->writeNotice($output, \sprintf('%s is an #[AppTemplate], but no apps.<id>.class names it, so it does not run.', $template));
81        }
82    }
83
84    private function writeUnmatchedOverridesNotice(OutputInterface $output, AppCatalog $apps): void
85    {
86        if (!$apps->unmatchedOverrideIds->isEmpty()) {
87            $this->writeNotice($output, \sprintf(
88                'stewart.yaml or the environment configures automations that were not found, so their settings are ignored: %s. Known automations: %s.',
89                implode(', ', $apps->unmatchedOverrideIds->toStrings()),
90                $apps->knownIds->isEmpty() ? 'none' : implode(', ', $apps->knownIds->toStrings()),
91            ));
92        }
93    }
94
95    // Sorted by class, so the entries that run one class, and a template nothing runs, sit together.
96    private function writeAppTable(OutputInterface $output, DiscoveryResult $discovered, AppCatalog $apps, WorkerSlotCollection $slots): void
97    {
98        $rows = $discovered->apps->mapToList(static function (DiscoveredApp $app) use ($apps, $slots): array {
99            $definition = $apps->enabled->find($app->id);
100            $state = $definition === null ? 'disabled' : ($definition->startsPaused ? 'paused' : 'enabled');
101
102            return [$app->id->value, $app->class, (string) ($slots->findWorkerOfApp($app->id) ?? '-'), $state];
103        });
104
105        foreach ($discovered->listUnusedTemplateClasses() as $template) {
106            $rows[] = ['-', $template, '-', 'no entry'];
107        }
108
109        usort($rows, static fn(array $a, array $b): int => [$a[1], $a[0]] <=> [$b[1], $b[0]]);
110
111        if ($rows !== []) {
112            new Table($output)->setStyle('compact')->setHeaders(['app', 'class', 'worker', 'state'])->setRows($rows)->render();
113        }
114    }
115}