refactor: improve lazy loading and logging in DI container

- Enhance lazy loading fallback with detailed logging of failures
- Simplify singleton resolution logic, removing redundancy
- Add `canAutoWire` method for cleaner class auto-wiring checks
- Optimize logging resolution in `InitializerProcessor`
- Remove unused code and redundant debug logs across DI components
This commit is contained in:
2025-11-03 15:12:54 +01:00
parent b6bff8b19b
commit 376fcd5fc1
3 changed files with 71 additions and 65 deletions

View File

@@ -39,11 +39,10 @@ final readonly class InitializerProcessor
*/
public function processInitializers(DiscoveryRegistry $results): void
{
// Safe Logger resolution - use if available, otherwise rely on error_log
$logger = $this->container->get(Logger::class);
$logger = $this->getLogger();
$initializerResults = $results->attributes->get(Initializer::class);
$logger?->debug("InitializerProcessor: Processing " . count($initializerResults) . " initializers");
$logger->debug("InitializerProcessor: Processing " . count($initializerResults) . " initializers");
$dependencyGraph = new InitializerDependencyGraph($this->reflectionProvider);
@@ -63,7 +62,11 @@ final readonly class InitializerProcessor
// Context-Filter: Prüfe ob Initializer für aktuellen Context erlaubt ist
// The contexts data is directly in the additionalData
// Wenn contexts null ist, ist der Initializer für alle Contexts verfügbar
if (isset($initializerData['contexts']) && $initializerData['contexts'] !== null && ! $this->isContextAllowed(...$initializerData['contexts'])) {
if (
isset($initializerData['contexts'])
&& $initializerData['contexts'] !== null
&& ! $this->isContextAllowed(...$initializerData['contexts'])
) {
continue;
}
@@ -81,7 +84,7 @@ final readonly class InitializerProcessor
$dependencyGraph->addInitializer($returnType, $discoveredAttribute->className, $methodName);
}
} catch (\Throwable $e) {
$logger?->error(
$logger->error(
"Failed to register initializer: {$discoveredAttribute->className->toString()}::{$methodName->toString()}",
LogContext::withExceptionAndData($e, [
'class' => $discoveredAttribute->className->toString(),
@@ -107,13 +110,10 @@ final readonly class InitializerProcessor
$executionOrder = $graph->getExecutionOrder();
foreach ($executionOrder as $returnType) {
if ($graph->hasNode($returnType)) {
/** @var DependencyGraphNode $node */
$node = $graph->getNode($returnType);
$this->registerLazyService(
$returnType,
$node->getClassName(),
@@ -123,6 +123,12 @@ final readonly class InitializerProcessor
}
} catch (\Throwable $e) {
// Fallback: Registriere alle Services ohne spezielle Reihenfolge
$logger = $this->getLogger();
$logger->warning(
"Failed to register services with dependency graph, falling back to unordered registration",
LogContext::withException($e)
);
/** @var string $returnType */
foreach ($graph->getNodes() as $returnType => $node) {
$this->registerLazyService(
@@ -132,8 +138,6 @@ final readonly class InitializerProcessor
);
}
}
unset($graph);
}
/**
@@ -164,9 +168,7 @@ final readonly class InitializerProcessor
*/
private function registerLazyService(string $returnType, string $className, string $methodName): void
{
$factory = function ($container) use ($className, $methodName, $returnType) {
try {
$instance = $container->invoker->invoke(ClassName::create($className), $methodName);
} catch (\Throwable $e) {
@@ -174,7 +176,6 @@ final readonly class InitializerProcessor
throw $e;
}
// Wenn das ein Interface ist, registriere auch die konkrete Klasse automatisch
if (interface_exists($returnType)) {
$concreteClass = get_class($instance);
@@ -189,12 +190,9 @@ final readonly class InitializerProcessor
try {
// Registriere den Return-Type (Interface oder konkrete Klasse)
$this->container->singleton($returnType, $factory);
} catch (\Throwable $e) {
// Safe Logger resolution - use if available
$logger = $this->container->has(Logger::class) ? $this->container->get(Logger::class) : null;
$logger?->error(
$logger = $this->getLogger();
$logger->error(
"Failed to register lazy service for return type: {$returnType}",
LogContext::withExceptionAndData($e, [
'return_type' => $returnType,
@@ -206,4 +204,12 @@ final readonly class InitializerProcessor
// Service registration failed - continue to prevent breaking the entire application
}
}
/**
* Gibt den Logger zurück (ist im Framework immer verfügbar)
*/
private function getLogger(): Logger
{
return $this->container->get(Logger::class);
}
}