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:
2025-11-03 18:17:31 +01:00
parent 0fa163276e
commit 2b4772a922
2 changed files with 16 additions and 10 deletions

View File

@@ -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);
} }

View File

@@ -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,