app/Plugin/SimpleMaintenance/SimpleMaintenanceController.php line 43

Open in your IDE?
  1. <?php
  2. /**
  3.  * Created by SYSTEM_KD
  4.  * Date: 2018/08/15
  5.  */
  6. namespace Plugin\SimpleMaintenance;
  7. use Eccube\Common\EccubeConfig;
  8. use Plugin\SimpleMaintenance\Entity\SimpleMConfig;
  9. use Plugin\SimpleMaintenance\Repository\SimpleMConfigRepository;
  10. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  11. use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
  12. use Symfony\Component\HttpKernel\KernelEvents;
  13. /**
  14.  * Class SimpleMaintenanceController
  15.  * @package Plugin\SimpleMaintenance\Controller
  16.  */
  17. class SimpleMaintenanceController implements EventSubscriberInterface
  18. {
  19.     /** @var EccubeConfig */
  20.     private $eccubeService;
  21.     /** @var SimpleMConfigRepository */
  22.     private $configRepository;
  23.     public function __construct(
  24.         EccubeConfig            $eccubeConfig,
  25.         SimpleMConfigRepository $configRepository
  26.     )
  27.     {
  28.         $this->eccubeService $eccubeConfig;
  29.         $this->configRepository $configRepository;
  30.     }
  31.     /**
  32.      * レスポンスフック
  33.      *
  34.      * @param FilterResponseEvent $event
  35.      */
  36.     public function onKernelResponse(FilterResponseEvent $event)
  37.     {
  38.         if (!$event->isMasterRequest()) {
  39.             return;
  40.         }
  41.         $request $event->getRequest();
  42.         if ($request->isXmlHttpRequest()) {
  43.             return;
  44.         }
  45.         $path $request->getPathInfo();
  46.         $adminRoot $this->eccubeService->get('eccube_admin_route');
  47.         if (strpos($path'/' trim($adminRoot'/')) === 0) {
  48.             // admin_route はメンテナンスページから除外
  49.             return;
  50.         }
  51.         // 設定情報取得
  52.         /** @var SimpleMConfig $config */
  53.         $config $this->configRepository->get();
  54.         if ($config) {
  55.             if ($config->isMenteMode()) {
  56.                 $session $request->getSession();
  57.                 $is_admin $session->has('_security_admin');
  58.                 if ($config->isAdminCloseFlg() && $is_admin) {
  59.                     // 管理者でかつ、管理者での閲覧有効時の場合は除外
  60.                     return;
  61.                 }
  62.                 // メンテナンスモード有効
  63.                 $html $config->getPageHtml();
  64.                 $response $event->getResponse();
  65.                 $response->setContent($html);
  66.             }
  67.         }
  68.     }
  69.     /**
  70.      * Returns an array of event names this subscriber wants to listen to.
  71.      *
  72.      * The array keys are event names and the value can be:
  73.      *
  74.      *  * The method name to call (priority defaults to 0)
  75.      *  * An array composed of the method name to call and the priority
  76.      *  * An array of arrays composed of the method names to call and respective
  77.      *    priorities, or 0 if unset
  78.      *
  79.      * For instance:
  80.      *
  81.      *  * array('eventName' => 'methodName')
  82.      *  * array('eventName' => array('methodName', $priority))
  83.      *  * array('eventName' => array(array('methodName1', $priority), array('methodName2')))
  84.      *
  85.      * @return array The event names to listen to
  86.      */
  87.     public static function getSubscribedEvents()
  88.     {
  89.         return [
  90.             KernelEvents::RESPONSE => ['onKernelResponse'0],
  91.         ];
  92.     }
  93. }