app/Plugin/ApgFormBuilder42/RoutingListener.php line 48

Open in your IDE?
  1. <?php
  2. namespace Plugin\ApgFormBuilder42;
  3. use Doctrine\ORM\EntityManagerInterface;
  4. use Eccube\Common\EccubeConfig;
  5. use Eccube\Request\Context;
  6. use Plugin\ApgFormBuilder42\Entity\ApgForm;
  7. use Plugin\ApgFormBuilder42\Repository\FormRepository;
  8. use Psr\Container\ContainerInterface;
  9. use Symfony\Component\HttpFoundation\RequestStack;
  10. use Symfony\Component\HttpFoundation\Response;
  11. use Symfony\Component\HttpKernel\EventListener\RouterListener;
  12. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  13. use Symfony\Component\Routing\Matcher\UrlMatcher;
  14. use Symfony\Component\Routing\RequestContext;
  15. use Symfony\Component\Routing\Route;
  16. use Symfony\Component\Routing\RouteCollection;
  17. use Symfony\Component\Routing\RouterInterface;
  18. class RoutingListener extends RouterListener
  19. {
  20.     /** @var Context */
  21.     private $context;
  22.     /** @var ContainerInterface */
  23.     private $requestStack;
  24.     /** @var EntityManagerInterface */
  25.     private $em;
  26.     /** @var RouterInterface */
  27.     private $router;
  28.     /** @var RouteCollection */
  29.     private $routeCollection;
  30.     /** @var \Twig\Environment */
  31.     private $twig;
  32.     /**
  33.      * コンストラクタ
  34.      *
  35.      * @param Context $context
  36.      * @param \Twig\Environment $twig
  37.      * @param RequestStack $requestStack
  38.      * @param EccubeConfig $eccubeConfig
  39.      * @param RouterInterface $router
  40.      * @param EntityManagerInterface $em
  41.      */
  42.     public function __construct(
  43.         Context                $context,
  44.         \Twig\Environment      $twig,
  45.         RequestStack           $requestStack,
  46.         EccubeConfig           $eccubeConfig,
  47.         RouterInterface        $router,
  48.         EntityManagerInterface $em
  49.     )
  50.     {
  51.         $this->context $context;
  52.         $this->requestStack $requestStack;
  53.         $this->em $em;
  54.         $this->router $router;
  55.         $this->routeCollection $this->router->getRouteCollection();
  56.         $this->twig $twig;
  57.         $this->initialize();
  58.         $matcher = new UrlMatcher($this->routeCollection, new RequestContext());
  59.         parent::__construct($matcher$requestStack);
  60.     }
  61.     private function initialize()
  62.     {
  63.         if ($this->context->isFront()) {
  64.             $request $this->requestStack->getCurrentRequest();
  65.             $pathInfo $request->getPathInfo();
  66.             $path ltrim($pathInfo"/");
  67.             /** @var FormRepository $formRepository */
  68.             $formRepository $this->em->getRepository(ApgForm::class);
  69.             /** @var ApgForm $ApgForm */
  70.             $ApgForm $formRepository->findOneBy([
  71.                 'slug' => $path,
  72.                 'del_flg' => 0,
  73.             ]);
  74.             if (!empty($ApgForm)) {
  75.                 $this->routeCollection->add('apg_form_builder_form_' $ApgForm->getId(),
  76.                     new Route(
  77.                         '/' $path,
  78.                         [
  79.                             '_controller' => "Plugin\ApgFormBuilder42\Controller\FormController::input",
  80.                             'id' => $ApgForm->getId(),
  81.                         ]
  82.                     )
  83.                 );
  84.             } else {
  85.                 $pattern '|apg_form/([\d]+)|';
  86.                 $matches = [];
  87.                 if (preg_match($pattern$path$matches)) {
  88.                     $formId $matches[1];
  89.                     if (!empty($formId)) {
  90.                         /** @var ApgForm $ApgFormById */
  91.                         $ApgFormById $formRepository->findOneBy([
  92.                             'id' => $formId
  93.                         ]);
  94.                         // 適当なIDで指定された場合でかつslugが指定されている場合は、404
  95.                         if (!empty($ApgFormById) && !empty($ApgFormById->getSlug())) {
  96.                             $exception = new NotFoundHttpException();
  97.                             $infoMess 'ページがみつかりません。';
  98.                             $title trans('exception.error_title_not_found');
  99.                             $message trans('exception.error_message_not_found');
  100.                             $file 'error.twig';
  101.                             $content $this->twig->render($file, [
  102.                                 'error_title' => $title,
  103.                                 'error_message' => $message,
  104.                             ]);
  105.                             log_info($infoMess, [
  106.                                 $exception->getMessage(),
  107.                                 $exception->getFile(),
  108.                                 $exception->getLine(),
  109.                             ]);
  110.                             Response::create($content404)->send();
  111.                             exit;
  112.                         }
  113.                     }
  114.                 }
  115.             }
  116.         }
  117.     }
  118. }