nette/di
Nette DI is a fast, configurable dependency injection container for PHP. Compile-time container generation boosts performance, while extensions, autowiring, and service definitions make complex apps easy to wire. Integrates smoothly with the Nette framework or standalone.
Laravel’s Native DI vs. Nette/DI:
Laravel’s built-in Service Container (PSR-11 compliant) is tightly integrated with its ecosystem (e.g., IoC bindings, service providers, facades). nette/di is a standalone, compiled DI container optimized for performance and explicit wiring, not designed for Laravel’s conventions (e.g., service providers, facades, or Laravel’s app() helper).
resolve()), while nette/di compiles dependencies into static PHP code at build time. This creates a fundamental architectural mismatch—Laravel’s dynamic binding system cannot natively consume a pre-compiled container.league/container or custom wrappers).Key Laravel Features Unsupported:
nette/di lacks Laravel’s register()/boot() lifecycle hooks.when()/needs() logic is incompatible with nette/di’s compiled autowiring.nette/di does not integrate with Laravel’s facade system.tag()/context() features are absent.PSR-11 Compatibility:
nette/di implements PSR-11 (ContainerInterface) but with non-standard extensions (e.g., compiled containers, Neon/YAML config). Laravel’s container is PSR-11-compliant but expects runtime flexibility, while nette/di is optimized for compile-time rigidity.
league/container) to bridge nette/di with Laravel, but this introduces performance overhead (runtime resolution vs. compiled code).Configuration Overlap:
AppServiceProvider) for bindings.nette/di uses Neon/YAML or PHP-based definitions (e.g., services.neon).ServiceProvider that loads nette/di definitions into Laravel’s container).Autowiring Differences:
resolve() time).nette/di compiles autowiring into static code (faster but less flexible).nette/di) may lead to unpredictable resolution (e.g., circular dependencies handled differently).| Risk Area | Severity | Mitigation Strategy |
|---|---|---|
| Architectural Mismatch | Critical | Avoid using nette/di as Laravel’s primary container; restrict to non-Laravel components. |
| Performance Overhead | High | If used via PSR-11 adapter, expect ~20-30% slower resolution than native Laravel container. |
| Configuration Merge Hell | High | Requires custom ServiceProvider to sync nette/di definitions with Laravel’s container. |
| Debugging Complexity | Medium | nette/di’s Tracy panel won’t integrate with Laravel’s debugbar; use custom logging. |
| PHP 8+ Feature Gaps | Low | Laravel already supports PHP 8.5 attributes/enums; nette/di’s features (e.g., lazy services) are redundant. |
| Long-Term Maintenance | Medium | nette/di is Nette-specific; Laravel’s ecosystem (e.g., Horizon, Nova) assumes Laravel’s container. |
Why not use Laravel’s native container?
nette/di (e.g., compiled performance, Neon config) that justifies the integration risk?Illuminate\Container\Container, Illuminate\Foundation\Application)?Scope of Integration:
nette/di replace all Laravel services, or only specific components (e.g., domain layer)?nette/di-managed service calling a Laravel-managed service) be handled?Performance Trade-offs:
nette/di worth the runtime flexibility loss in Laravel’s container?Team Expertise:
nette/di’s type safety?Future-Proofing:
nette/di?nette/di if Laravel’s container evolves to meet the same needs?Laravel’s Stack:
Illuminate\Container\Container (PSR-11 compliant).AppServiceProvider), config/ files.nette/di Stack:
Compatibility Matrix:
| Feature | Laravel Container | nette/di |
Integration Feasibility |
|---|---|---|---|
| PSR-11 Compliance | ✅ Yes | ✅ Yes | High (via adapter) |
| Service Providers | ✅ Yes | ❌ No | Low (custom glue) |
| Autowiring | ✅ Runtime | ✅ Compile | Medium (conflict) |
| Facades | ✅ Yes | ❌ No | Low |
| Neon/YAML Config | ❌ No | ✅ Yes | High (but redundant) |
| Compiled Performance | ❌ No | ✅ Yes | High (but isolated) |
| Tracy Debugging | ❌ No | ✅ Yes | Low (incompatible) |
| PHP 8.5+ Features | ✅ Yes | ✅ Yes | High |
Scope: Use nette/di only for non-Laravel components (e.g., domain layer, background jobs).
Implementation:
nette/di’s Container in a class implementing Psr\Container\ContainerInterface.class NetteDiAdapter implements ContainerInterface {
private Container $netteContainer;
public function __construct(Container $container) {
$this->netteContainer = $container;
}
public function get(string $id) {
return $this->netteContainer->getService($id);
}
public function has(string $id): bool {
return $this->netteContainer->hasService($id);
}
}
$netteContainer = new Container();
$netteContainer->loadFromFile(__DIR__.'/services.neon');
$adapter = new NetteDiAdapter($netteContainer);
app()->bind('nette', fn() => $adapter);
#[Inject] and configure in services.neon.Pros:
nette/di’s strengths (How can I help you explore Laravel packages today?