decodelabs / pandora
强大的PSR-11依赖注入容器
v0.2.15
2024-04-26 17:19 UTC
Requires
- php: ^8.1
- decodelabs/archetype: ^0.2|^0.3
- decodelabs/exceptional: ^0.4
- decodelabs/glitch-support: ^0.4
- decodelabs/slingshot: ^0.1.0
- psr/container: ^2
Requires (Dev)
Provides
- psr/container-implementation: 1.1|2.0
README
PHP的强大PSR-11依赖注入容器
Pandora提供了稳固的DI容器结构的所有常规好处,并添加了一些额外的魔法元素。
在DecodeLabs博客上获取新闻和更新。
安装
composer require decodelabs/pandora
使用
实例化一个新的Container
以组织你的重要对象
use DecodeLabs\Pandora\Container; $container = new Container();
将实例或类绑定到接口,并在需要时检索它们
use My\Library\CoolInterface; use My\Library\CoolImplementation; // Implements CoolInterface // Instance $container->bind(CoolInterface::class, new CoolImplementation()); $imp = $container->get(CoolInterface::class); // Reused class $container->bind(CoolInterface::class, CoolImplementation::class); // Creates a new instace every call $imp = $container->get(CoolInterface::class); // Shared class $container->bindShared(CoolInterface::class, CoolImplementation::class); // Stores created instance and returns that each call $imp = $container->get(CoolInterface::class); // Locked instance - will only bind if CoolInterface has not been bound before $container->bindLocked(CoolInterface::class, new CoolImplementation()); $imp = $container->get(CoolInterface::class); // Bind a factory $container->bind(CoolInterface::class, function($container) { return new CoolImplementation(); })
组允许将多个实例绑定到一个接口
// Group multiple bindings $container->bindToGroup(CoolInterface::class, new CoolImplementation(1)); $container->bindToGroup(CoolInterface::class, new CoolImplementation(2)); $container->bindToGroup(CoolInterface::class, new CoolImplementation(3)); $group = $container->getGroup(CoolInterface::class); // Contains 3 Implementations $container->each(CoolInterface::class, function($instance, $container) { // Do something with each instance });
别名在检索对象时很有用,无需重复接口
// Aliased instance $container->bind(CoolInterface::class, new CoolImplementation()) ->alias('cool.thing'); $imp = $container->get('cool.thing'); // Or $container->registerAlias(CoolInterface::class, 'cool.thing'); $container->bind(CoolInterface::class, CoolImplementation::class); $imp = $container->get('cool.thing');
检索
可以将参数传递给实现类的构造函数
$imp = $container->getWith(CoolInterface::class, ['list', 'of', 'params']); // Or inject parameters for later: $container->inject(CoolInterface::class, 'paramName', 'paramValue'); $imp = $container->get(CoolInterface::class);
容器还具有ArrayAccess别名,用于get / bind / has / remove
$imp = $container[CoolInterface::class]; if(isset($container[CoolInterface::class])) { unset($container[CoolInterface::class]); }
使用成员名称访问绑定控制器
$binding = $container->{CoolInterface::class}; // Or $binding = $container->getBinding(CoolInterface::class);
事件
对容器上的事件做出反应
$container->afterResolving(CoolInterface::class, function($instance, $container) { // Prepare instance }); $container->afterRebinding(CoolInterface::class, function($instance, $container) { // Prepare instance again }); // Triggers resolve callback once $imp = $container->get(CoolInterface::class); // Triggers rebinding callback $container->bind(CoolInterface::class, new AnotherImplementation());
服务提供者
实现Provider
接口,并注册它以进行懒加载的大规模绑定,几乎没有任何开销
use DecodeLabs\Pandora\Provider; use DecodeLabs\Pandora\Container; class CoolService implements Provider { public static function getProvidedServices(): array { return [ CoolInterface::class, OtherInterface::class ]; } public function registerServices(Container $container): void { $container->bindShared(CoolInterface::class, CoolImplementation::class) ->alias('cool.thing'); // Create factory closure $container->bind(OtherInterface::class, function($container) { return new class implements OtherInterface { public function hello(): string { return 'world'; } }; }); } } $conainer->registerProvider(CoolService::class);
许可
Pandora遵循MIT许可证。有关完整许可证文本,请参阅LICENSE。