bayfrontmedia / container
一个易于使用的PSR-11兼容的依赖注入容器。
v3.0.0
2023-01-26 16:44 UTC
Requires
- php: ^8.0
- psr/container: ^2.0
README
一个易于使用的PSR-11兼容的依赖注入容器。
许可证
本项目为开源项目,并受MIT许可证的许可。
作者
需求
- PHP
^8.0
安装
composer require bayfrontmedia/container
使用
开始使用容器
use Bayfront\Container;
$container = new Container();
公共方法
set
描述
将条目设置到容器中。
匿名函数(闭包)在第一次 get()
调用时被调用。
参数
$id
(字符串)$value
(混合类型)$overwrite = false
(布尔值): 如果为false,当存在具有相同ID的条目时,将抛出ContainerException
异常。否则,将覆盖。
返回
- (void)
抛出
Bayfront\Container\ContainerException
示例
可以在容器中设置任何类型的值。
可以使用匿名函数(闭包)设置具有依赖关系的服务,该函数返回类的实例。
第一次使用 get()
从容器请求服务时,将调用匿名函数,并将结果保存。后续调用将始终返回相同的ID。
匿名函数始终将容器实例作为第一个参数调用。这允许您在需要时引用容器中的其他项目。如果不需要访问容器,则可以省略函数签名中的参数。
// Set a service with no dependencies $container->set('Fully\Namespaced\ClassName', function () { return new ClassName(); }); // Set a service with dependencies $container->set('Fully\Namespaced\ClassName', function (ContainerInterface $container) { $dependency = $container->get('Fully\Namespaced\Dependency'); return new ClassName($dependency); }); // Any type of value can be set, then used as a parameter $container->set('classname_config', [ // Config array ]); $container->set('Fully\Namespaced\ClassName', function (ContainerInterface $container) { $config = $container->get('classname_config'); return new ClassName($config); }); // Preexisting class instances can be set without using an anonymous function $class = new ClassName(); $container->set('ClassName', $class);
getEntries
描述
返回容器中所有现有ID的数组。
参数
- 无
返回
- (数组)
示例
$entries = $container->getEntries();
get
描述
通过ID或别名从容器中获取条目。
参数
$id
(字符串)
返回
- (混合类型)
抛出
Bayfront\Container\NotFoundException
示例
$service = $container->get('Fully\Namespaced\ClassName');
make
描述
创建并返回一个新的类实例,自动注入容器中存在的依赖。
参数
$class
(字符串)$params = []
(数组): 传递给类构造函数的额外参数。
返回
- (混合类型)
抛出
Bayfront\Container\ContainerException
Bayfront\Container\NotFoundException
示例
class ClassName { protected $service; protected $config; public function __construct(AnotherService $service, array $config) { $this->service = $service; $this->config = $config; } } $instance = $container->make('Fully\Namespaced\ClassName', [ 'config' => [] ]);
has
描述
条目或别名是否存在于容器中?(即:是否可以使用此ID使用 get()
解析条目?)
参数
$id
(字符串): ID或别名
返回
- (布尔值)
示例
if ($container->has('Fully\Namespaced\ClassName')) { // Do something }
remove
描述
如果存在,从容器中删除条目。
参数
$id
(字符串)
返回
- (void)
示例
$container->remove('Fully\Namespaced\ClassName');
setAlias
描述
为给定的ID设置别名。
参数
$alias
(字符串)$id
(字符串)$overwrite = false
(布尔值): 如果为false,当存在具有相同名称的别名时,将抛出ContainerException
异常。否则,将覆盖。
返回
- (void)
抛出
Bayfront\Container\ContainerException
示例
$container->setAlias('alias', 'Fully\Namespaced\ClassName');
别名的一个好处是,它们允许您以简洁、易于记住的方式从容器中检索条目。此外,别名允许您将接口绑定到实现。
例如
$container->setAlias('Fully\Namespaced\Implementation', 'Fully\Namespaced\Interface');
现在,每当类需要一个 Fully\Namespaced\Interface
的实现时,如果容器中存在,将返回 Fully\Namespaced\Implementation
的实例。
getAliases
描述
返回所有现有别名的数组。
参数
- 无
返回
- (数组)
示例
$aliases = $container->getAliases();
hasAlias
描述
别名是否存在?
参数
$alias
(字符串)
返回
- (布尔值)
示例
if ($container->hasAlias('alias')) { // Do something }
removeAlias
描述
删除别名。
参数
$alias
(字符串)
返回
- (void)
示例
$container->removeAlias('alias');