Files
server/core/lib/Console/Module/ModuleUninstallCommand.php
T
2026-07-19 22:39:28 -04:00

96 lines
2.9 KiB
PHP

<?php
declare(strict_types=1);
namespace KTXC\Console\Module;
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;
}
}
}