Initial Version
This commit is contained in:
92
core/lib/Console/ModuleDisableCommand.php
Normal file
92
core/lib/Console/ModuleDisableCommand.php
Normal file
@@ -0,0 +1,92 @@
|
||||
<?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 Disable Command
|
||||
*
|
||||
* Disables an enabled module.
|
||||
*/
|
||||
#[AsCommand(
|
||||
name: 'module:disable',
|
||||
description: 'Disable a module',
|
||||
)]
|
||||
class ModuleDisableCommand 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 disable')
|
||||
->setHelp('This command disables an enabled module without uninstalling it.')
|
||||
;
|
||||
}
|
||||
|
||||
protected function execute(InputInterface $input, OutputInterface $output): int
|
||||
{
|
||||
$io = new SymfonyStyle($input, $output);
|
||||
$handle = $input->getArgument('handle');
|
||||
|
||||
$io->title('Disable Module');
|
||||
|
||||
try {
|
||||
// Prevent disabling core module
|
||||
if ($handle === 'core') {
|
||||
$io->error('Cannot disable the core module.');
|
||||
return Command::FAILURE;
|
||||
}
|
||||
|
||||
// Find the module
|
||||
$modules = $this->moduleManager->list(installedOnly: true, enabledOnly: false);
|
||||
$module = $modules[$handle] ?? null;
|
||||
|
||||
if (!$module) {
|
||||
$io->error("Module '{$handle}' not found or not installed.");
|
||||
return Command::FAILURE;
|
||||
}
|
||||
|
||||
if (!$module->enabled()) {
|
||||
$io->warning("Module '{$handle}' is already disabled.");
|
||||
return Command::SUCCESS;
|
||||
}
|
||||
|
||||
// Disable the module
|
||||
$io->text("Disabling module '{$handle}'...");
|
||||
$this->moduleManager->disable($handle);
|
||||
|
||||
$this->logger->info('Module disabled via console', [
|
||||
'handle' => $handle,
|
||||
'command' => $this->getName(),
|
||||
]);
|
||||
|
||||
$io->success("Module '{$handle}' disabled successfully!");
|
||||
|
||||
return Command::SUCCESS;
|
||||
|
||||
} catch (\Throwable $e) {
|
||||
$io->error('Failed to disable module: ' . $e->getMessage());
|
||||
$this->logger->error('Module disable failed', [
|
||||
'handle' => $handle,
|
||||
'error' => $e->getMessage(),
|
||||
]);
|
||||
return Command::FAILURE;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user