3

Hoes does Symfony resolve the Sylius service sylius.controller.shop_user service to the controller class file Sylius\Bundle\UserBundle\Controller\UserController.

My understanding is that sylius.controller.shop_user is a service, and that in Symfony there will be a corresponding service configuration. This service configuration will tell Symfony which class to use when it needs to instantiate the service.

However, I can't seem to find a sylius.controller.shop_user configuration in the Sylius source configuration anywhere. There's just references to this service in routing files

#File: src/Sylius/Bundle/ShopBundle/Resources/config/routing/ajax/user.yml
sylius_shop_ajax_user_check_action:
    path: /check
    methods: [GET]
    defaults:
        _controller: sylius.controller.shop_user:showAction
        _format: json
        _sylius:
            repository:
                method: findOneByEmail
                arguments:
                    email: $email
            serialization_groups: [Secured]

or in on-disk container cache files.

var/cache/dev/srcKernelDevDebugContainer.xml
1798:    <parameter key="sylius.controller.shop_user.class">Sylius\Bundle\UserBundle\Controller\UserController</parameter>
15230:    <service id="sylius.controller.shop_user" class="Sylius\Bundle\UserBundle\Controller\UserController" public="true">

So how does Symfony know to instantiate the right class for this service?

Is there configuration I'm not seeing? Some Symfony magic that auto-generates the class? Some other mysterious third thing where I don't know what I don't know?

I don't have any specific task in mind, I'm just trying to get a feel for how Sylius and Symfony work under the hood.

Alana Storm
  • 164,128
  • 91
  • 395
  • 599
  • 1
    It is probably configured dynamically in the SyliusBundle dependency injection Extension class. Just a guess but the Extension class is where most of the di magic tends to occur. – Cerad Jan 22 '19 at 23:13
  • @Cerad as part of Symfony's normal dynamic controller handler? Or something special Sylius is doing? Or something else? – Alana Storm Jan 23 '19 at 04:07
  • I have not peeked under the hood for Sylius but the extension is where services are usually defined. If it is not defined in the extension itself the almost certainly in a compiler pass. A controller is nothing special. Symfony routing will pull any service you specify for _controller and call the action on it. Try a search through the entire code base for shop_user. That should take you right to the definition. – Cerad Jan 23 '19 at 13:03
  • So out of curiosity I loaded Sylius and poked around a bit. It is indeed a huge project. Many many many levels of abstraction. @pamil's answer below is a good starting point. – Cerad Jan 23 '19 at 13:46

2 Answers2

1

The controller service is defined based on ResourceBundle's configuration in Sylius\Bundle\ResourceBundle\DependencyInjection\Driver\AbstractDriver::addController. This driver is called when loading a bundle.

pamil
  • 980
  • 2
  • 10
  • 20
1

Services with the name sylius.controller.[entity-name] are part of the Sylius entity resource system. As best I can tell, when you define your new doctrine entities in a specific way and register them as a Sylius resource, Sylius will automatically generate these controller services based on your configuration.

The actual line of code that defines these services is here.

#File: src/Sylius/Bundle/ResourceBundle/DependencyInjection/Driver/AbstractDriver.php
/* ... */
$container->setDefinition($metadata->getServiceId('controller'), $definition);
/* ... */

The Sylius\Bundle\ResourceBundle\DependencyInjection\Driver\AbstractDriver class is a (as of 1.3) a base class for the Sylius\Bundle\ResourceBundle\DependencyInjection\Driver\Doctrine\DoctrineORMDriver class. How this class ends up being used is by Symfony is unclear, but is fortunately beyond the scope of this answer.

Alana Storm
  • 164,128
  • 91
  • 395
  • 599