implement console

This commit is contained in:
root
2026-01-07 00:05:55 -05:00
parent 965d839a8c
commit 3324157ed9
7 changed files with 1064 additions and 8 deletions

View 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;
}
}
}

View 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 Enable Command
*
* Enables a disabled module.
*/
#[AsCommand(
name: 'module:enable',
description: 'Enable a module',
)]
class ModuleEnableCommand 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 enable')
->setHelp('This command enables a previously disabled module.')
;
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
$io = new SymfonyStyle($input, $output);
$handle = $input->getArgument('handle');
$io->title('Enable Module');
try {
// 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 enabled.");
return Command::SUCCESS;
}
// Enable the module
$io->text("Enabling module '{$handle}'...");
$this->moduleManager->enable($handle);
$this->logger->info('Module enabled via console', [
'handle' => $handle,
'command' => $this->getName(),
]);
$io->success("Module '{$handle}' enabled successfully!");
return Command::SUCCESS;
} catch (\Throwable $e) {
$io->error('Failed to enable module: ' . $e->getMessage());
$this->logger->error('Module enable failed', [
'handle' => $handle,
'error' => $e->getMessage(),
]);
return Command::FAILURE;
}
}
}

View File

@@ -0,0 +1,86 @@
<?php
declare(strict_types=1);
namespace KTXC\Console;
use KTXC\Module\ModuleManager;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
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 List Command
*
* Lists all modules with their status and version information.
*/
#[AsCommand(
name: 'module:list',
description: 'List all modules with their status and versions',
)]
class ModuleListCommand extends Command
{
public function __construct(
private readonly ModuleManager $moduleManager
) {
parent::__construct();
}
protected function configure(): void
{
$this
->addOption('all', 'a', InputOption::VALUE_NONE, 'Show all modules including disabled ones')
->setHelp('This command lists all installed modules with their status and version information.')
;
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
$io = new SymfonyStyle($input, $output);
$showAll = $input->getOption('all');
$io->title('Installed Modules');
try {
$modules = $this->moduleManager->list(
installedOnly: true,
enabledOnly: !$showAll
);
if (count($modules) === 0) {
$io->warning('No modules found.');
return Command::SUCCESS;
}
$rows = [];
foreach ($modules as $module) {
$status = $module->enabled() ? '<fg=green>Enabled</>' : '<fg=yellow>Disabled</>';
$upgrade = $module->needsUpgrade() ? '<fg=red>Yes</>' : '';
$rows[] = [
$module->handle(),
$module->version(),
$status,
$upgrade,
$module->namespace() ?? 'N/A',
];
}
$io->table(
['Handle', 'Version', 'Status', 'Needs Upgrade', 'Namespace'],
$rows
);
$io->success(sprintf('Found %d module(s).', count($modules)));
return Command::SUCCESS;
} catch (\Throwable $e) {
$io->error('Failed to list modules: ' . $e->getMessage());
return Command::FAILURE;
}
}
}