solophp/container
最新稳定版本:v2.4.0
Composer 安装命令:
composer require solophp/container
包简介
Dependency Injection Container implementing WritableContainerInterface
关键字:
README 文档
README
Lightweight PSR-11 dependency injection container with auto-wiring, interface binding, and singleton caching.
Features
- PSR-11 Compatible — Implements
WritableContainerInterfacefromsolophp/contracts - Auto-wiring — Automatic dependency resolution via constructor reflection
- Interface Binding — Bind abstracts to concrete implementations
- Singleton Caching — Each service resolved once and cached
- Cache Invalidation —
set()invalidates cached instance,reset()clears all - Service Factories — Register services as callable factories
- Circular Dependency Detection — Cycles in bindings or auto-wiring fail fast with a readable chain
Installation
composer require solophp/container
Quick Example
use Solo\Container\Container; $container = new Container(); // Register a service factory $container->set(Database::class, fn($c) => new Database('localhost', 'mydb')); // Bind interface to implementation $container->bind(LoggerInterface::class, FileLogger::class); // Resolve with auto-wired dependencies $repo = $container->get(UserRepository::class);
Usage
Constructor Registration
$container = new Container([ 'config' => fn() => new Config('config.php'), 'cache' => fn($c) => new Cache($c->get('config')), ]);
Auto-wiring
The container automatically resolves class dependencies via constructor reflection:
class UserRepository { public function __construct( private Database $database, private LoggerInterface $logger ) {} } // Database and LoggerInterface resolved automatically $repo = $container->get(UserRepository::class);
Interface Binding
$container->bind(LoggerInterface::class, FileLogger::class); $container->bind(CacheInterface::class, RedisCache::class);
Re-registering Services
set() invalidates the cached instance, so the next get() uses the new factory:
$container->set(Connection::class, fn() => new Connection('db1')); $conn1 = $container->get(Connection::class); // Connection to db1 $container->set(Connection::class, fn() => new Connection('db2')); $conn2 = $container->get(Connection::class); // Connection to db2
Resetting All Instances
When a root dependency changes and the entire dependency tree needs rebuilding:
$container->reset(); // All cached instances cleared
Circular Dependencies
Cycles are detected and throw ContainerException with the full resolution chain:
class A { public function __construct(public B $b) {} } class B { public function __construct(public A $a) {} } $container->get(A::class); // ContainerException: Circular dependency detected: A -> B -> A
The same applies to recursive bindings (bind(A, B); bind(B, A)) and factories that call $c->get() on themselves.
Error Handling
Solo\Container\Exceptions\NotFoundException— service not foundSolo\Container\Exceptions\ContainerException— service cannot be resolved (non-instantiable, unresolvable parameter, or circular dependency)
Requirements
- PHP 8.1+
License
MIT License. See LICENSE for details.
统计信息
- 总下载量: 125
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 0
- 依赖项目数: 2
- 推荐数: 2
其他信息
- 授权协议: MIT
- 更新时间: 2024-07-13