src/EventSubscriber/ControllerSubscriber.php line 42

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.     private $twigExtension;
  25.     public function __construct(SourceCodeExtension $twigExtension)
  26.     {
  27.         $this->twigExtension $twigExtension;
  28.     }
  29.     public static function getSubscribedEvents(): array
  30.     {
  31.         return [
  32.             KernelEvents::CONTROLLER => 'registerCurrentController',
  33.         ];
  34.     }
  35.     public function registerCurrentController(ControllerEvent $event): void
  36.     {
  37.         // this check is needed because in Symfony a request can perform any
  38.         // number of sub-requests. See
  39.         // https://symfony.com/doc/current/components/http_kernel/introduction.html#sub-requests
  40.         if ($event->isMasterRequest()) {
  41.             $this->twigExtension->setController($event->getController());
  42.         }
  43.     }
  44. }