feat: improve module management
Signed-off-by: Sebastian Krupinski <krupinski01@gmail.com>
This commit is contained in:
86
core/lib/Console/ModuleInstallCommand.php
Normal file
86
core/lib/Console/ModuleInstallCommand.php
Normal file
@@ -0,0 +1,86 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace KTXC\Console;
|
||||
|
||||
use KTXC\Module\ModuleManager;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use Symfony\Component\Console\Attribute\AsCommand;
|
||||
use Symfony\Component\Console\Command\Command;
|
||||
use Symfony\Component\Console\Input\InputArgument;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
use Symfony\Component\Console\Style\SymfonyStyle;
|
||||
|
||||
/**
|
||||
* Module Install Command
|
||||
*
|
||||
* Installs a module from the filesystem.
|
||||
*/
|
||||
#[AsCommand(
|
||||
name: 'module:install',
|
||||
description: 'Install a module',
|
||||
)]
|
||||
class ModuleInstallCommand extends Command
|
||||
{
|
||||
public function __construct(
|
||||
private readonly ModuleManager $moduleManager,
|
||||
private readonly LoggerInterface $logger
|
||||
) {
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
protected function configure(): void
|
||||
{
|
||||
$this
|
||||
->addArgument('handle', InputArgument::REQUIRED, 'Module handle to install')
|
||||
->setHelp('This command installs a module from the filesystem.')
|
||||
;
|
||||
}
|
||||
|
||||
protected function execute(InputInterface $input, OutputInterface $output): int
|
||||
{
|
||||
$io = new SymfonyStyle($input, $output);
|
||||
$handle = $input->getArgument('handle');
|
||||
|
||||
$io->title('Install Module');
|
||||
|
||||
try {
|
||||
// Prevent installing core module
|
||||
if ($handle === 'core') {
|
||||
$io->error('Cannot install the core module.');
|
||||
return Command::FAILURE;
|
||||
}
|
||||
|
||||
// Check if the module is already installed
|
||||
$module = $this->moduleManager->fetch($handle);
|
||||
|
||||
if ($module) {
|
||||
$io->warning("Module '{$handle}' is already installed.");
|
||||
return Command::SUCCESS;
|
||||
}
|
||||
|
||||
// Install the module
|
||||
$io->text("Installing module '{$handle}'...");
|
||||
$this->moduleManager->install($handle);
|
||||
|
||||
$this->logger->info('Module installed via console', [
|
||||
'handle' => $handle,
|
||||
'command' => $this->getName(),
|
||||
]);
|
||||
|
||||
$io->success("Module '{$handle}' installed successfully!");
|
||||
|
||||
return Command::SUCCESS;
|
||||
|
||||
} catch (\Throwable $e) {
|
||||
$io->error('Failed to install module: ' . $e->getMessage());
|
||||
$this->logger->error('Module install failed', [
|
||||
'handle' => $handle,
|
||||
'error' => $e->getMessage(),
|
||||
]);
|
||||
return Command::FAILURE;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user