Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony 4 setter injection not working in bundle with or without autowire

I thought I fully understood Symfony's autowiring behaviour, but there must be something I'm missing and I'm hoping someone can fill in the blanks.

Three questions to begin with:

  • Is there a difference in the Symfony autowire behaviour in the main application vs in a bundle?
  • If you ask Symfony to autowire bundle services, does it completely ignore service definitions in the bundles services.yaml file?
  • Are there known issues with the setter injection functionality in Symfony 4?

I have a service definition within a bundle that uses setter injection but it seems to be completely ignored by Symfony, when I've asked Symfony to autowire my bundle services and even when I've asked Symfony to exclude services from the autowiring.

My application is using Symfony v4.1.3.

I have included my Bundle in my applications bundles.php file.

<?php

return [
  //... core bundles,
  Acme\\Symfony\\AcmeCustomBundle\\AcmeCustomBundle::class => ['all' => true] 
];

In the default Symfony application services.yaml file, I have asked Symfony to autowire my bundles services with the following:

Acme\Symfony\AcmeCustomBundle\:
    resource: '../vendor/acme-symfony/custom-bundle/*'
    exclude: '../vendor/acme-symfony/custom-bundle/{Model,Tests}'

In my bundles services.yaml file located in ../vendor/acme-symfony/custom-bundle/Resources/config/services.yaml, I have the following:

parameters:

services:
    Acme\Symfony\AcmeCustomBundle\Search\ConfigurationReader:
      calls:
        - method: setIndex
          arguments:
            $index: '%elasticsearch.index%'
        - method: setSchema
          arguments:
            $schema: '%elasticsearch.schema%'  

The parameters are set in my bundle extension class (extends configurable extension), and I have already verified that the parameters do exist and are getting set using the following method:

$container->setParameter('elasticsearch.index', $mergedConfigs['elasticsearch']['index']);
$container->setParameter('elasticsearch.schema', $mergedConfigs['elasticsearch']['schema']);

Now back to the problem. Symfony is not performing the setter injection, even when I tell Symfony to not autowire the above service by doing the following:

 Acme\Symfony\AcmeCustomBundle\:
    resource: '../vendor/acme-symfony/custom-bundle/*'
    exclude: '../vendor/acme-symfony/custom-bundle/{Model,Tests,Search}'

I did however get Symfony to configure my service when I

  • Turned off the autowiring for the above service
  • Used the factory method of service creation

This kind of answered my second question above, but I'm not 100% sure of it. Nonetheless, I would much rather not have to use a factory class just to get around what may be an issue with Symfony, or my lack of understanding of how the setter injection / autowiring really work.

Can anyone see something obvious that I'm missing?

like image 334
onetimeengineer Avatar asked Oct 23 '25 12:10

onetimeengineer


1 Answers

You can autowire other methods (e.g. Setters) if you want, just by using the @required annotation in your service:

/**
 * @required
 */
public function setFoo(FooInterface $foo)
{
    $this->foo = $foo;
}

Autowiring other methods

like image 141
fgamess Avatar answered Oct 27 '25 00:10

fgamess



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!