X-Git-Url: http://aleph1.co.uk/gitweb/?a=blobdiff_plain;f=vendor%2Fsymfony-cmf%2Frouting%2FNestedMatcher%2FNestedMatcher.php;fp=vendor%2Fsymfony-cmf%2Frouting%2FNestedMatcher%2FNestedMatcher.php;h=cc68463d54a94de60684d2ee90e22db03dd0a695;hb=a2bd1bf0c2c1f1a17d188f4dc0726a45494cefae;hp=0000000000000000000000000000000000000000;hpb=57c063afa3f66b07c4bbddc2d6129a96d90f0aad;p=yaffs-website diff --git a/vendor/symfony-cmf/routing/NestedMatcher/NestedMatcher.php b/vendor/symfony-cmf/routing/NestedMatcher/NestedMatcher.php new file mode 100644 index 000000000..cc68463d5 --- /dev/null +++ b/vendor/symfony-cmf/routing/NestedMatcher/NestedMatcher.php @@ -0,0 +1,189 @@ +setRouteProvider($provider); + } + if (null !== $final) { + $this->setFinalMatcher($final); + } + } + + /** + * Sets the route provider for the matching plan. + * + * @param RouteProviderInterface $provider A source of routes. + * + * @return NestedMatcher this object to have a fluent interface + */ + public function setRouteProvider(RouteProviderInterface $provider) + { + $this->routeProvider = $provider; + + return $this; + } + + /** + * Adds a partial matcher to the matching plan. + * + * Partial matchers will be run in the order in which they are added. + * + * @param RouteFilterInterface $filter + * @param int $priority (optional) The priority of the + * filter. Higher number filters will + * be used first. Defaults to 0. + * + * @return NestedMatcher this object to have a fluent interface + */ + public function addRouteFilter(RouteFilterInterface $filter, $priority = 0) + { + if (empty($this->filters[$priority])) { + $this->filters[$priority] = array(); + } + + $this->filters[$priority][] = $filter; + $this->sortedFilters = array(); + + return $this; + } + + /** + * Sets the final matcher for the matching plan. + * + * @param FinalMatcherInterface $final The final matcher that will have to + * pick the route that will be used. + * + * @return NestedMatcher this object to have a fluent interface + */ + public function setFinalMatcher(FinalMatcherInterface $final) + { + $this->finalMatcher = $final; + + return $this; + } + + /** + * {@inheritdoc} + */ + public function matchRequest(Request $request) + { + $collection = $this->routeProvider->getRouteCollectionForRequest($request); + if (!count($collection)) { + throw new ResourceNotFoundException(); + } + + // Route filters are expected to throw an exception themselves if they + // end up filtering the list down to 0. + foreach ($this->getRouteFilters() as $filter) { + $collection = $filter->filter($collection, $request); + } + + $attributes = $this->finalMatcher->finalMatch($collection, $request); + + return $attributes; + } + + /** + * Sorts the filters and flattens them. + * + * @return RouteFilterInterface[] the filters ordered by priority + */ + public function getRouteFilters() + { + if (empty($this->sortedFilters)) { + $this->sortedFilters = $this->sortFilters(); + } + + return $this->sortedFilters; + } + + /** + * Sort filters by priority. + * + * The highest priority number is the highest priority (reverse sorting). + * + * @return RouteFilterInterface[] the sorted filters + */ + protected function sortFilters() + { + $sortedFilters = array(); + krsort($this->filters); + + foreach ($this->filters as $filters) { + $sortedFilters = array_merge($sortedFilters, $filters); + } + + return $sortedFilters; + } +}