fresh-advance / regex-dependency
一个小巧、可配置的服务定位器/DI 容器,可以以正则表达式描述键。该组件也可以用作路由器。
v0.3.0
2019-11-10 16:25 UTC
Requires
- php: >=7.2
- psr/container: ^1.0
Requires (Dev)
This package is auto-updated.
Last update: 2024-09-16 03:27:11 UTC
README
一个小巧、可配置的服务定位器/DI 容器,可以以正则表达式描述键。该组件也可以用作路由器。
该包
- 符合 PSR-11。
- 遵循 PSR-4 和 PSR-12。
- 在 PHP 7.2+ 上运行。
简单案例
$configuration = new Collection(
new Item('key', 'value'),
);
$container = new Container($configuration);
$value = $container->get('key');
var_dump:
string(5) "value"
模式案例
$configuration = new Collection(
new Pattern('examplePattern', '/Example\/.*?$/i', 'SomeValue'),
);
$container = new Container($configuration);
$value = $container->get('Example/Something');
var_dump:
string(9) "SomeValue"
值作为回调
$configuration = new Collection(
new Pattern('examplePattern', '/Example\/(?P<special>.*?)$/i', function ($dependency, $match) {
return $match;
}),
);
$container = new Container($configuration);
$keyValue = $container->get('Example/Something');
var_dump:
array(3) {
[0] => string(17) "Example/Something"
'special' => string(9) "Something"
[1] => string(9) "Something"
}
向回调发送两个参数
- 容器 $dependency - 容器的当前实例
- 数组 $match - 包含匹配结果的数组:提供的匹配响应是:['Controller/SomeName', 'SomeName']
服务注册表
默认情况下,回调返回的对象不会被缓存,但它们可以被缓存,如果将它们包装为 服务。
$configuration = new Collection(
new Item('someKey', function (Container $dependency) {
return new Service(new \stdClass());
})
);