app/Plugin/EccubePaymentLite4/EventListener/EventSubscriber/Admin/AddRegularNav.php line 47

Open in your IDE?
  1. <?php
  2. namespace Plugin\EccubePaymentLite4\EventListener\EventSubscriber\Admin;
  3. use Eccube\Event\TemplateEvent;
  4. use Eccube\Request\Context;
  5. use Plugin\EccubePaymentLite4\Entity\Config;
  6. use Plugin\EccubePaymentLite4\Repository\ConfigRepository;
  7. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
  8. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  9. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  10. use Symfony\Component\HttpKernel\Event\FilterControllerEvent;
  11. use Symfony\Component\HttpKernel\KernelEvents;
  12. class AddRegularNav implements EventSubscriberInterface
  13. {
  14.     /**
  15.      * @var Context
  16.      */
  17.     private $context;
  18.     /**
  19.      * @var ConfigRepository
  20.      */
  21.     private $configRepository;
  22.     /**
  23.      * @var EventDispatcherInterface
  24.      */
  25.     private $eventDispatcher;
  26.     public function __construct(
  27.         Context $context,
  28.         ConfigRepository $configRepository,
  29.         EventDispatcherInterface $eventDispatcher
  30.     ) {
  31.         $this->context $context;
  32.         $this->configRepository $configRepository;
  33.         $this->eventDispatcher $eventDispatcher;
  34.     }
  35.     public static function getSubscribedEvents()
  36.     {
  37.         return [
  38.             KernelEvents::CONTROLLER_ARGUMENTS => 'onKernelController',
  39.         ];
  40.     }
  41.     public function onKernelController(FilterControllerEvent $event)
  42.     {
  43.         if (!$this->context->isAdmin()) {
  44.             return;
  45.         }
  46.         /** @var Config $Config */
  47.         $Config $this->configRepository->find(1);
  48.         if (!$Config->getRegular()) {
  49.             return;
  50.         }
  51.         if (!$event->getRequest()->attributes->has('_template')) {
  52.             return;
  53.         }
  54.         /* @var Template $template */
  55.         $template $event->getRequest()->attributes->get('_template');
  56.         $this->eventDispatcher->addListener($template->getTemplate(), function (TemplateEvent $templateEvent) {
  57.             $templateEvent->addSnippet('@EccubePaymentLite4/admin/Nav/regular_nav.twig');
  58.         });
  59.     }
  60. }