F
F
First Name2019-09-27 10:18:17
symfony
First Name, 2019-09-27 10:18:17

What is the correct way to name a service in Symfony to be called in different places?

Hello.
There is
a BaseBuilder class

public function __construct(string $file, array $mapping)
    {
        $this->file = $file;
        $this->mapping = $mapping;
    }

I use this class in two different services, which I named differently.
FirstBuilder:
    public: true
    autowire: false
    class: App\Builder\BuilderBase
    arguments:
      $file: '%kernel.project_dir%/public/files/first.xml'
      $mapping: '%first.mapping%'
 TwoBuilder:
    public: true
    autowire: false
    class: App\Builder\BuilderBase
    arguments:
      $file: '%kernel.project_dir%/public/files/two.xml'
      $mapping: '%two.mapping%'

There is also a factory, into which one of the services is thrown
App\Factory\Point\Test:
    public: true
    class: App\Factory\Point\Test
    arguments:
      $creator: '@App\Creator\TestCreator'
      $sender: '@App\Sender\TestSender'
      $builder: '@FirstBuilder'

BUT it throws an error: C
Cannot autowire service "App\Builder\BuilderBase": argument "$file" of method "__construct()" is type-hinted "string", you should configure its value explicitly.
Where is the mistake?
Documentation and Google didn't help :S
Thank you in advance for your answers.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey Skobkin, 2019-09-27
@skobkin

What is "not working"?
Well, you yourself turned off autowiring in the examples above. For example, here:

TwoBuilder:
    public: true
    ###
    autowire: false
    ###
    class: App\Builder\BuilderBase
    arguments:
      $file: '%kernel.project_dir%/public/files/two.xml'
      $mapping: '%two.mapping%'

Of course, I could be wrong, but substitution of arguments by name is, sort of, part of autowiring.
Another thought is that most likely your services FirstBuilderand TwoBilder(correctly: SecondBilder) are configured correctly (if you omit the potential problem with passing by name without autowiring), and the configuration error occurs with the service App\Builder\BuilderBase. Please note that I do not mean the class, but the service ID. That is, in fact, you have a manual configuration of two "virtual" services, so to speak, and one configuration of the default service representation, which is created automatically (because autowiring and autoconfiguration are enabled globally, which you did not show in the question, by the way).
In other words:
- Service FirstBuilderis valid and configured normally
- Service TwoBuilder is valid and configured normally
- ServiceApp\Builder\BuilderBase- tries to configure automatically, but can't because for scalar arguments, arrays, and everything else, what does a service need or default value bindings (see services._defaults.bind)? Or explicitly passing arguments in the configuration block you wrote.
So, in order not to disable autowiring globally, it would be better for you to make one builder default with the service ID App\Builder\BuilderBaseso that the system does not try to configure another service for this class, and the second builder is already called whatever you like.
Conclusion:
Read about innovations in DI after 3.3 (or better, about later ones), comprehend, refactor the configuration.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question