Switon's dependency injection container for autowiring, named services, and bootstrap wiring.
- Shared container:
ContainerInterfacehandles service resolution and definition registration. - Service autowiring:
#[Autowired]can inject object services without manual lookups. - Config autowiring: the same attribute can fill scalar and array properties from config.
- Named instances:
FactoryInterfacesupports multiple implementations of the same type. - Lazy dependencies:
Lazydefers non-default resolution until a value is actually used. - Autowired instances:
#[Autowired(instances: true)]can inject service arrays from config.
composer require switon/di# switon.yml
Switon\Core\AppInterface:
id: admin
name: Admin Panel
version: 2.0.0
env: dev
debug: true
timezone: Asia/Shanghaiuse Switon\Core\AppInterface;
use Switon\Core\Attribute\Autowired;
class DashboardService
{
#[Autowired] protected AppInterface $app;
public function title(): string
{
return $this->app->name() . ' (' . $this->app->env() . ')';
}
}Docs: https://docs.switon.dev/latest/di
MIT.