app/Customize/Event/EstimateOrOrderTypeEvent.php line 92

Open in your IDE?
  1. <?php
  2. /*
  3. * Plugin Name : ProductOption
  4. *
  5. * Copyright (C) BraTech Co., Ltd. All Rights Reserved.
  6. * http://www.bratech.co.jp/
  7. *
  8. * For the full copyright and license information, please view the LICENSE
  9. * file that was distributed with this source code.
  10. */
  11. namespace Customize\Event;
  12. use Eccube\Request\Context;
  13. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  14. use Symfony\Component\HttpKernel\Event\RequestEvent;
  15. use Symfony\Component\HttpKernel\KernelEvents;
  16. use Symfony\Component\Routing\Exception\MethodNotAllowedException;
  17. use Symfony\Component\Routing\Exception\ResourceNotFoundException;
  18. use Symfony\Component\Routing\RouterInterface;
  19. use Twig\Environment;
  20. /**
  21.  * 指定したURLの場合にestimateかorderかのタイプをtwigにグローバル変数として渡す
  22.  */
  23. class EstimateOrOrderTypeEvent implements EventSubscriberInterface
  24. {
  25.     /**
  26.      * @var array
  27.      */
  28.     protected $business_types = [
  29.         'admin_estimate_new' => 'estimate',
  30.         'admin_estimate_edit' => 'estimate',
  31.         'admin_estimate_search_customer_html' => 'estimate',
  32.         'admin_estimate_search_customer_html_page' => 'estimate',
  33.         'admin_estimate_search_product' => 'estimate',
  34.         'admin_estimate_search_product_page' => 'estimate',
  35.         'admin_estimate_search_estimate_item_type' => 'estimate',
  36.         'admin_estimate_option_edit' => 'estimate',
  37.         'admin_estimate_search_material' => 'estimate',
  38.         'admin_estimate_search_option' => 'estimate',
  39.         'admin_order_new' => 'order',
  40.         'admin_order_edit' => 'order',
  41.         'admin_order_search_customer_html' => 'order',
  42.         'admin_order_search_customer_html_page' => 'order',
  43.         'admin_order_search_product' => 'order',
  44.         'admin_order_search_product_page' => 'order',
  45.         'admin_order_search_order_item_type' => 'order',
  46.         'admin_order_option_edit' => 'order',
  47.         'admin_order_search_material' => 'order',
  48.         'admin_order_search_option' => 'order',
  49.     ];
  50.     /**
  51.      * @var bool 初期化済かどうか.
  52.      */
  53.     protected $initialized false;
  54.     /**
  55.      * @var Environment
  56.      */
  57.     protected $twig;
  58.     /**
  59.      * @var Context
  60.      */
  61.     protected $requestContext;
  62.     /**
  63.      * @var RouterInterface
  64.      */
  65.     protected $router;
  66.     /**
  67.      * TwigInitializeListener constructor.
  68.      *
  69.      * @param Environment $twig
  70.      * @param Context $context
  71.      */
  72.     public function __construct(
  73.         Environment $twig,
  74.         Context $context,
  75.         RouterInterface $router
  76.     ) {
  77.         $this->twig $twig;
  78.         $this->requestContext $context;
  79.         $this->router $router;
  80.     }
  81.     public function onKernelRequest(RequestEvent $event)
  82.     {
  83.         if ($this->initialized) {
  84.             return;
  85.         }
  86.         // URLからルート名を取得
  87.         // 本番環境ではコンパイル済みルーターを使用するため、TabaCMS2等の動的ルートが
  88.         // 含まれず ResourceNotFoundException が発生する場合がある。
  89.         // このイベントは admin_estimate_* / admin_order_* のみを対象とするため、
  90.         // それ以外のルートで例外が発生しても無視して処理を続行する。
  91.         $pathInfo $event->getRequest()->getPathInfo();
  92.         try {
  93.             $routeParameters $this->router->match($pathInfo);
  94.             $routeName $routeParameters['_route'];
  95.             $business_type $this->business_types[$routeName] ?? null;
  96.             if ($business_type) {
  97.                 $this->twig->addGlobal('business_type'$business_type);
  98.             }
  99.         } catch (ResourceNotFoundException MethodNotAllowedException $e) {
  100.             // 動的ルート(/gallery 等)はコンパイル済みルーターで解決できないため無視する
  101.         }
  102.         $this->initialized true;
  103.     }
  104.     /**
  105.      * {@inheritdoc}
  106.      */
  107.     public static function getSubscribedEvents()
  108.     {
  109.         return [
  110.             KernelEvents::REQUEST => ['onKernelRequest'20],
  111.         ];
  112.     }
  113. }