app/Customize/EventListener/RequestListener.php line 44

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of EC-CUBE
  4.  *
  5.  * Copyright(c) EC-CUBE CO.,LTD. All Rights Reserved.
  6.  *
  7.  * http://www.ec-cube.co.jp/
  8.  *
  9.  * For the full copyright and license information, please view the LICENSE
  10.  * file that was distributed with this source code.
  11.  */
  12. namespace Customize\EventListener;
  13. use Customize\Service\LmHelper;
  14. use Eccube\Request\Context;
  15. use Symfony\Component\HttpFoundation\RedirectResponse;
  16. use Symfony\Component\HttpFoundation\Response;
  17. use Symfony\Component\HttpKernel\Event\RequestEvent;
  18. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  19. class RequestListener implements EventSubscriberInterface
  20. {
  21.     /**
  22.      * @var Context
  23.      */
  24.     private $requestContext;
  25.     /**
  26.      * @var LmHelper $lmHelper
  27.      */
  28.     protected $lmHelper;
  29.     public function __construct(
  30.         Context  $context,
  31.         LmHelper $lmHelper)
  32.     {
  33.         $this->requestContext $context;
  34.         $this->lmHelper $lmHelper;
  35.     }
  36.     public function onKernelRequest(RequestEvent $event)
  37.     {
  38.         if ($this->requestContext->isFront()) {
  39.             if (empty($event->getRequest()->getPathInfo())) {
  40.                 return;
  41.             }
  42.             $url preg_replace("/((\/)?\?.+)/"""$event->getRequest()->getPathInfo());
  43.             $redirectUrl $this->lmHelper->getRedirectUrl($url);
  44.             if (empty($redirectUrl)) {
  45.                 $redirectUrl $this->lmHelper->getRedirectUrl($event->getRequest()->getPathInfo());
  46.             }
  47.             if (!empty($redirectUrl)) {
  48.                 if (($params $event->getRequest()->query->all()) && ($query http_build_query($params))) {
  49.                     $redirectUrl "{$redirectUrl}?{$query}";
  50.                 }
  51.                 $event->setResponse(new RedirectResponse($redirectUrlResponse::HTTP_MOVED_PERMANENTLY));
  52.             }
  53.         }
  54.     }
  55.     /**
  56.      * Return the events to subscribe to.
  57.      *
  58.      * @return array
  59.      */
  60.     public static function getSubscribedEvents(): array
  61.     {
  62.         return [
  63.             RequestEvent::class => ['onKernelRequest'40],
  64.         ];
  65.     }
  66. }