chilldev / dependency-injection-extra
提供了一些常用的Symfony2 DI模式的实现。
这个包的官方仓库似乎已经消失,因此该包已被冻结。
0.0.1
2013-09-12 18:44 UTC
Requires
- php: >=5.4
- symfony/dependency-injection: ~2.1
Requires (Dev)
- ext-xsl: *
- phpmd/phpmd: 1.5.0
- phpunit/phpunit: 3.7.25
- satooshi/php-coveralls: 0.6.1
- sebastian/phpcpd: 1.4.3
- squizlabs/php_codesniffer: 1.4.6
This package is not auto-updated.
Last update: 2019-03-08 14:40:41 UTC
README
ChillDevDependencyInjectionExtra 是一个库,它实现了在 Symfony2 DI 中常用的一些模式。
安装
这个库以 Composer 包 的形式提供。要安装它,只需将以下依赖定义添加到您的 composer.json
文件中
"chilldev/dependency-injection-extra": "dev-master"
如果您想使用特定版本,可以将 dev-master
替换为不同的约束条件。
注意: 这个库需要 PHP 5.4。
注意: 这不是一个捆绑包;您不需要修改您的内核或项目的任何其他部分来使用它 - 直接在代码中使用即可。
使用方法
您可以使用 ChillDev\DependencyInjection\Compiler\TagGrabbingPass
来自动将所有带有标签的服务抓取到容器中。考虑以下DI服务
services: repository: class: "YourAdaptersRepository" first: class: "YourFirstAdapter" tags: - { name: "your.adapter.tag", adapter: "first" } second: class: "YourSecondAdapter" tags: - { name: "your.adapter.tag", adapter: "second" } third: class: "YourThirdAdapter" tags: - { name: "your.adapter.tag", adapter: "third" }
假设您的仓库类如下所示
class YourAdaptersRepository extends ArrayObject { public function registerAdapter($key, $adapter) { $this[$key] = $adapter; } }
这是一个非常常见的设置 - 现在您想将所有带有 your.adapter.tag
标签的服务添加到 repository
服务中。您不需要编写自己的自定义DI编译器传递,可以使用现成的实现来做到这一点。就像这样简单
use ChillDev\DependencyInjection\Compiler\TagGrabbingPass; use Symfony\Component\DependencyInjection\Compiler\PassConfig; use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\HttpKernel\Bundle\Bundle; class YourBundle extends Bundle { public function build(ContainerBuilder $container) { parent::build($container); $container->addCompilerPass( new TagGrabbingPass( // this is tag name for which you want to look 'your.adapter.tag', // this is ID of container service 'repository', // this is name of method for registering new adapters 'registerAdapter', // this is tag attribute name to be used as key identifier 'adapter' ), PassConfig::TYPE_OPTIMIZE ); } }
资源
- 源文档
- 带有API文档的GitHub页面
- 问题跟踪器
- Packagist 包
- Chillout Development @ GitHub
- Chillout Development @ Facebook
贡献
您想帮助改进这个项目吗?只需 分支 它并提交一个pull request。您可以自己完成所有事情,不需要询问是否可以,只需做您想做的所有酷炫的事情!
此项目在MIT许可证下发布。
作者
ChillDevDependencyInjectionExtra 由 Chillout Development 提供。
贡献者列表