app/Plugin/SimpleMaintenance/SimpleMaintenanceController.php line 44

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