<?php
/*
* This file is part of EC-CUBE
*
* Copyright(c) EC-CUBE CO.,LTD. All Rights Reserved.
*
* http://www.ec-cube.co.jp/
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Customize\EventListener;
use Customize\Service\LmHelper;
use Eccube\Request\Context;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class RequestListener implements EventSubscriberInterface
{
/**
* @var Context
*/
private $requestContext;
/**
* @var LmHelper $lmHelper
*/
protected $lmHelper;
public function __construct(
Context $context,
LmHelper $lmHelper)
{
$this->requestContext = $context;
$this->lmHelper = $lmHelper;
}
public function onKernelRequest(RequestEvent $event)
{
if ($this->requestContext->isFront()) {
if (empty($event->getRequest()->getPathInfo())) {
return;
}
$url = preg_replace("/((\/)?\?.+)/", "", $event->getRequest()->getPathInfo());
$redirectUrl = $this->lmHelper->getRedirectUrl($url);
if (empty($redirectUrl)) {
$redirectUrl = $this->lmHelper->getRedirectUrl($event->getRequest()->getPathInfo());
}
if (!empty($redirectUrl)) {
if (($params = $event->getRequest()->query->all()) && ($query = http_build_query($params))) {
$redirectUrl = "{$redirectUrl}?{$query}";
}
$event->setResponse(new RedirectResponse($redirectUrl, Response::HTTP_MOVED_PERMANENTLY));
}
}
}
/**
* Return the events to subscribe to.
*
* @return array
*/
public static function getSubscribedEvents(): array
{
return [
RequestEvent::class => ['onKernelRequest', 40],
];
}
}