Dependency injection 向现有服务添加setter注入

Dependency injection 向现有服务添加setter注入,dependency-injection,symfony,doctrine-orm,Dependency Injection,Symfony,Doctrine Orm,我想向现有服务添加setter注入。基本服务的定义如下: [container] Information for service doctrine.orm.default_entity_manager Service Id doctrine.orm.default_entity_manager Class Doctrine\ORM\EntityManager Tags - Scope container Public yes 我已经

我想向现有服务添加setter注入。基本服务的定义如下:

[container] Information for service doctrine.orm.default_entity_manager

Service Id   doctrine.orm.default_entity_manager
Class        Doctrine\ORM\EntityManager
Tags         -
Scope        container
Public       yes
我已经在
config.yml
文件中重写了服务的class参数,以使用我自己的类,该类向基类添加了
setExample()
方法。服务定义现在看起来像:

[container] Information for service doctrine.orm.default_entity_manager

Service Id   doctrine.orm.default_entity_manager
Class        MyCustom\EntityManager
Tags         -
Scope        container
Public       yes

如何修改/覆盖此服务配置以在我自己的
services.yml
文件中添加我自己的setter注入

我要做的是在bundle DependencyInjection命名空间中使用扩展类。应该是这样的:

<?php

namespace YourNamespace\YourBundle\DependencyInjection;

use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\DependencyInjection\Loader;
use Symfony\Component\DependencyInjection\Definition;

/**
 * Provide a DIC extension to support configurable cheeta subscription adapters.
 */
class YourNamespaceYourBundleExtension extends Extension 
{    
    /**
     * {@inheritDoc}
     */
    public function load( array $configs, ContainerBuilder $container )
    {
        // Some stuff with configuration...
        $configuration = new Configuration();
        $config = $this->processConfiguration( $configuration, $configs );
        // Load service configuration
        $loader = new Loader\XmlFileLoader( $container, new FileLocator(__DIR__.'/../Resources/config' ) );
        $loader->load( 'services.xml' );
        $container->findDefinition('doctrine.orm.default_entity_manager')->addMethodCall('setExample', array(/* Eventual parameters */));
    }

}
load('services.xml');
$container->findDefinition('doctrine.orm.default_entity_manager')->addMethodCall('setExample',数组(/*最终参数*/);
}
}
使用服务继承可能也有同样的方法,但我从未尝试过。但是,在扩展类中,您可以执行与在service.xml文件中相同的操作,还可以使用php逻辑和语义配置来执行更高级的操作