Блог Андрея

 
 

Comfortable service decoration in Symfony with console command decorate-controller

Ru.

When I decided to decorate the FOSUserBundle ResettingController controller, it turned out to be difficult to write a new service registration configuration. Besides the fact that I have to find the aliases of all the services that the original controller accepts, I have to write a class, almost all methods of which simply call the methods of the decorated controller (it's boring!).

We want to use good ready-made solutions as much as possible, which means that the operation of decorating the controller from a third-party bundle will be routine.

Because I created command:

php bin/console landlib:decorate-controller

which creates a «blank» for the controller class that decorates the desired service and displays in stdout the yaml fragment of the configuration of the new service. You just have to copy this configuration to your config/services.yaml.

Install

composer require landlib/symfonytoolsbundle

Add in your config/bundles.php array item

return [
// ...
Landlib\SymfonyToolsBundle\SymfonyToolsBundle::class => ['all' => true]
];

~~~~~~~~~

Usage

For example decorate FOS\UserBundle\Controller\ResettingController

php bin/console landlib:decorate-controller

Command will request enter the path to override controller. Need enter absolute path, for example

/home/user/sym3.4project/vendor/friendsofsymfony/user-bundle/Controller/ResettingController.php

After request command wil generate file

/home/user/sym3.4project/src/Controller/ResettingController.php

If file already exists, it will rewrite or remove! No copies are saved.

and will output the yaml fragment of the configuration:

Add in your configuration config/services.yaml:

==================

    App\Controller\ResettingController:
        decorates: fos_user.resetting.controller
        arguments:
            - '@App\Controller\ResettingController.inner'
            - '@event_dispatcher'
            - '@fos_user.resetting.form.factory'
            - '@fos_user.user_manager'
            - '@fos_user.util.token_generator'
            - '@fos_user.mailer'
            - '%fos_user.resetting.retry_ttl%'
            - '@service_container'

==================

Remember to change the name of the controller in the routes or annotation file.

Pay attention to the last line of output, the route (or routes) for those actions that you want to reload must be manually adjusted in the route configuration! (Because the team cannot know which of the controller actions you want to overload).

Troubles

If during the process you suddenly saw the error Cannot autowire service..., then you forgot to copy the configuration fragment to your services.yaml.

You can remove file

/home/user/sym3.4project/src/Controller/ResettingController.php

(file path from example Usage section)

or append yaml configuration fragment to your services.yaml.

For other features of the SymfonyToolsBundle package, see the github documentation.