<?php
namespace Plugin\ApgFormBuilder42;
use Doctrine\ORM\EntityManagerInterface;
use Eccube\Common\EccubeConfig;
use Eccube\Request\Context;
use Plugin\ApgFormBuilder42\Entity\ApgForm;
use Plugin\ApgFormBuilder42\Repository\FormRepository;
use Psr\Container\ContainerInterface;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\EventListener\RouterListener;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\Routing\Matcher\UrlMatcher;
use Symfony\Component\Routing\RequestContext;
use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\RouteCollection;
use Symfony\Component\Routing\RouterInterface;
class RoutingListener extends RouterListener
{
/** @var Context */
private $context;
/** @var ContainerInterface */
private $requestStack;
/** @var EntityManagerInterface */
private $em;
/** @var RouterInterface */
private $router;
/** @var RouteCollection */
private $routeCollection;
/** @var \Twig\Environment */
private $twig;
/**
* コンストラクタ
*
* @param Context $context
* @param \Twig\Environment $twig
* @param RequestStack $requestStack
* @param EccubeConfig $eccubeConfig
* @param RouterInterface $router
* @param EntityManagerInterface $em
*/
public function __construct(
Context $context,
\Twig\Environment $twig,
RequestStack $requestStack,
EccubeConfig $eccubeConfig,
RouterInterface $router,
EntityManagerInterface $em
)
{
$this->context = $context;
$this->requestStack = $requestStack;
$this->em = $em;
$this->router = $router;
$this->routeCollection = $this->router->getRouteCollection();
$this->twig = $twig;
$this->initialize();
$matcher = new UrlMatcher($this->routeCollection, new RequestContext());
parent::__construct($matcher, $requestStack);
}
private function initialize()
{
if ($this->context->isFront()) {
$request = $this->requestStack->getCurrentRequest();
$pathInfo = $request->getPathInfo();
$path = ltrim($pathInfo, "/");
/** @var FormRepository $formRepository */
$formRepository = $this->em->getRepository(ApgForm::class);
/** @var ApgForm $ApgForm */
$ApgForm = $formRepository->findOneBy([
'slug' => $path,
'del_flg' => 0,
]);
if (!empty($ApgForm)) {
$this->routeCollection->add('apg_form_builder_form_' . $ApgForm->getId(),
new Route(
'/' . $path,
[
'_controller' => "Plugin\ApgFormBuilder42\Controller\FormController::input",
'id' => $ApgForm->getId(),
]
)
);
} else {
$pattern = '|apg_form/([\d]+)|';
$matches = [];
if (preg_match($pattern, $path, $matches)) {
$formId = $matches[1];
if (!empty($formId)) {
/** @var ApgForm $ApgFormById */
$ApgFormById = $formRepository->findOneBy([
'id' => $formId
]);
// 適当なIDで指定された場合でかつslugが指定されている場合は、404
if (!empty($ApgFormById) && !empty($ApgFormById->getSlug())) {
$exception = new NotFoundHttpException();
$infoMess = 'ページがみつかりません。';
$title = trans('exception.error_title_not_found');
$message = trans('exception.error_message_not_found');
$file = 'error.twig';
$content = $this->twig->render($file, [
'error_title' => $title,
'error_message' => $message,
]);
log_info($infoMess, [
$exception->getMessage(),
$exception->getFile(),
$exception->getLine(),
]);
Response::create($content, 404)->send();
exit;
}
}
}
}
}
}
}