src/EventSubscriber/PageControllerSubscriber.php line 25

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use App\Controller\PageControllerInterface;
  4. use Doctrine\ORM\EntityManagerInterface;
  5. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  6. use Symfony\Component\HttpKernel\Event\ControllerEvent;
  7. use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
  8. use Symfony\Component\HttpKernel\KernelEvents;
  9. use Symfony\Contracts\Translation\TranslatorInterface;
  10. class PageControllerSubscriber implements EventSubscriberInterface
  11. {
  12. private $translator;
  13. private $em;
  14. public function __construct(TranslatorInterface $translator, EntityManagerInterface $em)
  15. {
  16. $this->translator = $translator;
  17. $this->em = $em;
  18. }
  19. public function onKernelController(ControllerEvent $event)
  20. {
  21. $controller = $event->getController();
  22. // when a controller class defines multiple action methods, the controller
  23. // is returned as [$controllerInstance, 'methodName']
  24. if (is_array($controller)) {
  25. $controller = $controller[0];
  26. }
  27. if ($controller instanceof PageControllerInterface) {
  28. $controller->initPage($this->translator, $this->em);
  29. }
  30. }
  31. public static function getSubscribedEvents(): array
  32. {
  33. return [
  34. KernelEvents::CONTROLLER => 'onKernelController',
  35. ];
  36. }
  37. }