app/Plugin/TabaHtmlEditor2/TabaHtmlEditorEvent.php line 74

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\TabaHtmlEditor2;
  9. use Eccube\Event\TemplateEvent;
  10. use Eccube\Request\Context;
  11. use Doctrine\ORM\EntityManagerInterface;
  12. use Plugin\TabaHtmlEditor2\Common\UserConfig;
  13. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  14. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  15. use Symfony\Component\HttpKernel\Event\ControllerArgumentsEvent;
  16. use Symfony\Component\HttpKernel\KernelEvents;
  17. class TabaHtmlEditorEvent implements EventSubscriberInterface
  18. {
  19.     /**
  20.      * @var EntityManagerInterface
  21.      */
  22.     private $entityManager;
  23.     /**
  24.      *
  25.      * @var EventDispatcherInterface
  26.      */
  27.     private $eventDispatcher;
  28.     /**
  29.      *
  30.      * @var Context
  31.      */
  32.     private $requestContext;
  33.     /**
  34.      * @var array
  35.      */
  36.     private $eccubeConfig;
  37.     public function __construct(
  38.         EntityManagerInterface $entityManager,
  39.         EventDispatcherInterface $eventDispatcher,
  40.         Context $requestContext)
  41.     {
  42.         $this->entityManager $entityManager;
  43.         $this->eventDispatcher $eventDispatcher;
  44.         $this->requestContext $requestContext;
  45.     }
  46.     /**
  47.      *
  48.      * {@inheritdoc}
  49.      *
  50.      * @return array
  51.      */
  52.     public static function getSubscribedEvents()
  53.     {
  54.         return [
  55.             KernelEvents::CONTROLLER_ARGUMENTS => [
  56.                 [
  57.                     'onKernelControllerArguments',
  58.                     100000000
  59.                 ],
  60.             ]
  61.         ];
  62.     }
  63.     /**
  64.      * @param ControllerArgumentsEvent $event
  65.      */
  66.     public function onKernelControllerArguments(ControllerArgumentsEvent $event): void
  67.     {
  68.         //
  69.         // 管理画面イベント
  70.         //
  71.         if ($this->requestContext->isAdmin()) {
  72.             //
  73.             // テンプレートイベント
  74.             //
  75.             if ($event->getRequest()->attributes->has('_template') && ($template $event->getRequest()->attributes->get('_template'))) {
  76.                 if ($template->getTemplate()) {
  77.                     $templateName str_replace('@','',$template->getTemplate());
  78.                     $userConfig UserConfig::getInstance();
  79.                     if (($pages $userConfig->get('page'))) {
  80.                         foreach ($pages as $page) {
  81.                             if (
  82.                                 $page
  83.                                 && $page['template']
  84.                                 && $templateName == str_replace('@','',$page['template'])
  85.                                 && isset($page['selector'])
  86.                             ) {
  87.                                 $this->eventDispatcher->addListener($template->getTemplate(), function (TemplateEvent $templateEvent) use ($page) {
  88.                                     if (!is_array($page['selector'])) $page['selector'] = [$page['selector']];
  89.                                     $templateEvent->setParameter("selectors",$page['selector']);
  90.                                     $templateEvent->addSnippet('@TabaHtmlEditor2/admin/snippet/editor.twig');
  91.                                     $templateEvent->addAsset('@TabaHtmlEditor2/admin/snippet/asset.twig');
  92.                                 });
  93.                                 break;
  94.                             }
  95.                         }
  96.                     }
  97.                 }
  98.             }
  99.         }
  100.     }
  101. }