Choosing one of many services as a service to use

Scenario:

  • I use the dependency injection component as a standalone component. No packages / symfony -framework etc.
  • I have a set of services that are responsible for such things, for example. for writing data to the hard drive in various ways (for example, using ftp, direct file operations, saving to db, ...)
  • Each of these services requires a different configuration (e.g. sql credentials, ftp credentials, ...)
  • I want to define one of these services as a "shared service to use" for other services for which there is a dependency.
  • Which service to use is configured by

Example:

Given these simplified definitions of parameters and services:

<parameter key="file.adapterType">direct</parameter> <service id="file.db" /> <service id="file.ftp" /> <service id="file.direct" /> 

I would like to define an alias for the service that I really want to use

 <service id="file.adapter" alias="file.%file.adapterType%"/> 

for other services that simply rely on this service:

 <service id="photos"> <argument type="service" id="file.adapter" /> </service> 

What I have tried so far:

  • Defining an alias with a parameter in it alias-id. However, the parameter inside the alias identifier cannot be resolved.
  • Definition of the identifier directly in the arguments tag - again, the parameter is not allowed to it:

     <service id="photo"> <argument type="service" id="file.%file.adapterType%" /> </service> 
  • Creation of a factory. The get method gets the parameter and di container and returns the corresponding service:

     public function get(ContainerInterface $container, $parameter) { return $container->get('file.' . $parameter); } 

    However, the container is not registered as a service. Registering it in advance does not affect loading the configuration from the file (as a result, the error message “service container not found” appears):

     $container->set("container", $container); $loader = new XmlFileLoader($container, new FileLocator(__DIR__ . '/services')); $loader->load('fileadapter.xml'); // << gives error $container->get("container"); // << working 

Other alternatives?

I can’t set an alias after loading the configuration file, since there are services in the file that is dependent on the alias.

Has anyone got any other ideas on how to handle this?

+4
source share
3 answers

In fact, I believe that you were on the right track with the third paragraph in the “What I have tried so far” section, except that the service container is registered as a service, but it is called service_container , not container .

+1
source

If I understand your problem correctly, you can move file.* Services to a file and services that use file.adapter to another file, and create an alias file.adapter , creating an alias between downloading files. for instance

 $loader = new XmlFileLoader($container, new FileLocator(__DIR__ . '/services')); $loader->load('fileadapter.xml'); $defaultAdapterId = "file." . $container->getParameter('file.adapterType'); if($container->hasDefinition($defaultAdapterId)) { $container->setAlias('file.adapter', $defaultAdapterId); } $loader->load('other_services.xml'); 
+1
source

now (since version 2.7 actually), you can use the symfony expression language for the same thing, using only the configuration

see here http://symfony.com/doc/current/service_container/expression_language.html

0
source

Source: https://habr.com/ru/post/1444139/


All Articles