<?php
/*
* Plugin Name : ProductOption
*
* Copyright (C) BraTech Co., Ltd. All Rights Reserved.
* http://www.bratech.co.jp/
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Customize\Event;
use Eccube\Request\Context;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\Routing\Exception\MethodNotAllowedException;
use Symfony\Component\Routing\Exception\ResourceNotFoundException;
use Symfony\Component\Routing\RouterInterface;
use Twig\Environment;
/**
* 指定したURLの場合にestimateかorderかのタイプをtwigにグローバル変数として渡す
*/
class EstimateOrOrderTypeEvent implements EventSubscriberInterface
{
/**
* @var array
*/
protected $business_types = [
'admin_estimate_new' => 'estimate',
'admin_estimate_edit' => 'estimate',
'admin_estimate_search_customer_html' => 'estimate',
'admin_estimate_search_customer_html_page' => 'estimate',
'admin_estimate_search_product' => 'estimate',
'admin_estimate_search_product_page' => 'estimate',
'admin_estimate_search_estimate_item_type' => 'estimate',
'admin_estimate_option_edit' => 'estimate',
'admin_estimate_search_material' => 'estimate',
'admin_estimate_search_option' => 'estimate',
'admin_order_new' => 'order',
'admin_order_edit' => 'order',
'admin_order_search_customer_html' => 'order',
'admin_order_search_customer_html_page' => 'order',
'admin_order_search_product' => 'order',
'admin_order_search_product_page' => 'order',
'admin_order_search_order_item_type' => 'order',
'admin_order_option_edit' => 'order',
'admin_order_search_material' => 'order',
'admin_order_search_option' => 'order',
];
/**
* @var bool 初期化済かどうか.
*/
protected $initialized = false;
/**
* @var Environment
*/
protected $twig;
/**
* @var Context
*/
protected $requestContext;
/**
* @var RouterInterface
*/
protected $router;
/**
* TwigInitializeListener constructor.
*
* @param Environment $twig
* @param Context $context
*/
public function __construct(
Environment $twig,
Context $context,
RouterInterface $router
) {
$this->twig = $twig;
$this->requestContext = $context;
$this->router = $router;
}
public function onKernelRequest(RequestEvent $event)
{
if ($this->initialized) {
return;
}
// URLからルート名を取得
// 本番環境ではコンパイル済みルーターを使用するため、TabaCMS2等の動的ルートが
// 含まれず ResourceNotFoundException が発生する場合がある。
// このイベントは admin_estimate_* / admin_order_* のみを対象とするため、
// それ以外のルートで例外が発生しても無視して処理を続行する。
$pathInfo = $event->getRequest()->getPathInfo();
try {
$routeParameters = $this->router->match($pathInfo);
$routeName = $routeParameters['_route'];
$business_type = $this->business_types[$routeName] ?? null;
if ($business_type) {
$this->twig->addGlobal('business_type', $business_type);
}
} catch (ResourceNotFoundException | MethodNotAllowedException $e) {
// 動的ルート(/gallery 等)はコンパイル済みルーターで解決できないため無視する
}
$this->initialized = true;
}
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents()
{
return [
KernelEvents::REQUEST => ['onKernelRequest', 20],
];
}
}