Symfony - use service from another bundle
Asked Answered
A

1

8

I am attempting to use a service from another bundle, without creating any dependencies between the bundles. I'm trying to use CompilerPass but this might not be the best method.

I have created a ParamConverter in BarBundle which FooBundle should use for it's Controllers.

However I get the error: No converter named foo_bundle.converter_service found for conversion of parameter fooObject

What I have so far:

app/config/config.yml

foo_bundle:
    converter_service: bar_bundle.converter.object

src/BarBundle/config/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);
}

Controller Annotation:

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

If I call $container->get('foo_bundle.converter_service') in the CompilerPass I can see the object is correctly getting set.

Is it a priority issue? I.e. is the CompilerPass run after annotations are parsed. Or is this just the wrong approach?

Avaavadavat answered 16/3, 2018 at 16:53 Comment(0)
E
1

As @ParamConverter are also load with a CompilerPass, you probably have to force yours to be execute first.

Note that default compiler pass priority is 0 and higher the priority, the earlier it's executed.

Try this :

public function build(ContainerBuilder $container)
{
    parent::build($container);
    $container->addCompilerPass(new CompilerPass(), PassConfig::TYPE_BEFORE_OPTIMIZATION, 30);
}
Exuberate answered 16/3, 2018 at 18:6 Comment(1)
I can see that should work having read the docs symfony.com/doc/3.4/components/dependency_injection/…, but I still get the error No converter named foo_bundle.converter_service found for conversion of parameter fooObject. I think I will try a different approach by splitting the paramConverter into method calls within the action.Avaavadavat

© 2022 - 2024 — McMap. All rights reserved.