refactor(di, cache): improve cyclic dependency handling and enhance error logging
- Refactor `CyclicDependencyException` to streamline cycle extraction logic and variable assignments. - Improve error message formatting with clearer structure and actionable tips. - Add detailed stack trace logging in `CacheInitializer` when Redis fails, for better debugging.
This commit is contained in:
@@ -66,7 +66,10 @@ final readonly class CacheInitializer
|
||||
$redisCache = new GeneralCache(new RedisCache($redisConnection), $serializer, $compression);
|
||||
} catch (\Throwable $e) {
|
||||
// Fallback to file cache if Redis is not available
|
||||
error_log("Redis not available, falling back to file cache: " . $e->getMessage());
|
||||
|
||||
error_log('Redis not available, falling back to file cache: ' . $e->getMessage());
|
||||
error_log(print_r($e->getTrace(), true));
|
||||
|
||||
$redisCache = new GeneralCache(new FileCache(), $serializer, $compression);
|
||||
}
|
||||
|
||||
|
||||
@@ -22,15 +22,18 @@ final class CyclicDependencyException extends ContainerException
|
||||
|
||||
if ($cycleStartIndex !== false) {
|
||||
// Extrahiere nur den Zyklus-Teil
|
||||
$this->cycle = array_slice($dependencyChain, $cycleStartIndex);
|
||||
$this->cycle[] = $class; // Schließe den Zyklus
|
||||
$this->cycleStart = $dependencyChain[$cycleStartIndex];
|
||||
$cycle = array_slice($dependencyChain, $cycleStartIndex);
|
||||
$cycle[] = $class; // Schließe den Zyklus
|
||||
$cycleStart = $dependencyChain[$cycleStartIndex];
|
||||
} else {
|
||||
// Fallback: Verwende die gesamte Kette
|
||||
$this->cycle = array_merge($dependencyChain, [$class]);
|
||||
$this->cycleStart = $dependencyChain[0] ?? $class;
|
||||
$cycle = array_merge($dependencyChain, [$class]);
|
||||
$cycleStart = $dependencyChain[0] ?? $class;
|
||||
}
|
||||
|
||||
$this->cycle = $cycle;
|
||||
$this->cycleStart = $cycleStart;
|
||||
|
||||
$context = ExceptionContext::forOperation('dependency_resolution', 'DI')
|
||||
->withData([
|
||||
'dependencyChain' => $dependencyChain,
|
||||
|
||||
Reference in New Issue
Block a user