feat: improve module management
All checks were successful
JS Unit Tests / test (pull_request) Successful in 39s
Build Test / build (pull_request) Successful in 44s
PHP Unit Tests / test (pull_request) Successful in 53s

Signed-off-by: Sebastian Krupinski <krupinski01@gmail.com>
This commit is contained in:
2026-02-22 00:40:38 -05:00
parent f44fd85dcc
commit c687bd0795
12 changed files with 391 additions and 28 deletions

View File

@@ -54,8 +54,7 @@ class ModuleDisableCommand extends Command
} }
// Find the module // Find the module
$modules = $this->moduleManager->list(installedOnly: true, enabledOnly: false); $module = $this->moduleManager->fetch($handle);
$module = $modules[$handle] ?? null;
if (!$module) { if (!$module) {
$io->error("Module '{$handle}' not found or not installed."); $io->error("Module '{$handle}' not found or not installed.");

View File

@@ -48,8 +48,7 @@ class ModuleEnableCommand extends Command
try { try {
// Find the module // Find the module
$modules = $this->moduleManager->list(installedOnly: true, enabledOnly: false); $module = $this->moduleManager->fetch($handle);
$module = $modules[$handle] ?? null;
if (!$module) { if (!$module) {
$io->error("Module '{$handle}' not found or not installed."); $io->error("Module '{$handle}' not found or not installed.");

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

View File

@@ -45,10 +45,7 @@ class ModuleListCommand extends Command
$io->title('Installed Modules'); $io->title('Installed Modules');
try { try {
$modules = $this->moduleManager->list( $modules = $this->moduleManager->list();
installedOnly: true,
enabledOnly: !$showAll
);
if (count($modules) === 0) { if (count($modules) === 0) {
$io->warning('No modules found.'); $io->warning('No modules found.');

View File

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

View File

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

View File

@@ -21,7 +21,7 @@ class ModuleController extends ControllerAbstract
)] )]
public function index(): JsonResponse public function index(): JsonResponse
{ {
$modules = $this->moduleManager->list(false); $modules = $this->moduleManager->list();
return new JsonResponse(['modules' => $modules]); return new JsonResponse(['modules' => $modules]);
} }

View File

@@ -102,6 +102,9 @@ class Module extends ModuleInstanceAbstract implements ModuleConsoleInterface, M
\KTXC\Console\ModuleListCommand::class, \KTXC\Console\ModuleListCommand::class,
\KTXC\Console\ModuleEnableCommand::class, \KTXC\Console\ModuleEnableCommand::class,
\KTXC\Console\ModuleDisableCommand::class, \KTXC\Console\ModuleDisableCommand::class,
\KTXC\Console\ModuleInstallCommand::class,
\KTXC\Console\ModuleUninstallCommand::class,
\KTXC\Console\ModuleUpgradeCommand::class,
]; ];
} }

View File

@@ -2,7 +2,7 @@
namespace KTXC\Module; namespace KTXC\Module;
use KTXC\Application; use KTXC\Server;
/** /**
* Custom autoloader for modules that allows PascalCase namespaces * Custom autoloader for modules that allows PascalCase namespaces
@@ -73,7 +73,7 @@ class ModuleAutoloader
} }
// Register module namespaces with Composer ClassLoader // Register module namespaces with Composer ClassLoader
$composerLoader = \KTXC\Application::getComposerLoader(); $composerLoader = Server::getComposerLoader();
if ($composerLoader !== null) { if ($composerLoader !== null) {
foreach ($this->namespaceMap as $namespace => $folderName) { foreach ($this->namespaceMap as $namespace => $folderName) {
$composerLoader->addPsr4( $composerLoader->addPsr4(

View File

@@ -31,9 +31,9 @@ class ModuleManager
* *
* @param bool $installedOnly If true, only return modules that are in the database * @param bool $installedOnly If true, only return modules that are in the database
* @param bool $enabledOnly If true, only return modules that are enabled (implies installedOnly) * @param bool $enabledOnly If true, only return modules that are enabled (implies installedOnly)
* @return Module[] * @return ModuleObject[]
*/ */
public function list(bool $installedOnly = true, $enabledOnly = true): ModuleCollection public function list(bool| null $installedOnly = null, bool| null $enabledOnly = null): ModuleCollection
{ {
$modules = New ModuleCollection(); $modules = New ModuleCollection();
@@ -44,12 +44,8 @@ class ModuleManager
} }
// load all modules from store // load all modules from store
$entries = $this->repository->list(); $entries = $this->repository->list($installedOnly, $enabledOnly);
foreach ($entries as $entry) { foreach ($entries as $entry) {
if ($enabledOnly && !$entry->getEnabled()) {
continue; // Skip disabled modules if filtering for enabled only
}
// instance module
$handle = $entry->getHandle(); $handle = $entry->getHandle();
if (isset($this->moduleInstances[$entry->getHandle()])) { if (isset($this->moduleInstances[$entry->getHandle()])) {
$modules[$handle] = new ModuleObject($this->moduleInstances[$handle], $entry); $modules[$handle] = new ModuleObject($this->moduleInstances[$handle], $entry);
@@ -60,7 +56,7 @@ class ModuleManager
} }
} }
// load all modules from filesystem // load all modules from filesystem
if ($installedOnly === false) { if ($installedOnly !== true) {
$discovered = $this->modulesDiscover(); $discovered = $this->modulesDiscover();
foreach ($discovered as $moduleInstance) { foreach ($discovered as $moduleInstance) {
$handle = $moduleInstance->handle(); $handle = $moduleInstance->handle();
@@ -73,6 +69,21 @@ class ModuleManager
return $modules; return $modules;
} }
public function fetch(string $handle): ?ModuleObject
{
$entry = $this->repository->fetch($handle);
if (!$entry) {
return null;
}
$moduleInstance = $this->moduleInstance($entry->getHandle(), $entry->getNamespace());
if (!$moduleInstance) {
return null;
}
return new ModuleObject($moduleInstance, $entry);
}
public function install(string $handle): void public function install(string $handle): void
{ {
// First, try to find the module by scanning the filesystem // First, try to find the module by scanning the filesystem
@@ -258,7 +269,7 @@ class ModuleManager
public function modulesBoot(): void public function modulesBoot(): void
{ {
// Only load modules that are enabled in the database // Only load modules that are enabled in the database
$modules = $this->list(); $modules = $this->list(true, true);
$this->logger->debug('Booting enabled modules', ['count' => count($modules)]); $this->logger->debug('Booting enabled modules', ['count' => count($modules)]);
foreach ($modules as $module) { foreach ($modules as $module) {
$handle = $module->handle(); $handle = $module->handle();

View File

@@ -9,10 +9,7 @@ use KTXF\Module\ModuleConsoleInterface;
use KTXF\Module\ModuleInstanceInterface; use KTXF\Module\ModuleInstanceInterface;
/** /**
* Module is a unified wrapper that combines both the ModuleInterface instance * Module is a unified wrapper that combines the filesystem module instance and the database module entry.
* (from filesystem) and ModuleEntry (from database) into a single object.
*
* This provides a single source of truth for all module information.
*/ */
class ModuleObject implements JsonSerializable class ModuleObject implements JsonSerializable
{ {
@@ -32,6 +29,9 @@ class ModuleObject implements JsonSerializable
return [ return [
'id' => $this->id(), 'id' => $this->id(),
'handle' => $this->handle(), 'handle' => $this->handle(),
'label' => $this->label(),
'description' => $this->description(),
'author' => $this->author(),
'version' => $this->version(), 'version' => $this->version(),
'namespace' => $this->namespace(), 'namespace' => $this->namespace(),
'installed' => $this->installed(), 'installed' => $this->installed(),
@@ -86,6 +86,21 @@ class ModuleObject implements JsonSerializable
return null; return null;
} }
public function label(): string
{
return $this->instance?->label() ?? '';
}
public function description(): string
{
return $this->instance?->description() ?? '';
}
public function author(): string
{
return $this->instance?->author() ?? '';
}
public function version(): string public function version(): string
{ {
// Prefer current version from filesystem // Prefer current version from filesystem

View File

@@ -3,6 +3,7 @@
namespace KTXC\Module\Store; namespace KTXC\Module\Store;
use KTXC\Db\DataStore; use KTXC\Db\DataStore;
use KTXC\Db\ObjectId;
class ModuleStore class ModuleStore
{ {
@@ -13,9 +14,18 @@ class ModuleStore
protected readonly DataStore $dataStore protected readonly DataStore $dataStore
) { } ) { }
public function list(): array public function list(bool|null $installed = null, bool|null $enabled = null): array
{ {
$cursor = $this->dataStore->selectCollection(self::COLLECTION_NAME)->find(['enabled' => true, 'installed' => true]); $filter = [];
if ($installed !== null) {
$filter['installed'] = $installed;
}
if ($enabled !== null) {
$filter['enabled'] = $enabled;
}
$cursor = $this->dataStore->selectCollection(self::COLLECTION_NAME)->find($filter);
$modules = []; $modules = [];
foreach ($cursor as $entry) { foreach ($cursor as $entry) {
$entity = new ModuleEntry(); $entity = new ModuleEntry();
@@ -52,7 +62,11 @@ class ModuleStore
{ {
$id = $entry->getId(); $id = $entry->getId();
if (!$id) { return null; } if (!$id) { return null; }
$this->dataStore->selectCollection(self::COLLECTION_NAME)->updateOne(['_id' => $id], ['$set' => $entry->jsonSerialize()]);
$data = $entry->jsonSerialize();
unset($data['id']);
$result = $this->dataStore->selectCollection(self::COLLECTION_NAME)->updateOne(['_id' => new ObjectId($id)], ['$set' => $data]);
return $entry; return $entry;
} }
@@ -60,7 +74,7 @@ class ModuleStore
{ {
$id = $entry->getId(); $id = $entry->getId();
if (!$id) { return; } if (!$id) { return; }
$this->dataStore->selectCollection(self::COLLECTION_NAME)->deleteOne([ '_id' => $id]); $result = $this->dataStore->selectCollection(self::COLLECTION_NAME)->deleteOne(['_id' => new ObjectId($id)]);
} }
} }