refactor: add circular dependency detection and error handling in DI container

- Introduce `InitializerCycleException` for detailed cycle reporting
- Enhance `InitializerProcessor` fallback with explicit discovery order handling and logging
- Implement proactive cycle detection in `InitializerDependencyGraph`
- Improve `ClassName` and `MethodName` with `Stringable` support
This commit is contained in:
2025-11-03 15:37:40 +01:00
parent 376fcd5fc1
commit 0ca382f80b
6 changed files with 173 additions and 4 deletions

View File

@@ -9,6 +9,7 @@ use App\Framework\Context\ExecutionContext;
use App\Framework\Core\ValueObjects\ClassName;
use App\Framework\Core\ValueObjects\MethodName;
use App\Framework\DI\Container;
use App\Framework\DI\Exceptions\InitializerCycleException;
use App\Framework\DI\Initializer;
use App\Framework\DI\InitializerDependencyGraph;
use App\Framework\DI\ValueObjects\DependencyGraphNode;
@@ -121,8 +122,28 @@ final readonly class InitializerProcessor
);
}
}
} catch (InitializerCycleException $e) {
// Spezielle Behandlung für Cycles: Expliziter Fallback mit detailliertem Logging
$logger = $this->getLogger();
$logger->error(
"Circular dependencies detected in initializers, registering in discovery order",
LogContext::withExceptionAndData($e, [
'cycles' => $e->getCycles(),
'cycle_count' => $e->getCycleCount(),
])
);
// Fallback: Registriere alle Services in Discovery-Reihenfolge
/** @var string $returnType */
foreach ($graph->getNodes() as $returnType => $node) {
$this->registerLazyService(
$returnType,
$node->getClassName(),
$node->getMethodName()
);
}
} catch (\Throwable $e) {
// Fallback: Registriere alle Services ohne spezielle Reihenfolge
// Andere Fehler: Fallback mit generischer Warnung
$logger = $this->getLogger();
$logger->warning(
"Failed to register services with dependency graph, falling back to unordered registration",