0

I have an interface:

<?php
interface FooInterface
{
    function doSth();

}

and two classes that implements this interface:

<?php
class FooImplementationOne implements FooInterface
{
}

class FooImplementationTwo implements FooInterface
{
}

And now i want to autowire one of classes that implements FooInterface based on called endpoint:

<?php
class Service
{
    public function __construct(
        private FooInterface $interface
    );

}

Is there any chance to achieve that using some kind of Resolver/Provider?

I've tried implementing class that implements ServiceSubscriberInterface (this didnt work at all) and another one class implementing ArgumentValueResolverInterface (that one worked for controllers, but obviously not for services :( )

Calliso
  • 31
  • 6
  • 1
    You can use [named aliases](https://symfony.com/doc/current/service_container/autowiring.html#dealing-with-multiple-implementations-of-the-same-type) in which the service will be decided based on the name of the argument `FooInterface $foo1` `FooInterface $foo2`. If that does not suit your needs then take another look at service locators. You end up injecting the locator and then picking which interface you need from it. – Cerad Feb 17 '23 at 18:30
  • Service locator is exactly what I need! Thank you very much for help – Calliso Feb 17 '23 at 21:28

0 Answers0