internations / decorator-bundle
此包已被废弃,不再维护。没有建议的替代包。
用于处理服务容器中装饰器的 Symfony 扩展包
v1.2.0
2024-07-30 11:46 UTC
Requires
- php: ~7.1
- symfony/config: ~3 | ~4
- symfony/dependency-injection: ~3 | ~4
- symfony/expression-language: ~3 | ~4
- symfony/http-kernel: ~3 | ~4
- symfony/security: ~3 | ~4
Requires (Dev)
- internations/testing-component: dev-master
- phpunit/phpunit: ~6
README
为 Symfony 依赖注入容器提供一致的装饰器处理。
安装
适用于 Symfony 3.3 至 4
composer require internations/decorator-bundle
适用于 Symfony < 3.3
composer require internations/decorator-bundle:~0
用法
"装饰此"-模式
在 "装饰此"-模式下,你在相关的依赖中声明装饰器。配置示例
<service id="iterator" class="ArrayIterator"> <argument type="collection"> <argument type="string">element1</argument> <argument type="string">element2</argument> </argument> <tag name="decorator.decorate_this" decorator_id="infinite_iterator"/> </service> <service id="infinite_iterator" class="InifiteIterator" public="false"> <argument type="service" id="__subject__"/> </service>
上面的配置将创建此实例
$iterator = new InfiniteIterator( new ArrayIterator(['element1', 'element2']) );
"装饰其他"模式
在 "装饰其他"-模式下,你在装饰器定义本身中声明要装饰的主题。配置示例
<service id="iterator" class="ArrayIterator"> <argument type="collection"> <argument type="string">element1</argument> <argument type="string">element2</argument> </argument> </service> <service id="infinite_iterator" class="InifiteIterator" public="false"> <argument type="service" id="__subject__"/> <tag name="decorator.decorate_other" service_id="iterator"/> </service>
上面的配置将再次创建此实例
$iterator = new InfiniteIterator( new ArrayIterator(['element1', 'element2']) );
指定优先级
为了控制装饰的顺序,支持为装饰器设置优先级标志。优先级可以在 PHP_INT_MAX
和 -PHP_INT_MAX
之间,默认优先级为 0
。
<service id="infinite_iterator" class="InifiteIterator" public="false"> <argument type="service" id="__subject__"/> <tag name="decorator.decorate_other" service_id="iterator" priority="255"/> </service>
以编程方式使用 Bundle
在需要在不使用 XML 配置的情况下重复使用装饰功能的情况下,你可以在你的 Compiler Pass 中使用以下 API。以下是一个示例,添加装饰服务 common_service
,使用装饰器 special_decorator
和优先级 255
。
namespace …; use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; use InterNations\Bundle\DecoratorBundle\Tagger\DecorationTagger; use Symfony\Component\DependencyInjection\ContainerBuilder; class MyCompilerPass implements CompilerPassInterface { public function process(ContainerBuilder $container) { DecorationTagger::tag($container, 'common_service', 'special_decorator', 255); } }
致谢
基于 Dovydas Bartkevicius 的想法,并得到了 Max Beutel 的许多反馈。