vendor/symfony-cmf/routing/src/NestedMatcher/NestedMatcher.php line 140

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\Component\Routing\NestedMatcher;
  11. use Symfony\Cmf\Component\Routing\RouteProviderInterface;
  12. use Symfony\Component\HttpFoundation\Request;
  13. use Symfony\Component\Routing\Exception\ResourceNotFoundException;
  14. use Symfony\Component\Routing\Matcher\RequestMatcherInterface;
  15. /**
  16.  * A more flexible approach to matching. The route collection to match against
  17.  * can be dynamically determined based on the request and users can inject
  18.  * their own filters or use a custom final matching strategy.
  19.  *
  20.  * The nested matcher splits matching into three configurable steps:
  21.  *
  22.  * 1) Get potential matches from a RouteProviderInterface
  23.  * 2) Apply any RouteFilterInterface to reduce the route collection
  24.  * 3) Have FinalMatcherInterface select the best match of the remaining routes
  25.  *
  26.  * @author Larry Garfield
  27.  * @author David Buchmann
  28.  */
  29. class NestedMatcher implements RequestMatcherInterface
  30. {
  31.     /**
  32.      * The route provider responsible for the first-pass match.
  33.      *
  34.      * @var RouteProviderInterface
  35.      */
  36.     protected $routeProvider;
  37.     /**
  38.      * The final matcher.
  39.      *
  40.      * @var FinalMatcherInterface
  41.      */
  42.     protected $finalMatcher;
  43.     /**
  44.      * An array of RouteFilterInterface objects.
  45.      *
  46.      * @var RouteFilterInterface[]
  47.      */
  48.     protected $filters = [];
  49.     /**
  50.      * Array of RouteFilterInterface objects, sorted.
  51.      *
  52.      * @var RouteFilterInterface[]
  53.      */
  54.     protected $sortedFilters = [];
  55.     /**
  56.      * Constructs a new NestedMatcher.
  57.      *
  58.      * @param RouteProviderInterface $provider The route provider this matcher
  59.      *                                         should use
  60.      * @param FinalMatcherInterface  $final    The Final Matcher to match the
  61.      *                                         routes
  62.      */
  63.     public function __construct(
  64.         RouteProviderInterface $provider null,
  65.         FinalMatcherInterface $final null
  66.     ) {
  67.         if (null !== $provider) {
  68.             $this->setRouteProvider($provider);
  69.         }
  70.         if (null !== $final) {
  71.             $this->setFinalMatcher($final);
  72.         }
  73.     }
  74.     /**
  75.      * Sets the route provider for the matching plan.
  76.      *
  77.      * @param RouteProviderInterface $provider A source of routes
  78.      *
  79.      * @return NestedMatcher this object to have a fluent interface
  80.      */
  81.     public function setRouteProvider(RouteProviderInterface $provider)
  82.     {
  83.         $this->routeProvider $provider;
  84.         return $this;
  85.     }
  86.     /**
  87.      * Adds a partial matcher to the matching plan.
  88.      *
  89.      * Partial matchers will be run in the order in which they are added.
  90.      *
  91.      * @param int $priority (optional) The priority of the
  92.      *                      filter. Higher number filters will
  93.      *                      be used first. Defaults to 0
  94.      *
  95.      * @return NestedMatcher this object to have a fluent interface
  96.      */
  97.     public function addRouteFilter(RouteFilterInterface $filter$priority 0)
  98.     {
  99.         if (empty($this->filters[$priority])) {
  100.             $this->filters[$priority] = [];
  101.         }
  102.         $this->filters[$priority][] = $filter;
  103.         $this->sortedFilters = [];
  104.         return $this;
  105.     }
  106.     /**
  107.      * Sets the final matcher for the matching plan.
  108.      *
  109.      * @param FinalMatcherInterface $final The final matcher that will have to
  110.      *                                     pick the route that will be used
  111.      *
  112.      * @return NestedMatcher this object to have a fluent interface
  113.      */
  114.     public function setFinalMatcher(FinalMatcherInterface $final)
  115.     {
  116.         $this->finalMatcher $final;
  117.         return $this;
  118.     }
  119.     /**
  120.      * {@inheritdoc}
  121.      */
  122.     public function matchRequest(Request $request)
  123.     {
  124.         $collection $this->routeProvider->getRouteCollectionForRequest($request);
  125.         if (!count($collection)) {
  126.             throw new ResourceNotFoundException();
  127.         }
  128.         // Route filters are expected to throw an exception themselves if they
  129.         // end up filtering the list down to 0.
  130.         foreach ($this->getRouteFilters() as $filter) {
  131.             $collection $filter->filter($collection$request);
  132.         }
  133.         return $this->finalMatcher->finalMatch($collection$request);
  134.     }
  135.     /**
  136.      * Sorts the filters and flattens them.
  137.      *
  138.      * @return RouteFilterInterface[] the filters ordered by priority
  139.      */
  140.     public function getRouteFilters()
  141.     {
  142.         if (=== count($this->sortedFilters)) {
  143.             $this->sortedFilters $this->sortFilters();
  144.         }
  145.         return $this->sortedFilters;
  146.     }
  147.     /**
  148.      * Sort filters by priority.
  149.      *
  150.      * The highest priority number is the highest priority (reverse sorting).
  151.      *
  152.      * @return RouteFilterInterface[] the sorted filters
  153.      */
  154.     protected function sortFilters()
  155.     {
  156.         if (=== count($this->filters)) {
  157.             return [];
  158.         }
  159.         krsort($this->filters);
  160.         return call_user_func_array('array_merge'$this->filters);
  161.     }
  162. }