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

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