1

I have several services: DieselCaseService, CarloanCaseService LvCaseService.

The controller decides which of services to get.

$type = $quickCheck["type"];
/**
 * @var $caseService \App\Service\Cases\CaseInterface
*/
$type = 'diesel; // for test purposes

$caseService = $this->get('case_service.' . $type);

The service aliases are declared like this:

case_service.diesel:
    alias: App\Service\Cases\DieselCaseService
    public: true
class DieselCaseService implements CaseInterface 
{
.
.
.
}

If I try to get the DieselCaseService, I get an error

Service "case_service.diesel" not found: even though it exists in the
app's container, the container inside
"App\Controller\Api\AccountController" is a smaller service locator that only knows about the "doctrine", "form.factory",
"fos_rest.view_handler", "http_kernel", "parameter_bag",
"request_stack", "router", "security.authorization_checker",
"security.csrf.token_manager", "security.token_storage", "serializer",
"session", "templating" and "twig" services. Try using dependency
injection instead.

What can I do? I don't want to inject all of the services to the controller

double-beep
  • 5,031
  • 17
  • 33
  • 41
olek07
  • 513
  • 2
  • 7
  • 21
  • 3
    This is a job for service subscribers. AbstractController is already a subscriber. You can add your own services as well. [Scroll until your see the controller example here](https://symfony.com/doc/current/service_container/service_subscribers_locators.html#including-services). There are several other approaches to this problem so read the docs. It is possible to make a CaseServiceLocator which will [automatically contain all services which implement CaseInterface](https://stackoverflow.com/questions/53411936/how-to-avoid-service-container-in-factory-models/53412862#53412862). – Cerad Feb 08 '19 at 11:59
  • 1
    Instead of config programming (very bad practise as you can see), you could add `getName()` method to `CaseInterface`. That way you could easily get to any service without bounding it to a string value you type in config. – Tomas Votruba Feb 08 '19 at 14:40
  • 1
    Und then only inject the CaseInterface into the Action? Or have I you misunderstood? Do you have an example? – olek07 Feb 08 '19 at 15:02
  • @olek07 Into the constructor rather. I've added a more detailed example to the answer. If something is unclear, feel free to ask in comments under it. – Tomas Votruba Feb 09 '19 at 10:00

3 Answers3

3

For any "multiple instances of same type by key" situation, you can use autowired array.

1. Autodiscovery Services with App\ namespace

services:
    _defaults:
        autowire: true

    App\:
        resource: ../src

2. Require autowired array in Constructor

<?php

namespace App\Controller\Api;

use App\Service\Cases\DieselCaseService

final class AccountController
{
    /**
     * @var CaseInterface[]
     */
    private $cases;

    /**
     * @param CaseInterface[] $cases
     */
    public function __construct(array $cases)
    {
        foreach ($cases as $case) {
            $this->cases[$case->getName()] = $cases;
        }
    }

    public function someAction(): void
    {
        $dieselCase = $this->cases['diesel']; // @todo maybe add validation for exisiting key
        $dieselCase->anyMethod();
    }
}

3. Register compiler pass in Kernel

The autowired array functionality is not in Symfony core. It's possible thanks to compiler passes. You can write your own or use this one:

use Symplify\PackageBuilder\DependencyInjection\CompilerPass\AutowireArrayParameterCompilerPass;

final class AppKernel extends Kernel
{
    protected function build(ContainerBuilder $containerBuilder): void
    {
        $containerBuilder->addCompilerPass(new AutowireArrayParameterCompilerPass);
    }
}

That's it! :)

I use it on all my projects and it works like a charm.


Read more in post about autowired arrays I wrote.

Tomas Votruba
  • 23,240
  • 9
  • 79
  • 115
  • 1
    From where does Symfony know, that the parameter $cases in the constructor means to inject all of the cases (DieselCaseService, CarloanCaseService LvCaseService)? And where is declared 'diesel' to be possible to be used here `$this->cases['diesel'];`? – olek07 Feb 09 '19 at 14:47
  • From the compiler pass. The name si set here `$this->cases[$case->getName()] = $cases;` You need to extend your interface with `getName()` method, that would provide the key you'll use in controller to match. – Tomas Votruba Feb 09 '19 at 15:46
  • Do I really need to do all services public? – olek07 Feb 09 '19 at 20:28
  • 1
    I have read, that making of all services public is a bad practice. Moreover I have an own solution. Look my answer – olek07 Feb 10 '19 at 09:52
  • Public is not relevant in my answer, I've removed that. Well, using container in your answer is the worst practice. That kills constructor injection and you invite services locator to your code. – Tomas Votruba Feb 10 '19 at 10:34
  • Why is the using container the worst practice? I've read about it, but I don't understand why. Is your solution working without `public: true`? – olek07 Feb 10 '19 at 11:01
  • 1
    It's hidden static in your code - very easy to use, but nearly impossible to get rid of lately. I wrote [about bad consequences here](https://www.tomasvotruba.cz/blog/2018/04/26/how-i-got-into-static-trap-and-made-fool-of-myself/). "Is your solution working without public: true?" Yes, I added it by accident, it's not relevant to the code. – Tomas Votruba Feb 10 '19 at 11:59
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/188175/discussion-between-tomas-votruba-and-olek07). – Tomas Votruba Feb 10 '19 at 12:57
0

I have created a wrapper-service

<?php
namespace App;

use Symfony\Component\DependencyInjection\ContainerInterface;

class ServiceFactory
{

    /** @var ContainerInterface */
    private $container;


    public function __construct(ContainerInterface $container)
    {
        $this->container = $container;
    }

    public function getService($alias)
    {
        return $this->container->get($alias);
    }

    /**
     * @param $alias
     * @return bool
     */
    public function hasService($alias)
    {
        return $this->container->has($alias);
    }
}

than I inject this service into controller

public function saveRegisterData(Request $request, AccountService $accountService, ServiceFactory $serviceFactory) 
{
.
.
.

    if (!$serviceFactory->hasService('case_service.'.$type)) {
        throw new \LogicException("no valid case_service found");
    }

    /**
    * @var $caseService \App\Service\Cases\CaseInterface
    */
    $caseService = $serviceFactory->getService('case_service.' . $type);

.
.
.
}

olek07
  • 513
  • 2
  • 7
  • 21
  • 1
    Injecting the full container is something to be avoided as it gives global access to all your services and can make testing a bit more challenging. Take a look at my comment in your original question. The one with three up votes. Basically you want to start with CaseServiceFactory extends ServiceLocater and move on from there. – Cerad Feb 10 '19 at 15:42
  • If I define the service locator like this: class DoingLocator extends ServiceLocator { protected $services = [ 'bar' => 'app.service.bar', 'baz' => 'app.service.baz' ]; public function locate($string) { return $this->get($this->services[$string]); } } I get the error: Invalid definition for service "App\DoingLocator": an array of references is expected as first argument when the "container.service_locator" tag is set. – olek07 Feb 10 '19 at 19:24
  • thanks for that ! i'm aware of the associated problem related to testing, but everything else is way too much boilerplate for such a basic need – Vincent Apr 24 '23 at 09:58
0

Some rainy Sunday afternoon code. This question is basically a duplicate of several other questions but there are enough moving parts that I suppose it is worthwhile to provide some specific solutions.

Start by defining the cases just to make sure we are all on the same page:

interface CaseInterface { }
class DieselCaseService implements CaseInterface {}
class CarloanCaseService implements CaseInterface{}

The easiest way to answer the question is to add the case services directly to the controller's container:

class CaseController extends AbstractController
{
    public function action1()
    {
        $case = $this->get(DieselCaseService::class);

        return new Response(get_class($case));
    }
    // https://symfony.com/doc/current/service_container/service_subscribers_locators.html#including-services
    public static function getSubscribedServices()
    {
        return array_merge(parent::getSubscribedServices(), [
            // ...
            DieselCaseService::class => DieselCaseService::class,
            CarloanCaseService::class => CarloanCaseService::class,
        ]);
    }
}

The only thing I changed for your question is using the class names for service ids instead of something like 'case_service.diesel'. You can of course tweak the code to use your ids but class names are more standard. Note that there is no need for any entries in services.yaml for this to work.

There are a couple of issues with the above code which may or may not be a problem. The first is that only the one controller will have access to the case services. Which may be all you need. The second potential issue is that you need to explicitly list all your case services. Which again might be okay but it might be nice to automatically pick up services which implement the case interface.

It's not hard to do but it does require following all the steps.

Start be defining a case locator:

use Symfony\Component\DependencyInjection\ServiceLocator;

class CaseLocator extends ServiceLocator
{

}

Now we need some code to find all the case services and inject them into the locator. There are several approaches but perhaps the most straight forward is to use the Kernel class.

# src/Kernel.php
// Make the kernel a compiler pass by implementing the pass interface
class Kernel extends BaseKernel implements CompilerPassInterface 
{
    protected function build(ContainerBuilder $container)
    {
        // Tag all case interface classes for later use
        $container->registerForAutoconfiguration(CaseInterface::class)->addTag('case');
    }
    // and this is the actual compiler pass code
    public function process(ContainerBuilder $container)
    {
        // Add all the cases to the case locator
        $caseIds = [];
        foreach ($container->findTaggedServiceIds('case') as $id => $tags) {
            $caseIds[$id] = new Reference($id);
        }
        $caseLocator = $container->getDefinition(CaseLocator::class);
        $caseLocator->setArguments([$caseIds]);
    }

If you follow all of the above steps then you can inject your case locator into any controller (or other service) that happens to need it:

class CaseController extends AbstractController
{
    public function action2(CaseLocator $caseLocator)
    {
        $case = $caseLocator->get(CarloanCaseService::class);

        return new Response(get_class($case));
    }

And once again, there is no need to make any changes to your services.yaml for all this to work.

Cerad
  • 48,157
  • 8
  • 90
  • 92
  • Thanks a lot! It works!!! I have a little question left. I want to get the service by their aliases, like this `$caseService = $caseLocator->get('case_service.' . $type)` Actually it work only with the class name `$case = $caseLocator->get(CarloanCaseService::class);` – olek07 Feb 11 '19 at 08:35
  • To be perfectly honest, in my opinion, tweaking the code to use aliases is something best left to the student. – Cerad Feb 11 '19 at 12:17