vendor/contao/manager-bundle/src/EventListener/BackendMenuListener.php line 46

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4.  * This file is part of Contao.
  5.  *
  6.  * (c) Leo Feyer
  7.  *
  8.  * @license LGPL-3.0-or-later
  9.  */
  10. namespace Contao\ManagerBundle\EventListener;
  11. use Contao\CoreBundle\Event\MenuEvent;
  12. use Contao\ManagerBundle\HttpKernel\JwtManager;
  13. use Symfony\Component\HttpFoundation\RequestStack;
  14. use Symfony\Component\Routing\RouterInterface;
  15. use Symfony\Component\Security\Core\Security;
  16. use Symfony\Contracts\Translation\TranslatorInterface;
  17. /**
  18.  * @internal
  19.  */
  20. class BackendMenuListener
  21. {
  22.     private Security $security;
  23.     private RouterInterface $router;
  24.     private RequestStack $requestStack;
  25.     private TranslatorInterface $translator;
  26.     private bool $debug;
  27.     private ?string $managerPath;
  28.     private ?JwtManager $jwtManager;
  29.     public function __construct(Security $securityRouterInterface $routerRequestStack $requestStackTranslatorInterface $translatorbool $debug, ?string $managerPath, ?JwtManager $jwtManager)
  30.     {
  31.         $this->security $security;
  32.         $this->router $router;
  33.         $this->requestStack $requestStack;
  34.         $this->translator $translator;
  35.         $this->debug $debug;
  36.         $this->managerPath $managerPath;
  37.         $this->jwtManager $jwtManager;
  38.     }
  39.     public function __invoke(MenuEvent $event): void
  40.     {
  41.         if (!$this->security->isGranted('ROLE_ADMIN')) {
  42.             return;
  43.         }
  44.         $this->addDebugButton($event);
  45.         $this->addManagerLink($event);
  46.     }
  47.     /**
  48.      * Adds a debug button to the back end header navigation.
  49.      */
  50.     private function addDebugButton(MenuEvent $event): void
  51.     {
  52.         if (null === $this->jwtManager) {
  53.             return;
  54.         }
  55.         $tree $event->getTree();
  56.         if ('headerMenu' !== $tree->getName()) {
  57.             return;
  58.         }
  59.         if (!$request $this->requestStack->getCurrentRequest()) {
  60.             throw new \RuntimeException('The request stack did not contain a request');
  61.         }
  62.         $params = [
  63.             'do' => 'debug',
  64.             'key' => $this->debug 'disable' 'enable',
  65.             'referer' => base64_encode($request->server->get('QUERY_STRING''')),
  66.             'ref' => $request->attributes->get('_contao_referer_id'),
  67.         ];
  68.         $class 'icon-debug';
  69.         if ($this->debug) {
  70.             $class .= ' hover';
  71.         }
  72.         $debug $event->getFactory()
  73.             ->createItem('debug')
  74.             ->setLabel('debug_mode')
  75.             ->setUri($this->router->generate('contao_backend'$params))
  76.             ->setLinkAttribute('class'$class)
  77.             ->setLinkAttribute('title'$this->translator->trans('debug_mode', [], 'ContaoManagerBundle'))
  78.             ->setExtra('translation_domain''ContaoManagerBundle')
  79.         ;
  80.         $children = [];
  81.         // Try adding the debug button after the alerts button
  82.         foreach ($tree->getChildren() as $name => $item) {
  83.             $children[$name] = $item;
  84.             if ('alerts' === $name) {
  85.                 $children['debug'] = $debug;
  86.             }
  87.         }
  88.         // Prepend the debug button if it could not be added above
  89.         if (!isset($children['debug'])) {
  90.             $children = ['debug' => $debug] + $children;
  91.         }
  92.         $tree->setChildren($children);
  93.     }
  94.     /**
  95.      * Adds a link to the Contao Manager to the back end main navigation.
  96.      */
  97.     private function addManagerLink(MenuEvent $event): void
  98.     {
  99.         if (null === $this->managerPath) {
  100.             return;
  101.         }
  102.         $categoryNode $event->getTree()->getChild('system');
  103.         if (null === $categoryNode) {
  104.             return;
  105.         }
  106.         $item $event->getFactory()
  107.             ->createItem('contao_manager')
  108.             ->setLabel('Contao Manager')
  109.             ->setUri('/'.$this->managerPath)
  110.             ->setLinkAttribute('class''navigation contao_manager')
  111.             ->setLinkAttribute('title'$this->translator->trans('contao_manager_title', [], 'ContaoManagerBundle'))
  112.             ->setExtra('translation_domain'false)
  113.         ;
  114.         $categoryNode->addChild($item);
  115.     }
  116. }