app/Plugin/TabaCMS2/TabaCMSEvent.php line 115

Open in your IDE?
  1. <?php
  2. /*
  3.  * Copyright (C) 2018 SPREAD WORKS Inc.
  4.  *
  5.  * For the full copyright and license information, please view the LICENSE
  6.  * file that was distributed with this source code.
  7.  */
  8. namespace Plugin\TabaCMS2;
  9. use Plugin\TabaCMS2\Common\Constants;
  10. use Plugin\TabaCMS2\Repository\TypeRepository;
  11. use Plugin\TabaCMS2\Repository\PostRepository;
  12. use Eccube\Event\TemplateEvent;
  13. use Eccube\Request\Context;
  14. use Eccube\Common\EccubeConfig;
  15. use Symfony\Component\DependencyInjection\ContainerInterface;
  16. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  17. use Symfony\Component\Security\Csrf\CsrfTokenManagerInterface;
  18. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  19. use Symfony\Component\HttpKernel\Event\ControllerArgumentsEvent;
  20. use Symfony\Component\HttpKernel\KernelEvents;
  21. use Doctrine\ORM\EntityManagerInterface;
  22. class TabaCMSEvent implements EventSubscriberInterface
  23. {
  24.     /**
  25.      * @var ContainerInterface
  26.      */
  27.     private $container;
  28.     /**
  29.      * @var EntityManagerInterface
  30.      */
  31.     private $entityManager;
  32.     /**
  33.      *
  34.      * @var EventDispatcherInterface
  35.      */
  36.     private $eventDispatcher;
  37.     /**
  38.      *
  39.      * @var Context
  40.      */
  41.     private $requestContext;
  42.     /**
  43.      *
  44.      * @var CsrfTokenManagerInterface
  45.      */
  46.     private $csrfTokenManager;
  47.     /**
  48.      * @var array
  49.      */
  50.     private $eccubeConfig;
  51.     /**
  52.      * @var TypeRepository
  53.      */
  54.     private $typeRepo;
  55.     /**
  56.      * @var PostRepository
  57.      */
  58.     private $postRepo;
  59.     public function __construct(
  60.         ContainerInterface $container,
  61.         EntityManagerInterface $entityManager,
  62.         EventDispatcherInterface $eventDispatcher,
  63.         Context $requestContext,
  64.         CsrfTokenManagerInterface $csrfTokenManager,
  65.         EccubeConfig $eccubeConfig,
  66.         TypeRepository $typeRepo,
  67.         PostRepository $postRepo)
  68.     {
  69.         $this->container $container;
  70.         $this->entityManager $entityManager;
  71.         $this->eventDispatcher $eventDispatcher;
  72.         $this->requestContext $requestContext;
  73.         $this->csrfTokenManager $csrfTokenManager;
  74.         $this->eccubeConfig $eccubeConfig;
  75.         $this->typeRepo $typeRepo;
  76.         $this->postRepo $postRepo;
  77.     }
  78.     /**
  79.      *
  80.      * {@inheritdoc}
  81.      *
  82.      * @return array
  83.      */
  84.     public static function getSubscribedEvents()
  85.     {
  86.         return [
  87.             KernelEvents::CONTROLLER_ARGUMENTS => [
  88.                 [
  89.                     'onKernelController',
  90.                     100000000
  91.                 ],
  92.             ]
  93.         ];
  94.     }
  95.     /**
  96.      * @param ControllerArgumentsEvent $event
  97.      */
  98.     public function onKernelController(ControllerArgumentsEvent $event)
  99.     {
  100.         //
  101.         // 管理画面イベント
  102.         //
  103.         if ($this->requestContext->isAdmin()) {
  104.             //
  105.             // テンプレートイベント
  106.             //
  107.             if ($event->getRequest()->attributes->has('_template')) {
  108.                 $template $event->getRequest()->attributes->get('_template');
  109.                 $this->eventDispatcher->addListener($template->getTemplate(), function (TemplateEvent $templateEvent) {
  110.                     // 管理画面のナビゲーションにtaba app のメニューを差し込みます。
  111.                     $taba $this->container->get(Constants::CONTAINER_KEY_NAME);
  112.                     if (!$taba->get(Constants::PLUGIN_CATEGORY_ID ".menu")) {
  113.                         $templateEvent->addSnippet('@TabaCMS2/admin/snippet/nav_taba_app.twig');
  114.                         $taba->set(Constants::PLUGIN_CATEGORY_ID ".menu",true);
  115.                     }
  116.                     // Taba CMSのメニューを差し込みます。
  117.                     $templateEvent->setParameter("type_list",$this->typeRepo->findAll()); // 投稿タイプリストをセット
  118.                     $templateEvent->addSnippet('@TabaCMS2/admin/snippet/nav.twig');
  119.                 });
  120.             }
  121.         }
  122.     }
  123. }