feat: introduce module system and extract address book as first module (MODULAR-MONOLITH-V1-001)
Add a module kernel (ModuleManifest, ModuleRegistry) that allows modules to contribute routes, UI slots, search providers, layout context, session lifecycle, and permissions. Modules are activated via config/modules.php or APP_ENABLED_MODULES env variable. Conflicts (duplicate routes, permissions, slot keys) cause a fail-fast with a clear error message. Extract the address book from hardcoded Core integration points into the first module (modules/addressbook/). The module provides: - Aside icon-bar tab + People panel via UI slot system - Global search resource via AddressBookSearchProvider - Layout context data via AddressBookLayoutProvider - Session lifecycle via AddressBookSessionProvider Core cleanup removes address-book hardcodings from SearchSqlResourceProvider, SearchUiMetaProvider, SearchItemMapperProvider, appBuildLayoutNavContext(), and the aside templates. Permissions (ADDRESS_BOOK_VIEW) and business logic (AddressBookService) remain in Core as they gate general user visibility. Includes 38 new tests (894 total), PHPStan level 5 clean, and architecture tests verifying zero hardcoded address-book references in search/templates. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -2,6 +2,8 @@
|
||||
|
||||
namespace MintyPHP\Support;
|
||||
|
||||
use MintyPHP\App\Module\Contracts\SearchResourceProvider;
|
||||
use MintyPHP\App\Module\ModuleRegistry;
|
||||
use MintyPHP\Support\Search\SearchItemMapperProvider;
|
||||
use MintyPHP\Support\Search\SearchQueryNormalizer;
|
||||
use MintyPHP\Support\Search\SearchSpecialResourceProvider;
|
||||
@@ -10,9 +12,26 @@ use MintyPHP\Support\Search\SearchUiMetaProvider;
|
||||
|
||||
class SearchConfig
|
||||
{
|
||||
/** @var list<SearchResourceProvider> resolved module providers (cached per request) */
|
||||
private static ?array $moduleProviders = null;
|
||||
|
||||
/** @var array<string, SearchResourceProvider> key→provider lookup */
|
||||
private static array $moduleProviderByKey = [];
|
||||
|
||||
public static function tenantScopeFilters(): array
|
||||
{
|
||||
return SearchSqlResourceProvider::tenantScopeFilters();
|
||||
$filters = SearchSqlResourceProvider::tenantScopeFilters();
|
||||
|
||||
// Module providers may contribute tenant scope filters via their resource definitions
|
||||
foreach (self::resolveModuleProviders() as $provider) {
|
||||
foreach ($provider->resources() as $key => $resource) {
|
||||
if (isset($resource['tenant_filter']) && $resource['tenant_filter'] !== '') {
|
||||
$filters[$key] = $resource['tenant_filter'];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $filters;
|
||||
}
|
||||
|
||||
public static function resources(string $query, string $locale): array
|
||||
@@ -20,26 +39,134 @@ class SearchConfig
|
||||
return SearchSqlResourceProvider::resources($query, $locale);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return module-contributed search resource definitions.
|
||||
* These are separate from core SQL resources and processed by SearchDataService.
|
||||
*
|
||||
* @return array<string, array{label: string, permission: string, count_sql: string, preview_sql: string, result_sql: string, tenant_filter?: string}>
|
||||
*/
|
||||
public static function moduleResources(): array
|
||||
{
|
||||
$resources = [];
|
||||
foreach (self::resolveModuleProviders() as $provider) {
|
||||
foreach ($provider->resources() as $key => $resource) {
|
||||
$resources[$key] = $resource;
|
||||
}
|
||||
}
|
||||
return $resources;
|
||||
}
|
||||
|
||||
public static function iconForKey(string $key): string
|
||||
{
|
||||
// Check module provider key→icon mapping
|
||||
$moduleProvider = self::$moduleProviderByKey[$key] ?? null;
|
||||
if ($moduleProvider !== null) {
|
||||
$mapped = $moduleProvider->mapResultItem($key, []);
|
||||
if ($mapped !== null && isset($mapped['icon']) && $mapped['icon'] !== '') {
|
||||
return $mapped['icon'];
|
||||
}
|
||||
}
|
||||
|
||||
return SearchUiMetaProvider::iconForKey($key);
|
||||
}
|
||||
|
||||
public static function listUrl(string $key, string $query): string
|
||||
{
|
||||
// Ensure module providers are resolved before lookup
|
||||
self::resolveModuleProviders();
|
||||
$provider = self::$moduleProviderByKey[$key] ?? null;
|
||||
if ($provider !== null) {
|
||||
$url = $provider->listUrl($key, urlencode($query));
|
||||
if ($url !== '') {
|
||||
return $url;
|
||||
}
|
||||
}
|
||||
|
||||
return SearchUiMetaProvider::listUrl($key, $query);
|
||||
}
|
||||
|
||||
public static function mapPreviewItem(string $key, array $data, string $query): ?array
|
||||
{
|
||||
self::resolveModuleProviders();
|
||||
$provider = self::$moduleProviderByKey[$key] ?? null;
|
||||
if ($provider !== null) {
|
||||
$mapped = $provider->mapResultItem($key, $data);
|
||||
if ($mapped !== null) {
|
||||
return ['label' => $mapped['title'], 'url' => $mapped['url']];
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
return SearchItemMapperProvider::mapPreviewItem($key, $data, $query);
|
||||
}
|
||||
|
||||
public static function mapResultItem(string $key, string $label, array $data): ?array
|
||||
{
|
||||
self::resolveModuleProviders();
|
||||
$provider = self::$moduleProviderByKey[$key] ?? null;
|
||||
if ($provider !== null) {
|
||||
$mapped = $provider->mapResultItem($key, $data);
|
||||
if ($mapped !== null) {
|
||||
return [
|
||||
'type' => $label,
|
||||
'title' => $mapped['title'],
|
||||
'description' => $mapped['subtitle'] ?? '',
|
||||
'url' => $mapped['url'],
|
||||
'image' => '',
|
||||
'icon' => $mapped['icon'] ?? self::iconForKey($key),
|
||||
];
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
return SearchItemMapperProvider::mapResultItem($key, $label, $data);
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolve and cache module search providers for the current request.
|
||||
*
|
||||
* @return list<SearchResourceProvider>
|
||||
*/
|
||||
private static function resolveModuleProviders(): array
|
||||
{
|
||||
if (self::$moduleProviders !== null) {
|
||||
return self::$moduleProviders;
|
||||
}
|
||||
|
||||
self::$moduleProviders = [];
|
||||
self::$moduleProviderByKey = [];
|
||||
|
||||
try {
|
||||
/** @var ModuleRegistry $registry */
|
||||
$registry = app(ModuleRegistry::class);
|
||||
foreach ($registry->getSearchResources() as $providerClass) {
|
||||
if (!class_exists($providerClass)) {
|
||||
continue;
|
||||
}
|
||||
$provider = new $providerClass();
|
||||
if ($provider instanceof SearchResourceProvider) {
|
||||
self::$moduleProviders[] = $provider;
|
||||
foreach ($provider->resources() as $key => $resource) {
|
||||
self::$moduleProviderByKey[$key] = $provider;
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (\Throwable) {
|
||||
// fail-open: module registry may not be available
|
||||
}
|
||||
|
||||
return self::$moduleProviders;
|
||||
}
|
||||
|
||||
/**
|
||||
* Reset cached module providers (for testing).
|
||||
*/
|
||||
public static function resetModuleProviders(): void
|
||||
{
|
||||
self::$moduleProviders = null;
|
||||
self::$moduleProviderByKey = [];
|
||||
}
|
||||
|
||||
public static function hotkeyPreviewResource(string $query): ?array
|
||||
{
|
||||
return SearchSpecialResourceProvider::hotkeyPreviewResource($query);
|
||||
|
||||
Reference in New Issue
Block a user