Symfony - 使用另一个捆绑服务

时间:2018-03-16 16:53:13

标签: php symfony symfony-3.4

我正在尝试使用其他捆绑包中的服务,而不会在捆绑包之间创建任何依赖关系。我试图使用CompilerPass但这可能不是最好的方法。

我在BarBundle中创建了一个ParamConverter,FooBundle应该为它的控制器使用。

但是我收到错误:No converter named foo_bundle.converter_service found for conversion of parameter fooObject

到目前为止我所拥有的:

应用程序/配置/ config.yml

foo_bundle:
    converter_service: bar_bundle.converter.object

的src / BarBundle /配置/ service.yml

services:
    bar_bundle.converter.object:
        class: BarBundle\ParamConverter\ObjectParamConverter
        tags:
            - { name: request.param_converter, priority: 0, converter: bar_bundle.converter.object }

的src / FooBundle / DependencyInjection / FooBundleExtension.php

public function load(array $configs, ContainerBuilder $container)
{
    $configuration = new Configuration();
    $config = $this->processConfiguration($configuration, $configs);

    $loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
    $loader->load('services.yml');

    $container->setParameter('foo_bundle.converter_service.id', $config['converter_service']);
}

的src / FooBundle / FooBundle.php

public function build(ContainerBuilder $container)
{
    parent::build($container);

    $container->addCompilerPass(new CompilerPass());
}

的src / FooBundle / DependencyInjection / CompilerPass.php

public function process(ContainerBuilder $container)
{
    $convertserService = $container->getDefinition($container->getParameter('foo_bundle.converter_service.id'));
    $container->setDefinition('foo_bundle.converter_service', $convertserService);
}

控制器注释:

/**
 * Show Feed document.
 *
 * @Route("/{id}/add", name="object_add")
 * @ParamConverter("fooObject", converter="foo_bundle.converter_service")
 */
public function addAction(ObjectInterface $fooObject)

如果我在CompilerPass中调用$container->get('foo_bundle.converter_service'),我可以看到对象正确设置。

这是一个优先问题吗?即是解析注释后的CompilerPass运行。或者这只是错误的做法?

1 个答案:

答案 0 :(得分:0)

由于c.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: // Button Pressed soundPool.stop(SID1_c); SID1_c = soundPool.play(sound_c, 1, 1, 1, 0, 0); c.setBackgroundResource(R.drawable.key4); return true; case MotionEvent.ACTION_UP:// Button released handler =new Handler(); handler.postDelayed(new Runnable() { @Override public void run() { soundPool.stop(SID1_c); } },90); return false; } return false; } });也加载@ParamConverter,您可能必须先强制​​执行。{/ p>

请注意,默认编译器传递优先级为0且优先级越高,执行得越早。

试试这个:

CompilerPass