app/Plugin/TabaCMS2/EventListener/DynamicRoutingListener.php line 73

Open in your IDE?
  1. <?php
  2. /*
  3.  * Copyright (C) SPREAD WORKS Inc. All Rights Reserved.
  4.  *
  5.  * For the full copyright and license information, please view the LICENSE
  6.  * file that was distributed with this source code.
  7.  */
  8. namespace Plugin\TabaCMS2\EventListener;
  9. use Doctrine\ORM\EntityManagerInterface;
  10. use Plugin\TabaCMS2\Common\Constants;
  11. use Plugin\TabaCMS2\Common\UserConfig;
  12. use Plugin\TabaCMS2\Repository\TypeRepository;
  13. use Plugin\TabaCMS2\Repository\PostRepository;
  14. use Plugin\TabaCMS2\Repository\CategoryRepository;
  15. use Plugin\TabaCMS2\Entity\Type;
  16. use Plugin\TabaCMS2\Entity\Post;
  17. use Plugin\TabaCMS2\Entity\Category;
  18. use Psr\Container\ContainerInterface;
  19. use Symfony\Component\Routing\RouterInterface;
  20. use Symfony\Component\Routing\RouteCollection;
  21. use Symfony\Component\Routing\Route;
  22. use Symfony\Component\HttpKernel\Event\RequestEvent;
  23. use Symfony\Component\HttpKernel\EventListener\RouterListener;
  24. use Symfony\Component\Routing\Matcher\UrlMatcher;
  25. use Symfony\Component\HttpFoundation\RequestStack;
  26. use Symfony\Component\Routing\RequestContext;
  27. class DynamicRoutingListener extends RouterListener
  28. {
  29.     /**
  30.      * @var ContainerInterface
  31.      */
  32.     private $container;
  33.     /**
  34.      * @var RouterInterface
  35.      */
  36.     private $router;
  37.     /**
  38.      * @var RouteCollection
  39.      */
  40.     private $routes;
  41.     /**
  42.      * @var TypeRepository
  43.      */
  44.     private $typeRepo;
  45.     /**
  46.      * @var PostRepository
  47.      */
  48.     private $postRepo;
  49.     /**
  50.      * @var CategoryRepository
  51.      */
  52.     private $categoryRepo;
  53.     /**
  54.      * @var EntityManagerInterface
  55.      */
  56.     private $em;
  57.     //public function __construct(ContainerInterface $container, RequestStack $requestStack)
  58.     /**
  59.      * コンストラクタ
  60.      *
  61.      * @param ContainerInterface $container
  62.      * @param RequestStack $requestStack
  63.      */
  64.     public function __construct(EntityManagerInterface $emRouterInterface $routerRequestStack $requestStack)
  65.     {
  66.         $this->em $em;
  67.         $this->router $router;
  68.         $this->routes $router->getRouteCollection();
  69.         $this->typeRepo $this->em->getRepository(Type::class);
  70.         $this->categoryRepo $this->em->getRepository(Category::class);
  71.         $this->postRepo $this->em->getRepository(Post::class);
  72.         $this->load();
  73.         $requestContext = new RequestContext();
  74.         if ($requestStack->getCurrentRequest()) $requestContext->fromRequest($requestStack->getCurrentRequest());
  75.         parent::__construct(new UrlMatcher($this->routes,$requestContext),$requestStack);
  76.     }
  77.     /**
  78.      * ルーティングの設定
  79.      */
  80.     private function load()
  81.     {
  82.         // APIモードでの動作はルーティング設定を行わない
  83.         $mode UserConfig::getInstance()->get("mode");
  84.         if ($mode == 'api') {
  85.             return;
  86.         }
  87.         $front_uri_prefix UserConfig::getInstance()->get("front_uri_prefix");
  88.         // 投稿タイプ毎に動的にルーティングを設定します。
  89.         $type_list $this->typeRepo->findAll();
  90.         foreach ($type_list as $type) {
  91.             // フロント
  92.             if ($type->getPublicDiv() == Type::PUBLIC_DIV_PUBLIC) {
  93.                 // 投稿リスト
  94.                 $this->routes->add(Constants::FRONT_BIND_PREFIX '_list_' $type->getTypeId(),
  95.                     new Route(
  96.                         $front_uri_prefix '/' $type->getDataKey(),
  97.                         ['_controller' => Constants::FRONT_CONTROLLER "::prepare"]
  98.                     )
  99.                 );
  100.                 // カテゴリー
  101.                 $this->routes->add(Constants::FRONT_BIND_PREFIX '_category_' $type->getTypeId(),
  102.                     new Route(
  103.                         $front_uri_prefix '/' $type->getDataKey() . '/category/{category_data_key}',
  104.                         ['_controller' => Constants::FRONT_CONTROLLER "::prepare"]
  105.                     )
  106.                 );
  107.                 // タグ
  108.                 $this->routes->add(Constants::FRONT_BIND_PREFIX '_tag_' $type->getTypeId(),
  109.                     new Route(
  110.                         $front_uri_prefix '/' $type->getDataKey() . '/tag/{tag_data_key}',
  111.                         ['_controller' => Constants::FRONT_CONTROLLER "::prepare"]
  112.                     )
  113.                 );
  114.                 // アーカイブ
  115.                 $this->routes->add(Constants::FRONT_BIND_PREFIX '_archive_' $type->getTypeId(),
  116.                     new Route(
  117.                         $front_uri_prefix '/' $type->getDataKey() . '/archive}',
  118.                         ['_controller' => Constants::FRONT_CONTROLLER "::prepare"]
  119.                     )
  120.                 );
  121.                 // 投稿データ
  122.                 $this->routes->add(Constants::FRONT_BIND_PREFIX '_post_' $type->getTypeId(),
  123.                     new Route(
  124.                         $front_uri_prefix '/' $type->getDataKey() . '/{data_key}',
  125.                         ['_controller' => Constants::FRONT_CONTROLLER "::prepare"],
  126.                         ['data_key' => '.+']
  127.                     )
  128.                 );
  129.                 // ルーティングの上書き設定されている投稿リストを取得します。
  130.                 $post_list $this->postRepo->getList(array(
  131.                     "type_id" => $type->getTypeId(),
  132.                     "is_overwrite_route" => true,
  133.                 ));
  134.                 foreach ($post_list as $post) {
  135.                     if ($post->getOverwriteRoute()) {
  136.                         $this->routes->add($post->getOverwriteRoute(),
  137.                             new Route(
  138.                                 $front_uri_prefix '/' $type->getDataKey() . '/' $post->getDataKey(),
  139.                                 ['_controller' => Constants::FRONT_CONTROLLER "::prepare"]
  140.                             )
  141.                         );
  142.                     }
  143.                 }
  144.             }
  145.         }
  146.         // CSS、JS、画像など読み込み
  147.         $this->routes->add(Constants::FRONT_BIND_PREFIX '_assets',new Route(Constants::FRONT_URI_PREFIX '/assets/{file}',['_controller' => Constants::FRONT_CONTROLLER "::assets"],['file' => '[a-zA-Z0-9\/\-\_\.\s]+']));
  148.     }
  149.     /**
  150.      * {@inheritDoc}
  151.      * @see \Symfony\Component\HttpKernel\EventListener\RouterListener::onKernelRequest()
  152.      */
  153.     public function onKernelRequest(RequestEvent $event): void
  154.     {
  155.         try {
  156.             parent::onKernelRequest($event);
  157.         }
  158.         // 404のハンドリングをEC-CUBE側のロジックに任すため、エラーを握りつぶす
  159.         catch (\Symfony\Component\HttpKernel\Exception\NotFoundHttpException $e) {
  160.         }
  161.     }
  162. }