feat: improve module management
Signed-off-by: Sebastian Krupinski <krupinski01@gmail.com>
This commit is contained in:
@@ -54,8 +54,7 @@ class ModuleDisableCommand extends Command
|
||||
}
|
||||
|
||||
// Find the module
|
||||
$modules = $this->moduleManager->list(installedOnly: true, enabledOnly: false);
|
||||
$module = $modules[$handle] ?? null;
|
||||
$module = $this->moduleManager->fetch($handle);
|
||||
|
||||
if (!$module) {
|
||||
$io->error("Module '{$handle}' not found or not installed.");
|
||||
|
||||
@@ -48,8 +48,7 @@ class ModuleEnableCommand extends Command
|
||||
|
||||
try {
|
||||
// Find the module
|
||||
$modules = $this->moduleManager->list(installedOnly: true, enabledOnly: false);
|
||||
$module = $modules[$handle] ?? null;
|
||||
$module = $this->moduleManager->fetch($handle);
|
||||
|
||||
if (!$module) {
|
||||
$io->error("Module '{$handle}' not found or not installed.");
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -45,10 +45,7 @@ class ModuleListCommand extends Command
|
||||
$io->title('Installed Modules');
|
||||
|
||||
try {
|
||||
$modules = $this->moduleManager->list(
|
||||
installedOnly: true,
|
||||
enabledOnly: !$showAll
|
||||
);
|
||||
$modules = $this->moduleManager->list();
|
||||
|
||||
if (count($modules) === 0) {
|
||||
$io->warning('No modules found.');
|
||||
|
||||
95
core/lib/Console/ModuleUninstallCommand.php
Normal file
95
core/lib/Console/ModuleUninstallCommand.php
Normal file
@@ -0,0 +1,95 @@
|
||||
<?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\Input\InputOption;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
use Symfony\Component\Console\Style\SymfonyStyle;
|
||||
|
||||
/**
|
||||
* Module Uninstall Command
|
||||
*
|
||||
* Uninstalls an installed module.
|
||||
*/
|
||||
#[AsCommand(
|
||||
name: 'module:uninstall',
|
||||
description: 'Uninstall a module',
|
||||
)]
|
||||
class ModuleUninstallCommand 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 uninstall')
|
||||
->addOption('force', 'f', InputOption::VALUE_NONE, 'Skip confirmation prompt')
|
||||
->setHelp('This command uninstalls an installed module.')
|
||||
;
|
||||
}
|
||||
|
||||
protected function execute(InputInterface $input, OutputInterface $output): int
|
||||
{
|
||||
$io = new SymfonyStyle($input, $output);
|
||||
$handle = $input->getArgument('handle');
|
||||
$force = $input->getOption('force');
|
||||
|
||||
$io->title('Uninstall Module');
|
||||
|
||||
try {
|
||||
// Prevent uninstalling core module
|
||||
if ($handle === 'core') {
|
||||
$io->error('Cannot uninstall the core module.');
|
||||
return Command::FAILURE;
|
||||
}
|
||||
|
||||
// Find the module
|
||||
$module = $this->moduleManager->fetch($handle);
|
||||
|
||||
if (!$module) {
|
||||
$io->error("Module '{$handle}' not found or not installed.");
|
||||
return Command::FAILURE;
|
||||
}
|
||||
|
||||
// Confirm unless --force is passed
|
||||
if (!$force && !$io->confirm("Are you sure you want to uninstall module '{$handle}'?", false)) {
|
||||
$io->text('Uninstall cancelled.');
|
||||
return Command::SUCCESS;
|
||||
}
|
||||
|
||||
// Uninstall the module
|
||||
$io->text("Uninstalling module '{$handle}'...");
|
||||
$this->moduleManager->uninstall($handle);
|
||||
|
||||
$this->logger->info('Module uninstalled via console', [
|
||||
'handle' => $handle,
|
||||
'command' => $this->getName(),
|
||||
]);
|
||||
|
||||
$io->success("Module '{$handle}' uninstalled successfully!");
|
||||
|
||||
return Command::SUCCESS;
|
||||
|
||||
} catch (\Throwable $e) {
|
||||
$io->error('Failed to uninstall module: ' . $e->getMessage());
|
||||
$this->logger->error('Module uninstall failed', [
|
||||
'handle' => $handle,
|
||||
'error' => $e->getMessage(),
|
||||
]);
|
||||
return Command::FAILURE;
|
||||
}
|
||||
}
|
||||
}
|
||||
144
core/lib/Console/ModuleUpgradeCommand.php
Normal file
144
core/lib/Console/ModuleUpgradeCommand.php
Normal file
@@ -0,0 +1,144 @@
|
||||
<?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\Input\InputOption;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
use Symfony\Component\Console\Style\SymfonyStyle;
|
||||
|
||||
/**
|
||||
* Module Upgrade Command
|
||||
*
|
||||
* Upgrades an installed module to its latest version.
|
||||
*/
|
||||
#[AsCommand(
|
||||
name: 'module:upgrade',
|
||||
description: 'Upgrade a module',
|
||||
)]
|
||||
class ModuleUpgradeCommand extends Command
|
||||
{
|
||||
public function __construct(
|
||||
private readonly ModuleManager $moduleManager,
|
||||
private readonly LoggerInterface $logger
|
||||
) {
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
protected function configure(): void
|
||||
{
|
||||
$this
|
||||
->addArgument('handle', InputArgument::OPTIONAL, 'Module handle to upgrade (omit to upgrade all)')
|
||||
->addOption('all', 'a', InputOption::VALUE_NONE, 'Upgrade all modules that need upgrading')
|
||||
->setHelp('This command upgrades an installed module. Use --all to upgrade all modules that need upgrading.')
|
||||
;
|
||||
}
|
||||
|
||||
protected function execute(InputInterface $input, OutputInterface $output): int
|
||||
{
|
||||
$io = new SymfonyStyle($input, $output);
|
||||
$handle = $input->getArgument('handle');
|
||||
$all = $input->getOption('all');
|
||||
|
||||
$io->title('Upgrade Module');
|
||||
|
||||
try {
|
||||
if ($all || !$handle) {
|
||||
return $this->upgradeAll($io);
|
||||
}
|
||||
|
||||
return $this->upgradeOne($io, $handle);
|
||||
|
||||
} catch (\Throwable $e) {
|
||||
$io->error('Failed to upgrade module: ' . $e->getMessage());
|
||||
$this->logger->error('Module upgrade failed', [
|
||||
'handle' => $handle,
|
||||
'error' => $e->getMessage(),
|
||||
]);
|
||||
return Command::FAILURE;
|
||||
}
|
||||
}
|
||||
|
||||
private function upgradeOne(SymfonyStyle $io, string $handle): int
|
||||
{
|
||||
// Find the module
|
||||
$module = $this->moduleManager->fetch($handle);
|
||||
|
||||
if (!$module) {
|
||||
$io->error("Module '{$handle}' not found or not installed.");
|
||||
return Command::FAILURE;
|
||||
}
|
||||
|
||||
if (!$module->needsUpgrade()) {
|
||||
$io->success("Module '{$handle}' is already up to date.");
|
||||
return Command::SUCCESS;
|
||||
}
|
||||
|
||||
$io->text("Upgrading module '{$handle}'...");
|
||||
$this->moduleManager->upgrade($handle);
|
||||
|
||||
$this->logger->info('Module upgraded via console', [
|
||||
'handle' => $handle,
|
||||
'command' => $this->getName(),
|
||||
]);
|
||||
|
||||
$io->success("Module '{$handle}' upgraded successfully!");
|
||||
|
||||
return Command::SUCCESS;
|
||||
}
|
||||
|
||||
private function upgradeAll(SymfonyStyle $io): int
|
||||
{
|
||||
$modules = $this->moduleManager->list();
|
||||
$pending = [];
|
||||
|
||||
foreach ($modules as $module) {
|
||||
if ($module->needsUpgrade()) {
|
||||
$pending[] = $module->handle();
|
||||
}
|
||||
}
|
||||
|
||||
if (count($pending) === 0) {
|
||||
$io->success('All modules are up to date.');
|
||||
return Command::SUCCESS;
|
||||
}
|
||||
|
||||
$io->text(sprintf('Found %d module(s) to upgrade: %s', count($pending), implode(', ', $pending)));
|
||||
|
||||
$failed = [];
|
||||
foreach ($pending as $handle) {
|
||||
try {
|
||||
$io->text("Upgrading module '{$handle}'...");
|
||||
$this->moduleManager->upgrade($handle);
|
||||
$this->logger->info('Module upgraded via console', [
|
||||
'handle' => $handle,
|
||||
'command' => $this->getName(),
|
||||
]);
|
||||
$io->text("<fg=green>✓ '{$handle}' upgraded.</>");
|
||||
} catch (\Throwable $e) {
|
||||
$failed[] = $handle;
|
||||
$io->text("<fg=red>✗ '{$handle}' failed: {$e->getMessage()}</>");
|
||||
$this->logger->error('Module upgrade failed', [
|
||||
'handle' => $handle,
|
||||
'error' => $e->getMessage(),
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
if (count($failed) > 0) {
|
||||
$io->error(sprintf('Failed to upgrade %d module(s): %s', count($failed), implode(', ', $failed)));
|
||||
return Command::FAILURE;
|
||||
}
|
||||
|
||||
$io->success(sprintf('Successfully upgraded %d module(s).', count($pending)));
|
||||
|
||||
return Command::SUCCESS;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user