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

@@ -12,6 +12,8 @@ use App\Framework\DI\Exceptions\ContainerException;
use App\Framework\DI\Exceptions\CyclicDependencyException;
use App\Framework\DI\Exceptions\LazyLoadingException;
use App\Framework\Discovery\Results\DiscoveryRegistry;
use App\Framework\Logging\Logger;
use App\Framework\Logging\ValueObjects\LogContext;
use App\Framework\Metrics\FrameworkMetricsCollector;
use App\Framework\Reflection\CachedReflectionProvider;
use App\Framework\Reflection\ReflectionProvider;
@@ -93,11 +95,9 @@ final class DefaultContainer implements Container
$className = (string) $class;
// Bereits instanziierte Objekte zurückgeben
if ($this->instances->hasSingleton($className)) {
$singleton = $this->instances->getSingleton($className);
if ($singleton !== null) {
return $singleton;
}
$singleton = $this->instances->getSingleton($className);
if ($singleton !== null) {
return $singleton;
}
if ($this->instances->hasInstance($className)) {
@@ -108,10 +108,23 @@ final class DefaultContainer implements Container
if ($this->lazyInstantiator->canUseLazyLoading($className, $this->instances)) {
try {
return $this->lazyInstantiator->createLazyInstance($className);
} catch (Throwable $e) {
if (! $e instanceof LazyLoadingException) {
} catch (LazyLoadingException $e) {
// Log LazyLoading-Fehler und versuche Fallback (Logger ist immer verfügbar)
$this->get(Logger::class)->warning(
"Lazy loading failed for {$className}, falling back to normal instantiation",
LogContext::withException($e)
);
// Fallback: Versuche normale Instanziierung
try {
return $this->createInstance($className);
} catch (\Throwable $fallbackException) {
// Wenn Fallback auch fehlschlägt, ursprüngliche LazyLoadingException werfen
throw $e;
}
} catch (\Throwable $e) {
// Andere Exceptions direkt weiterwerfen
throw $e;
}
}
@@ -178,7 +191,7 @@ final class DefaultContainer implements Container
// Check if class is instantiable using the framework's method
if (! $reflection->isInstantiable()) {
$this->throwDetailedBindingException($class/*, $reflection*/);
$this->throwDetailedBindingException($class);
}
$dependencies = $this->dependencyResolver->resolveDependencies($className);
@@ -198,7 +211,7 @@ final class DefaultContainer implements Container
}
}
private function throwDetailedBindingException(string $class/*, $reflection*/): never
private function throwDetailedBindingException(string $class): never
{
$availableBindings = array_keys($this->bindings->getAllBindings());
@@ -265,7 +278,29 @@ final class DefaultContainer implements Container
return $this->instances->hasSingleton($class)
|| $this->instances->hasInstance($class)
|| $this->bindings->hasBinding($class)
|| (! empty($class) && ClassName::create($class)->exists() && $this->reflectionProvider->getClass(ClassName::create($class))->isInstantiable());
|| $this->canAutoWire($class);
}
/**
* Prüft ob eine Klasse automatisch instanziiert werden kann (auto-wiring)
* @param string $class Klassenname
*/
private function canAutoWire(string $class): bool
{
if (empty($class)) {
return false;
}
$className = ClassName::create($class);
if (! $className->exists()) {
return false;
}
try {
return $this->reflectionProvider->getClass($className)->isInstantiable();
} catch (\Throwable $e) {
return false;
}
}
/** @param class-string $class */