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);
|
||||
}
|
||||
|
||||
|
||||
@@ -19,18 +19,21 @@ final class CyclicDependencyException extends ContainerException
|
||||
) {
|
||||
// Finde wo der Zyklus beginnt (erste Wiederholung in der Kette)
|
||||
$cycleStartIndex = array_search($class, $dependencyChain, true);
|
||||
|
||||
|
||||
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,
|
||||
@@ -53,19 +56,19 @@ final class CyclicDependencyException extends ContainerException
|
||||
private function buildMessage(): string
|
||||
{
|
||||
$cycleStr = implode(' → ', $this->cycle);
|
||||
|
||||
|
||||
$message = "🔄 Zyklische Abhängigkeit entdeckt:\n\n";
|
||||
$message .= " {$cycleStr}\n";
|
||||
$message .= " ↑─────────────────────┘\n";
|
||||
$message .= " Der Zyklus beginnt hier bei '{$this->cycleStart}'\n\n";
|
||||
|
||||
|
||||
// Füge hilfreiche Hinweise hinzu
|
||||
$message .= "💡 Lösungsvorschläge:\n";
|
||||
$message .= " • Verwende Lazy Loading für eine der Abhängigkeiten\n";
|
||||
$message .= " • Füge eine Factory zwischen die Klassen ein\n";
|
||||
$message .= " • Refactoriere die Abhängigkeitsstruktur (Dependency Inversion)\n";
|
||||
$message .= " • Verwende Event-basierte Kommunikation statt direkter Abhängigkeit\n";
|
||||
|
||||
|
||||
return $message;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user