feat: extend module platform with UI slots, runtime components, CLI tooling and {{userId}} search support
Completes the generic module platform that enables modules to contribute
UI elements, runtime JS components, and search resources without any
core hardcoding.
New generic UI slot types:
- topbar.right_item: module-contributed topbar buttons
- layout.body_end_template: module-contributed dialog/overlay templates
- layout.head_style: module-contributed global CSS
- runtime.component: declarative JS component registration with phase ordering
New infrastructure:
- ModuleAutoloader: PSR-4 autoloading for module-local PHP classes
- ModuleRuntimePageBuilder: symlinks module pages into runtime directory
- ModuleRuntimeAssetPublisher: publishes module CSS/JS to web/modules/
- ModulePermissionSynchronizer: syncs module permissions to DB
- CLI scripts: module-runtime-sync, module-build, module-migrate,
module-permissions-sync, module-assets-sync
- {{userId}} placeholder in SearchDataService for user-scoped search queries
- Component runtime with phased initialization (early/default/late)
- AppContainer.protectExistingBindings() to prevent module→core overwrites
- Architecture tests: ModuleStructureContractTest, CoreTemplateIsolationTest,
FrontendComponentRuntimeContractTest, AppContainerIsolationContractTest
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-18 22:19:56 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace MintyPHP\App\Module;
|
|
|
|
|
|
|
|
|
|
use MintyPHP\Repository\Access\PermissionRepositoryInterface;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Synchronizes module-defined permissions into the permissions table.
|
|
|
|
|
*/
|
|
|
|
|
final class ModulePermissionSynchronizer
|
|
|
|
|
{
|
|
|
|
|
public function __construct(
|
|
|
|
|
private readonly ModuleRegistry $moduleRegistry,
|
|
|
|
|
private readonly PermissionRepositoryInterface $permissionRepository
|
|
|
|
|
) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2026-03-19 18:41:08 +01:00
|
|
|
* @return array{created: int, updated: int, unchanged: int, deactivated: int, total: int}
|
feat: extend module platform with UI slots, runtime components, CLI tooling and {{userId}} search support
Completes the generic module platform that enables modules to contribute
UI elements, runtime JS components, and search resources without any
core hardcoding.
New generic UI slot types:
- topbar.right_item: module-contributed topbar buttons
- layout.body_end_template: module-contributed dialog/overlay templates
- layout.head_style: module-contributed global CSS
- runtime.component: declarative JS component registration with phase ordering
New infrastructure:
- ModuleAutoloader: PSR-4 autoloading for module-local PHP classes
- ModuleRuntimePageBuilder: symlinks module pages into runtime directory
- ModuleRuntimeAssetPublisher: publishes module CSS/JS to web/modules/
- ModulePermissionSynchronizer: syncs module permissions to DB
- CLI scripts: module-runtime-sync, module-build, module-migrate,
module-permissions-sync, module-assets-sync
- {{userId}} placeholder in SearchDataService for user-scoped search queries
- Component runtime with phased initialization (early/default/late)
- AppContainer.protectExistingBindings() to prevent module→core overwrites
- Architecture tests: ModuleStructureContractTest, CoreTemplateIsolationTest,
FrontendComponentRuntimeContractTest, AppContainerIsolationContractTest
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-18 22:19:56 +01:00
|
|
|
*/
|
|
|
|
|
public function sync(): array
|
|
|
|
|
{
|
|
|
|
|
$created = 0;
|
|
|
|
|
$updated = 0;
|
|
|
|
|
$unchanged = 0;
|
|
|
|
|
$total = 0;
|
|
|
|
|
|
|
|
|
|
foreach ($this->moduleRegistry->getPermissions() as $permissionDefinition) {
|
|
|
|
|
$total++;
|
|
|
|
|
$key = trim((string) $permissionDefinition['key']);
|
|
|
|
|
if ($key === '') {
|
|
|
|
|
throw new \RuntimeException('Module permission definition has empty key.');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$desired = [
|
|
|
|
|
'key' => $key,
|
|
|
|
|
'description' => trim((string) $permissionDefinition['description']),
|
|
|
|
|
'active' => (int) $permissionDefinition['active'],
|
|
|
|
|
'is_system' => (int) $permissionDefinition['is_system'],
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
$existing = $this->permissionRepository->findByKey($key);
|
|
|
|
|
if (!is_array($existing)) {
|
|
|
|
|
$createdId = $this->permissionRepository->create($desired);
|
|
|
|
|
if ($createdId === null) {
|
|
|
|
|
throw new \RuntimeException("Failed to create module permission '{$key}'.");
|
|
|
|
|
}
|
|
|
|
|
$created++;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$existingId = (int) ($existing['id'] ?? 0);
|
|
|
|
|
if ($existingId <= 0) {
|
|
|
|
|
throw new \RuntimeException("Permission '{$key}' exists without a valid id.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($this->isPermissionEqual($existing, $desired)) {
|
|
|
|
|
$unchanged++;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$wasUpdated = $this->permissionRepository->update($existingId, $desired);
|
|
|
|
|
if (!$wasUpdated) {
|
|
|
|
|
throw new \RuntimeException("Failed to update module permission '{$key}' (id {$existingId}).");
|
|
|
|
|
}
|
|
|
|
|
$updated++;
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-19 18:41:08 +01:00
|
|
|
$deactivated = $this->deactivateOrphaned();
|
|
|
|
|
|
feat: extend module platform with UI slots, runtime components, CLI tooling and {{userId}} search support
Completes the generic module platform that enables modules to contribute
UI elements, runtime JS components, and search resources without any
core hardcoding.
New generic UI slot types:
- topbar.right_item: module-contributed topbar buttons
- layout.body_end_template: module-contributed dialog/overlay templates
- layout.head_style: module-contributed global CSS
- runtime.component: declarative JS component registration with phase ordering
New infrastructure:
- ModuleAutoloader: PSR-4 autoloading for module-local PHP classes
- ModuleRuntimePageBuilder: symlinks module pages into runtime directory
- ModuleRuntimeAssetPublisher: publishes module CSS/JS to web/modules/
- ModulePermissionSynchronizer: syncs module permissions to DB
- CLI scripts: module-runtime-sync, module-build, module-migrate,
module-permissions-sync, module-assets-sync
- {{userId}} placeholder in SearchDataService for user-scoped search queries
- Component runtime with phased initialization (early/default/late)
- AppContainer.protectExistingBindings() to prevent module→core overwrites
- Architecture tests: ModuleStructureContractTest, CoreTemplateIsolationTest,
FrontendComponentRuntimeContractTest, AppContainerIsolationContractTest
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-18 22:19:56 +01:00
|
|
|
return [
|
|
|
|
|
'created' => $created,
|
|
|
|
|
'updated' => $updated,
|
|
|
|
|
'unchanged' => $unchanged,
|
2026-03-19 18:41:08 +01:00
|
|
|
'deactivated' => $deactivated,
|
feat: extend module platform with UI slots, runtime components, CLI tooling and {{userId}} search support
Completes the generic module platform that enables modules to contribute
UI elements, runtime JS components, and search resources without any
core hardcoding.
New generic UI slot types:
- topbar.right_item: module-contributed topbar buttons
- layout.body_end_template: module-contributed dialog/overlay templates
- layout.head_style: module-contributed global CSS
- runtime.component: declarative JS component registration with phase ordering
New infrastructure:
- ModuleAutoloader: PSR-4 autoloading for module-local PHP classes
- ModuleRuntimePageBuilder: symlinks module pages into runtime directory
- ModuleRuntimeAssetPublisher: publishes module CSS/JS to web/modules/
- ModulePermissionSynchronizer: syncs module permissions to DB
- CLI scripts: module-runtime-sync, module-build, module-migrate,
module-permissions-sync, module-assets-sync
- {{userId}} placeholder in SearchDataService for user-scoped search queries
- Component runtime with phased initialization (early/default/late)
- AppContainer.protectExistingBindings() to prevent module→core overwrites
- Architecture tests: ModuleStructureContractTest, CoreTemplateIsolationTest,
FrontendComponentRuntimeContractTest, AppContainerIsolationContractTest
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-18 22:19:56 +01:00
|
|
|
'total' => $total,
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-19 18:41:08 +01:00
|
|
|
/**
|
|
|
|
|
* Deactivate system permissions that no active module claims.
|
|
|
|
|
*
|
|
|
|
|
* Only touches is_system=1 rows (module-owned). Admin-created permissions
|
|
|
|
|
* (is_system=0) are never modified. Setting active=0 is reversible — if the
|
|
|
|
|
* module is re-enabled, the next sync will set active=1 again.
|
|
|
|
|
*/
|
|
|
|
|
private function deactivateOrphaned(): int
|
|
|
|
|
{
|
|
|
|
|
$activeKeys = $this->moduleRegistry->getPermissionKeys();
|
|
|
|
|
$systemPermissions = $this->permissionRepository->listSystem();
|
|
|
|
|
$deactivated = 0;
|
|
|
|
|
|
|
|
|
|
foreach ($systemPermissions as $permission) {
|
|
|
|
|
$key = (string) $permission['key'];
|
|
|
|
|
$id = (int) $permission['id'];
|
|
|
|
|
$isActive = (int) $permission['active'];
|
|
|
|
|
|
|
|
|
|
if ($key === '' || $id <= 0) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Already inactive — nothing to do
|
|
|
|
|
if ($isActive === 0) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Still claimed by an active module — keep it
|
|
|
|
|
if (in_array($key, $activeKeys, true)) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Orphaned: system permission not claimed by any active module → deactivate
|
|
|
|
|
$this->permissionRepository->update($id, [
|
|
|
|
|
'key' => $key,
|
|
|
|
|
'description' => (string) $permission['description'],
|
|
|
|
|
'active' => 0,
|
|
|
|
|
'is_system' => 1,
|
|
|
|
|
]);
|
|
|
|
|
$deactivated++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $deactivated;
|
|
|
|
|
}
|
|
|
|
|
|
feat: extend module platform with UI slots, runtime components, CLI tooling and {{userId}} search support
Completes the generic module platform that enables modules to contribute
UI elements, runtime JS components, and search resources without any
core hardcoding.
New generic UI slot types:
- topbar.right_item: module-contributed topbar buttons
- layout.body_end_template: module-contributed dialog/overlay templates
- layout.head_style: module-contributed global CSS
- runtime.component: declarative JS component registration with phase ordering
New infrastructure:
- ModuleAutoloader: PSR-4 autoloading for module-local PHP classes
- ModuleRuntimePageBuilder: symlinks module pages into runtime directory
- ModuleRuntimeAssetPublisher: publishes module CSS/JS to web/modules/
- ModulePermissionSynchronizer: syncs module permissions to DB
- CLI scripts: module-runtime-sync, module-build, module-migrate,
module-permissions-sync, module-assets-sync
- {{userId}} placeholder in SearchDataService for user-scoped search queries
- Component runtime with phased initialization (early/default/late)
- AppContainer.protectExistingBindings() to prevent module→core overwrites
- Architecture tests: ModuleStructureContractTest, CoreTemplateIsolationTest,
FrontendComponentRuntimeContractTest, AppContainerIsolationContractTest
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-18 22:19:56 +01:00
|
|
|
/**
|
|
|
|
|
* @param array<string, mixed> $existing
|
|
|
|
|
* @param array<string, mixed> $desired
|
|
|
|
|
*/
|
|
|
|
|
private function isPermissionEqual(array $existing, array $desired): bool
|
|
|
|
|
{
|
|
|
|
|
$existingDescription = trim((string) ($existing['description'] ?? ''));
|
|
|
|
|
$existingActive = (int) ($existing['active'] ?? 0);
|
|
|
|
|
$existingSystem = (int) ($existing['is_system'] ?? 0);
|
|
|
|
|
|
|
|
|
|
return $existingDescription === (string) ($desired['description'] ?? '')
|
|
|
|
|
&& $existingActive === (int) ($desired['active'] ?? 0)
|
|
|
|
|
&& $existingSystem === (int) ($desired['is_system'] ?? 0);
|
|
|
|
|
}
|
|
|
|
|
}
|