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

@@ -102,6 +102,9 @@ class Module extends ModuleInstanceAbstract implements ModuleConsoleInterface, M
\KTXC\Console\ModuleListCommand::class,
\KTXC\Console\ModuleEnableCommand::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;
use KTXC\Application;
use KTXC\Server;
/**
* Custom autoloader for modules that allows PascalCase namespaces
@@ -73,7 +73,7 @@ class ModuleAutoloader
}
// Register module namespaces with Composer ClassLoader
$composerLoader = \KTXC\Application::getComposerLoader();
$composerLoader = Server::getComposerLoader();
if ($composerLoader !== null) {
foreach ($this->namespaceMap as $namespace => $folderName) {
$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 $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();
@@ -44,12 +44,8 @@ class ModuleManager
}
// load all modules from store
$entries = $this->repository->list();
$entries = $this->repository->list($installedOnly, $enabledOnly);
foreach ($entries as $entry) {
if ($enabledOnly && !$entry->getEnabled()) {
continue; // Skip disabled modules if filtering for enabled only
}
// instance module
$handle = $entry->getHandle();
if (isset($this->moduleInstances[$entry->getHandle()])) {
$modules[$handle] = new ModuleObject($this->moduleInstances[$handle], $entry);
@@ -60,7 +56,7 @@ class ModuleManager
}
}
// load all modules from filesystem
if ($installedOnly === false) {
if ($installedOnly !== true) {
$discovered = $this->modulesDiscover();
foreach ($discovered as $moduleInstance) {
$handle = $moduleInstance->handle();
@@ -72,6 +68,21 @@ class ModuleManager
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
{
@@ -258,7 +269,7 @@ class ModuleManager
public function modulesBoot(): void
{
// 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)]);
foreach ($modules as $module) {
$handle = $module->handle();

View File

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

View File

@@ -3,6 +3,7 @@
namespace KTXC\Module\Store;
use KTXC\Db\DataStore;
use KTXC\Db\ObjectId;
class ModuleStore
{
@@ -13,9 +14,18 @@ class ModuleStore
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 = [];
foreach ($cursor as $entry) {
$entity = new ModuleEntry();
@@ -52,7 +62,11 @@ class ModuleStore
{
$id = $entry->getId();
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;
}
@@ -60,7 +74,7 @@ class ModuleStore
{
$id = $entry->getId();
if (!$id) { return; }
$this->dataStore->selectCollection(self::COLLECTION_NAME)->deleteOne([ '_id' => $id]);
$result = $this->dataStore->selectCollection(self::COLLECTION_NAME)->deleteOne(['_id' => new ObjectId($id)]);
}
}