因此,您注入的参数数组没有服务数组。您可以通过以下方式逐个注入服务:
<services>
<service id="notifications_decorator" class="\NotificationsDecorator">
<argument type="service" id="decorator1"/>
<argument type="service" id="decorator2"/>
<argument type="service" id="decorator3"/>
</service>
</services>
或者(在我看来更好的方式)标记 decorators
服务并notifications_decorator
在编译过程中将它们注入。
更新:使用标记服务
在您的情况下,您必须像这样修改您的服务:
<services>
<service id="decorator1" class="\FirstDecorator">
<tag name="acme_decorator" />
</service>
<service id="decorator2" class="\SecondDecorator">
<tag name="acme_decorator" />
</service>
<service id="decorator3" class="\ThirdDecorator">
<tag name="acme_decorator" />
</service>
</services>
另外,您应该从部分中删除decorators.all
参数。<parameters>
接下来,您必须为以下添加类似addDectorator
功能\NotificationsDecorator
:
class NotificationsDecorator
{
private $decorators = array();
public function addDecorator($decorator)
{
$this->decorators[] = $decorator;
}
// more code
}
如果您为decorator
's 编写一些接口并将其添加为$decorator
foraddDecorator
函数的类型,那就太好了。
接下来,您必须编写自己的编译器通行证并询问他们有关标记服务的信息,并将此服务添加到另一个服务(类似于 doc):
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\Reference;
class DecoratorCompilerPass implements CompilerPassInterface
{
public function process(ContainerBuilder $container)
{
if (!$container->hasDefinition('notifications_decorator')) {
return;
}
$definition = $container->getDefinition('notifications_decorator');
$taggedServices = $container->findTaggedServiceIds('acme_decorator');
foreach ($taggedServices as $id => $attributes) {
$definition->addMethodCall(
'addDecorator',
array(new Reference($id))
);
}
}
}
最后,您应该将您的添加DecoratorCompilerPass
到Compiler
您的捆绑类中,例如:
class AcmeDemoBundle extends Bundle
{
public function build(ContainerBuilder $container)
{
parent::build($container);
$container->addCompilerPass(new DecoratorCompilerPass());
}
}
祝你好运!