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);
|
$redisCache = new GeneralCache(new RedisCache($redisConnection), $serializer, $compression);
|
||||||
} catch (\Throwable $e) {
|
} catch (\Throwable $e) {
|
||||||
// Fallback to file cache if Redis is not available
|
// 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);
|
$redisCache = new GeneralCache(new FileCache(), $serializer, $compression);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -22,15 +22,18 @@ final class CyclicDependencyException extends ContainerException
|
|||||||
|
|
||||||
if ($cycleStartIndex !== false) {
|
if ($cycleStartIndex !== false) {
|
||||||
// Extrahiere nur den Zyklus-Teil
|
// Extrahiere nur den Zyklus-Teil
|
||||||
$this->cycle = array_slice($dependencyChain, $cycleStartIndex);
|
$cycle = array_slice($dependencyChain, $cycleStartIndex);
|
||||||
$this->cycle[] = $class; // Schließe den Zyklus
|
$cycle[] = $class; // Schließe den Zyklus
|
||||||
$this->cycleStart = $dependencyChain[$cycleStartIndex];
|
$cycleStart = $dependencyChain[$cycleStartIndex];
|
||||||
} else {
|
} else {
|
||||||
// Fallback: Verwende die gesamte Kette
|
// Fallback: Verwende die gesamte Kette
|
||||||
$this->cycle = array_merge($dependencyChain, [$class]);
|
$cycle = array_merge($dependencyChain, [$class]);
|
||||||
$this->cycleStart = $dependencyChain[0] ?? $class;
|
$cycleStart = $dependencyChain[0] ?? $class;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$this->cycle = $cycle;
|
||||||
|
$this->cycleStart = $cycleStart;
|
||||||
|
|
||||||
$context = ExceptionContext::forOperation('dependency_resolution', 'DI')
|
$context = ExceptionContext::forOperation('dependency_resolution', 'DI')
|
||||||
->withData([
|
->withData([
|
||||||
'dependencyChain' => $dependencyChain,
|
'dependencyChain' => $dependencyChain,
|
||||||
|
|||||||
Reference in New Issue
Block a user