src/EventSubscriber/ControllerSubscriber.php line 40

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Symfony package.
  4.  *
  5.  * (c) Fabien Potencier <fabien@symfony.com>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace App\EventSubscriber;
  11. use App\Twig\SourceCodeExtension;
  12. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  13. use Symfony\Component\HttpKernel\Event\ControllerEvent;
  14. use Symfony\Component\HttpKernel\KernelEvents;
  15. /**
  16.  * Defines the method that 'listens' to the 'kernel.controller' event, which is
  17.  * triggered whenever a controller is executed in the application.
  18.  *
  19.  * @author Ryan Weaver <weaverryan@gmail.com>
  20.  * @author Javier Eguiluz <javier.eguiluz@gmail.com>
  21.  */
  22. class ControllerSubscriber implements EventSubscriberInterface
  23. {
  24.     public function __construct(
  25.         private SourceCodeExtension $twigExtension
  26.     ) {
  27.     }
  28.     public static function getSubscribedEvents(): array
  29.     {
  30.         return [
  31.             KernelEvents::CONTROLLER => 'registerCurrentController',
  32.         ];
  33.     }
  34.     public function registerCurrentController(ControllerEvent $event): void
  35.     {
  36.         // this check is needed because in Symfony a request can perform any
  37.         // number of sub-requests. See
  38.         // https://symfony.com/doc/current/components/http_kernel.html#sub-requests
  39.         if ($event->isMainRequest()) {
  40.             $this->twigExtension->setController($event->getController());
  41.         }
  42.     }
  43. }