- Add DISCOVERY_LOG_LEVEL=debug - Add DISCOVERY_SHOW_PROGRESS=true - Temporary changes for debugging InitializerProcessor fixes on production
90 lines
1.8 KiB
Markdown
90 lines
1.8 KiB
Markdown
# Dependency Injection Modul
|
|
|
|
## Übersicht
|
|
|
|
Das DI-Modul implementiert ein Dependency-Injection-Container-System, das die automatische Auflösung und Verwaltung von Abhängigkeiten ermöglicht.
|
|
|
|
## Hauptkomponenten
|
|
|
|
### Container
|
|
|
|
Der zentrale Service-Container, der Instanzen erstellt und verwaltet.
|
|
|
|
**Hauptfunktionen:**
|
|
- Service-Erstellung und -Auflösung
|
|
- Singleton-Verwaltung
|
|
- Rekursive Abhängigkeitsauflösung
|
|
|
|
### Initializer-Attribut
|
|
|
|
Das `#[Initializer]`-Attribut kennzeichnet Klassen, die Services im Container registrieren können.
|
|
|
|
**Beispiel:**
|
|
```php
|
|
#[Initializer]
|
|
readonly class AnalyticsInitializer
|
|
{
|
|
public function __invoke(Container $container): Analytics
|
|
{
|
|
// Service erstellen und konfigurieren
|
|
return $analytics;
|
|
}
|
|
}
|
|
```
|
|
|
|
## Verwendung
|
|
|
|
### Service definieren
|
|
|
|
```php
|
|
// Service-Interface
|
|
interface MyServiceInterface
|
|
{
|
|
public function doSomething(): void;
|
|
}
|
|
|
|
// Konkrete Implementierung
|
|
class MyService implements MyServiceInterface
|
|
{
|
|
public function doSomething(): void
|
|
{
|
|
// Implementierung
|
|
}
|
|
}
|
|
```
|
|
|
|
### Service registrieren
|
|
|
|
```php
|
|
#[Initializer]
|
|
class MyServiceInitializer
|
|
{
|
|
public function __invoke(Container $container): MyServiceInterface
|
|
{
|
|
return new MyService();
|
|
}
|
|
}
|
|
```
|
|
|
|
### Service verwenden
|
|
|
|
```php
|
|
class MyController
|
|
{
|
|
public function __construct(
|
|
private readonly MyServiceInterface $myService
|
|
) {}
|
|
|
|
public function action(): void
|
|
{
|
|
$this->myService->doSomething();
|
|
}
|
|
}
|
|
```
|
|
|
|
## Prinzipien
|
|
|
|
- **Automatische Auflösung**: Abhängigkeiten werden anhand von Typ-Hints automatisch aufgelöst
|
|
- **Lazy Loading**: Services werden erst erstellt, wenn sie benötigt werden
|
|
- **Singleton-Modus**: Standardmäßig werden Services als Singletons verwaltet
|