feat: tenant management console commands
Signed-off-by: Sebastian Krupinski <krupinski01@gmail.com>
This commit is contained in:
@@ -0,0 +1,91 @@
|
||||
<?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
|
||||
$module = $this->moduleManager->fetch($handle);
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,85 @@
|
||||
<?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
|
||||
$module = $this->moduleManager->fetch($handle);
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,83 @@
|
||||
<?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();
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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