eb99eb5a2e
Signed-off-by: Sebastian Krupinski <krupinski01@gmail.com>
77 lines
2.6 KiB
PHP
77 lines
2.6 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace KTXC\Runtime\Console;
|
|
|
|
use KTXC\Application\Execution\ExecutionDescriptor;
|
|
use KTXC\Kernel;
|
|
use KTXC\KernelInterface;
|
|
use KTXC\Module\ModuleManager;
|
|
use KTXF\Module\ModuleConsoleInterface;
|
|
use Psr\Container\ContainerInterface;
|
|
use Symfony\Component\Console\Application as ConsoleApplication;
|
|
use Symfony\Component\Console\Attribute\AsCommand;
|
|
use Symfony\Component\Console\Command\LazyCommand;
|
|
use Symfony\Component\Console\Input\InputInterface;
|
|
use Symfony\Component\Console\Output\OutputInterface;
|
|
|
|
final class ConsoleRuntime
|
|
{
|
|
public function __construct(
|
|
private readonly KernelInterface $kernel,
|
|
) {
|
|
}
|
|
|
|
public function run(?InputInterface $input = null, ?OutputInterface $output = null): int
|
|
{
|
|
return $this->kernel->executionRunner()->execute(
|
|
ExecutionDescriptor::cli(),
|
|
function () use ($input, $output): int {
|
|
$container = $this->kernel->container();
|
|
$console = new ConsoleApplication('Vallarx Console', Kernel::VERSION);
|
|
$console->setAutoExit(false);
|
|
|
|
/** @var ModuleManager $moduleManager */
|
|
$moduleManager = $container->get(ModuleManager::class);
|
|
foreach ($moduleManager->list() as $module) {
|
|
$instance = $module->instance();
|
|
if (!$instance instanceof ModuleConsoleInterface) {
|
|
continue;
|
|
}
|
|
|
|
foreach ($instance->registerCI() as $commandClass) {
|
|
$this->registerCommand($console, $container, $commandClass);
|
|
}
|
|
}
|
|
|
|
return $console->run($input, $output);
|
|
}
|
|
);
|
|
}
|
|
|
|
/**
|
|
* @param class-string $commandClass
|
|
*/
|
|
private function registerCommand(ConsoleApplication $console, ContainerInterface $container, string $commandClass): void {
|
|
if (!class_exists($commandClass)) {
|
|
throw new \RuntimeException("Command class not found: {$commandClass}");
|
|
}
|
|
|
|
$reflection = new \ReflectionClass($commandClass);
|
|
$attributes = $reflection->getAttributes(AsCommand::class);
|
|
if ($attributes === []) {
|
|
throw new \RuntimeException("Command {$commandClass} is missing #[AsCommand].");
|
|
}
|
|
|
|
$attribute = $attributes[0]->newInstance();
|
|
$console->add(new LazyCommand(
|
|
$attribute->name,
|
|
[],
|
|
$attribute->description ?? '',
|
|
$attribute->hidden ?? false,
|
|
static fn() => $container->get($commandClass),
|
|
));
|
|
}
|
|
}
|