vendor/symfony-cmf/routing-bundle/src/Doctrine/Orm/RouteProvider.php line 57

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Symfony CMF package.
  4.  *
  5.  * (c) Symfony CMF
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace Symfony\Cmf\Bundle\RoutingBundle\Doctrine\Orm;
  11. use Doctrine\DBAL\Exception\TableNotFoundException;
  12. use Doctrine\Persistence\ManagerRegistry;
  13. use Doctrine\Persistence\ObjectRepository;
  14. use Symfony\Cmf\Bundle\RoutingBundle\Doctrine\DoctrineProvider;
  15. use Symfony\Cmf\Component\Routing\Candidates\CandidatesInterface;
  16. use Symfony\Cmf\Component\Routing\RouteProviderInterface;
  17. use Symfony\Component\HttpFoundation\Request;
  18. use Symfony\Component\Routing\Exception\RouteNotFoundException;
  19. use Symfony\Component\Routing\RouteCollection;
  20. /**
  21.  * Provider loading routes from Doctrine.
  22.  *
  23.  * This is <strong>NOT</strong> not a doctrine repository but just the route
  24.  * provider for the NestedMatcher. (you could of course implement this
  25.  * interface in a repository class, if you need that)
  26.  *
  27.  * @author david.buchmann@liip.ch
  28.  */
  29. class RouteProvider extends DoctrineProvider implements RouteProviderInterface
  30. {
  31.     /**
  32.      * @var CandidatesInterface
  33.      */
  34.     private $candidatesStrategy;
  35.     public function __construct(ManagerRegistry $managerRegistryCandidatesInterface $candidatesStrategy$className)
  36.     {
  37.         parent::__construct($managerRegistry$className);
  38.         $this->candidatesStrategy $candidatesStrategy;
  39.     }
  40.     /**
  41.      * {@inheritdoc}
  42.      */
  43.     public function getRouteCollectionForRequest(Request $request)
  44.     {
  45.         $collection = new RouteCollection();
  46.         $candidates $this->candidatesStrategy->getCandidates($request);
  47.         if (=== \count($candidates)) {
  48.             return $collection;
  49.         }
  50.         $routes $this->getRouteRepository()->findByStaticPrefix($candidates, ['position' => 'ASC']);
  51.         /** @var $route Route */
  52.         foreach ($routes as $route) {
  53.             $collection->add($route->getName(), $route);
  54.         }
  55.         return $collection;
  56.     }
  57.     /**
  58.      * {@inheritdoc}
  59.      */
  60.     public function getRouteByName($name)
  61.     {
  62.         if (!$this->candidatesStrategy->isCandidate($name)) {
  63.             throw new RouteNotFoundException(sprintf('Route "%s" is not handled by this route provider'$name));
  64.         }
  65.         $route $this->getRouteRepository()->findOneBy(['name' => $name]);
  66.         if (!$route) {
  67.             throw new RouteNotFoundException("No route found for name '$name'");
  68.         }
  69.         return $route;
  70.     }
  71.     /**
  72.      * {@inheritdoc}
  73.      */
  74.     public function getRoutesByNames($names null)
  75.     {
  76.         if (null === $names) {
  77.             if (=== $this->routeCollectionLimit) {
  78.                 return [];
  79.             }
  80.             try {
  81.                 return $this->getRouteRepository()->findBy([], null$this->routeCollectionLimit ?: null);
  82.             } catch (TableNotFoundException $e) {
  83.                 return [];
  84.             }
  85.         }
  86.         $routes = [];
  87.         foreach ($names as $name) {
  88.             // TODO: if we do findByName with multivalue, we need to filter with isCandidate afterwards
  89.             try {
  90.                 $routes[] = $this->getRouteByName($name);
  91.             } catch (RouteNotFoundException $e) {
  92.                 // not found
  93.             }
  94.         }
  95.         return $routes;
  96.     }
  97.     /**
  98.      * @return ObjectRepository
  99.      */
  100.     protected function getRouteRepository()
  101.     {
  102.         return $this->getObjectManager()->getRepository($this->className);
  103.     }
  104. }