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

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.     /**
  23.      * @var Security
  24.      */
  25.     private $security;
  26.     /**
  27.      * @var RouterInterface
  28.      */
  29.     private $router;
  30.     /**
  31.      * @var RequestStack
  32.      */
  33.     private $requestStack;
  34.     /**
  35.      * @var TranslatorInterface
  36.      */
  37.     private $translator;
  38.     /**
  39.      * @var bool
  40.      */
  41.     private $debug;
  42.     /**
  43.      * @var string
  44.      */
  45.     private $managerPath;
  46.     /**
  47.      * @var JwtManager
  48.      */
  49.     private $jwtManager;
  50.     public function __construct(Security $securityRouterInterface $routerRequestStack $requestStackTranslatorInterface $translatorbool $debug, ?string $managerPath, ?JwtManager $jwtManager)
  51.     {
  52.         $this->security $security;
  53.         $this->router $router;
  54.         $this->requestStack $requestStack;
  55.         $this->translator $translator;
  56.         $this->debug $debug;
  57.         $this->managerPath $managerPath;
  58.         $this->jwtManager $jwtManager;
  59.     }
  60.     public function __invoke(MenuEvent $event): void
  61.     {
  62.         if (!$this->security->isGranted('ROLE_ADMIN')) {
  63.             return;
  64.         }
  65.         $this->addDebugButton($event);
  66.         $this->addManagerLink($event);
  67.     }
  68.     /**
  69.      * Adds a debug button to the back end header navigation.
  70.      */
  71.     private function addDebugButton(MenuEvent $event): void
  72.     {
  73.         if (null === $this->jwtManager) {
  74.             return;
  75.         }
  76.         $tree $event->getTree();
  77.         if ('headerMenu' !== $tree->getName()) {
  78.             return;
  79.         }
  80.         if (!$request $this->requestStack->getCurrentRequest()) {
  81.             throw new \RuntimeException('The request stack did not contain a request');
  82.         }
  83.         $params = [
  84.             'do' => 'debug',
  85.             'key' => $this->debug 'disable' 'enable',
  86.             'referer' => base64_encode($request->server->get('QUERY_STRING''')),
  87.             'ref' => $request->attributes->get('_contao_referer_id'),
  88.         ];
  89.         $class 'icon-debug';
  90.         if ($this->debug) {
  91.             $class .= ' hover';
  92.         }
  93.         $debug $event->getFactory()
  94.             ->createItem('debug')
  95.             ->setLabel('debug_mode')
  96.             ->setUri($this->router->generate('contao_backend'$params))
  97.             ->setLinkAttribute('class'$class)
  98.             ->setLinkAttribute('title'$this->translator->trans('debug_mode', [], 'ContaoManagerBundle'))
  99.             ->setExtra('translation_domain''ContaoManagerBundle')
  100.         ;
  101.         $children = [];
  102.         // Try adding the debug button after the alerts button
  103.         foreach ($tree->getChildren() as $name => $item) {
  104.             $children[$name] = $item;
  105.             if ('alerts' === $name) {
  106.                 $children['debug'] = $debug;
  107.             }
  108.         }
  109.         // Prepend the debug button if it could not be added above
  110.         if (!isset($children['debug'])) {
  111.             $children = ['debug' => $debug] + $children;
  112.         }
  113.         $tree->setChildren($children);
  114.     }
  115.     /**
  116.      * Adds a link to the Contao Manager to the back end main navigation.
  117.      */
  118.     private function addManagerLink(MenuEvent $event): void
  119.     {
  120.         if (null === $this->managerPath) {
  121.             return;
  122.         }
  123.         $categoryNode $event->getTree()->getChild('system');
  124.         if (null === $categoryNode) {
  125.             return;
  126.         }
  127.         $item $event->getFactory()
  128.             ->createItem('contao_manager')
  129.             ->setLabel('Contao Manager')
  130.             ->setUri('/'.$this->managerPath)
  131.             ->setLinkAttribute('class''navigation contao_manager')
  132.             ->setLinkAttribute('title'$this->translator->trans('contao_manager_title', [], 'ContaoManagerBundle'))
  133.             ->setExtra('translation_domain'false)
  134.         ;
  135.         $categoryNode->addChild($item);
  136.     }
  137. }