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