145 lines
4.4 KiB
PHP
145 lines
4.4 KiB
PHP
<?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;
|
|
}
|
|
}
|