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');
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?
source share