2026-03-19 18:20:13 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace MintyPHP\App\Module;
|
|
|
|
|
|
|
|
|
|
use MintyPHP\App\AppContainer;
|
|
|
|
|
use MintyPHP\App\Module\Contracts\EventListener;
|
2026-03-25 10:02:03 +01:00
|
|
|
use MintyPHP\Http\RequestContext;
|
refactor(audit): extract audit domain into self-contained module
Move the entire audit subsystem (system audit, API audit, import audit,
user lifecycle audit, frontend telemetry) from core into modules/audit/.
Core decoupling via interface-based injection:
- AuditRecorderInterface replaces SystemAuditService in 10+ core services
- UserLifecycleAuditInterface / ImportAuditInterface for specialized flows
- NullAuditRecorder fallback when audit module is disabled
- ApiBootstrap/ApiResponse use null-safe callable resolvers
Module structure (modules/audit/):
- Manifest with routes, permissions, scheduler jobs, authorization policy
- 9 services, 8 repositories, 6 domain enums, 4 job handlers
- 33 page files, 4 JS files, 8 test files, migration scripts, i18n
Core cleanup:
- OperationsAuthorizationPolicy, UiCapabilityMap, PermissionService
surgically cleaned of audit-specific constants
- Sidebar template cleared of hardcoded audit navigation
- AuditModuleIsolationContractTest ensures no future core→module coupling
All quality gates pass: 1346 tests (19,276 assertions), PHPStan level 5
clean, architecture contracts verified.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-25 21:12:49 +01:00
|
|
|
use MintyPHP\Service\Audit\AuditRecorderInterface;
|
2026-03-19 18:20:13 +01:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Fire-and-forget event dispatcher for module lifecycle events.
|
|
|
|
|
*
|
|
|
|
|
* Listeners are declared in module manifests and lazy-resolved from the container.
|
|
|
|
|
* A failing listener does not break the dispatch chain — the exception is swallowed
|
|
|
|
|
* so that one misbehaving module cannot disrupt core flows.
|
|
|
|
|
*/
|
|
|
|
|
final class ModuleEventDispatcher
|
|
|
|
|
{
|
|
|
|
|
/**
|
|
|
|
|
* @param array<string, list<array{class: class-string, method: string}>> $listenerMap
|
|
|
|
|
*/
|
|
|
|
|
public function __construct(
|
|
|
|
|
private readonly array $listenerMap,
|
2026-03-25 10:02:03 +01:00
|
|
|
private readonly AppContainer $container,
|
refactor(audit): extract audit domain into self-contained module
Move the entire audit subsystem (system audit, API audit, import audit,
user lifecycle audit, frontend telemetry) from core into modules/audit/.
Core decoupling via interface-based injection:
- AuditRecorderInterface replaces SystemAuditService in 10+ core services
- UserLifecycleAuditInterface / ImportAuditInterface for specialized flows
- NullAuditRecorder fallback when audit module is disabled
- ApiBootstrap/ApiResponse use null-safe callable resolvers
Module structure (modules/audit/):
- Manifest with routes, permissions, scheduler jobs, authorization policy
- 9 services, 8 repositories, 6 domain enums, 4 job handlers
- 33 page files, 4 JS files, 8 test files, migration scripts, i18n
Core cleanup:
- OperationsAuthorizationPolicy, UiCapabilityMap, PermissionService
surgically cleaned of audit-specific constants
- Sidebar template cleared of hardcoded audit navigation
- AuditModuleIsolationContractTest ensures no future core→module coupling
All quality gates pass: 1346 tests (19,276 assertions), PHPStan level 5
clean, architecture contracts verified.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-25 21:12:49 +01:00
|
|
|
private readonly ?AuditRecorderInterface $systemAuditService = null
|
2026-03-19 18:20:13 +01:00
|
|
|
) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param array<string, mixed> $payload
|
|
|
|
|
*/
|
|
|
|
|
public function dispatch(string $event, array $payload): void
|
|
|
|
|
{
|
|
|
|
|
$listeners = $this->listenerMap[$event] ?? [];
|
|
|
|
|
|
|
|
|
|
foreach ($listeners as $descriptor) {
|
2026-03-25 10:02:03 +01:00
|
|
|
$class = '';
|
|
|
|
|
$method = '';
|
2026-03-19 18:20:13 +01:00
|
|
|
try {
|
|
|
|
|
$class = $descriptor['class'];
|
|
|
|
|
$method = $descriptor['method'];
|
|
|
|
|
|
2026-03-25 10:02:03 +01:00
|
|
|
$listener = ModuleClassResolver::resolve($this->container, $class);
|
2026-03-19 18:20:13 +01:00
|
|
|
if (!$listener instanceof EventListener) {
|
2026-03-25 10:02:03 +01:00
|
|
|
continue;
|
2026-03-19 18:20:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$listener->{$method}($event, $payload);
|
2026-03-25 10:02:03 +01:00
|
|
|
} catch (\Throwable $exception) {
|
|
|
|
|
$this->recordListenerFailure($event, $class, $method, $exception);
|
2026-03-19 18:20:13 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-03-25 10:02:03 +01:00
|
|
|
|
|
|
|
|
private function recordListenerFailure(
|
|
|
|
|
string $event,
|
|
|
|
|
string $listenerClass,
|
|
|
|
|
string $listenerMethod,
|
|
|
|
|
\Throwable $exception
|
|
|
|
|
): void {
|
|
|
|
|
if ($this->systemAuditService === null) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$this->systemAuditService->record('module.listener.failed', 'failed', [
|
|
|
|
|
'metadata' => [
|
|
|
|
|
'module_event' => $event,
|
|
|
|
|
'listener_class' => $listenerClass,
|
|
|
|
|
'listener_method' => $listenerMethod,
|
|
|
|
|
'exception_class' => $exception::class,
|
|
|
|
|
'request_id' => RequestContext::id(),
|
|
|
|
|
],
|
|
|
|
|
]);
|
|
|
|
|
}
|
2026-03-19 18:20:13 +01:00
|
|
|
}
|