comparison core/lib/Drupal/Core/Routing/Router.php @ 14:1fec387a4317

Update Drupal core to 8.5.2 via Composer
author Chris Cannam
date Mon, 23 Apr 2018 09:46:53 +0100
parents 4c8ae668cc8c
children 129ea1e6d783
comparison
equal deleted inserted replaced
13:5fb285c0d0e3 14:1fec387a4317
1 <?php 1 <?php
2 2
3 namespace Drupal\Core\Routing; 3 namespace Drupal\Core\Routing;
4 4
5 use Drupal\Core\Path\CurrentPathStack; 5 use Drupal\Core\Path\CurrentPathStack;
6 use Symfony\Cmf\Component\Routing\Enhancer\RouteEnhancerInterface as BaseRouteEnhancerInterface; 6 use Drupal\Core\Routing\Enhancer\RouteEnhancerInterface;
7 use Symfony\Cmf\Component\Routing\LazyRouteCollection; 7 use Symfony\Cmf\Component\Routing\LazyRouteCollection;
8 use Symfony\Cmf\Component\Routing\NestedMatcher\RouteFilterInterface as BaseRouteFilterInterface; 8 use Symfony\Cmf\Component\Routing\RouteObjectInterface;
9 use Symfony\Cmf\Component\Routing\RouteProviderInterface as BaseRouteProviderInterface; 9 use Symfony\Cmf\Component\Routing\RouteProviderInterface as BaseRouteProviderInterface;
10 use Symfony\Component\HttpFoundation\Request; 10 use Symfony\Component\HttpFoundation\Request;
11 use Symfony\Component\Routing\Exception\MethodNotAllowedException; 11 use Symfony\Component\Routing\Exception\MethodNotAllowedException;
12 use Symfony\Component\Routing\Exception\ResourceNotFoundException; 12 use Symfony\Component\Routing\Exception\ResourceNotFoundException;
13 use Symfony\Component\Routing\Generator\UrlGeneratorInterface as BaseUrlGeneratorInterface; 13 use Symfony\Component\Routing\Generator\UrlGeneratorInterface as BaseUrlGeneratorInterface;
51 protected $routeProvider; 51 protected $routeProvider;
52 52
53 /** 53 /**
54 * The list of available enhancers. 54 * The list of available enhancers.
55 * 55 *
56 * @var \Symfony\Cmf\Component\Routing\Enhancer\RouteEnhancerInterface[] 56 * @var \Drupal\Core\Routing\EnhancerInterface[]
57 */ 57 */
58 protected $enhancers = []; 58 protected $enhancers = [];
59 59
60 /** 60 /**
61 * Cached sorted list of enhancers.
62 *
63 * @var \Symfony\Cmf\Component\Routing\Enhancer\RouteEnhancerInterface[]
64 */
65 protected $sortedEnhancers;
66
67 /**
68 * The list of available route filters. 61 * The list of available route filters.
69 * 62 *
70 * @var \Symfony\Cmf\Component\Routing\NestedMatcher\RouteFilterInterface[] 63 * @var \Drupal\Core\Routing\FilterInterface[]
71 */ 64 */
72 protected $filters = []; 65 protected $filters = [];
73
74 /**
75 * Cached sorted list route filters.
76 *
77 * @var \Symfony\Cmf\Component\Routing\NestedMatcher\RouteFilterInterface[]
78 */
79 protected $sortedFilters;
80 66
81 /** 67 /**
82 * The URL generator. 68 * The URL generator.
83 * 69 *
84 * @var \Symfony\Component\Routing\Generator\UrlGeneratorInterface 70 * @var \Symfony\Component\Routing\Generator\UrlGeneratorInterface
100 $this->routeProvider = $route_provider; 86 $this->routeProvider = $route_provider;
101 $this->urlGenerator = $url_generator; 87 $this->urlGenerator = $url_generator;
102 } 88 }
103 89
104 /** 90 /**
105 * Adds a route enhancer to the list of used route enhancers. 91 * Adds a route filter.
106 * 92 *
107 * @param \Symfony\Cmf\Component\Routing\Enhancer\RouteEnhancerInterface $route_enhancer 93 * @param \Drupal\Core\Routing\FilterInterface $route_filter
108 * A route enhancer. 94 * The route filter.
109 * @param int $priority 95 */
110 * (optional) The priority of the enhancer. Higher number enhancers will be 96 public function addRouteFilter(FilterInterface $route_filter) {
111 * used first. 97 $this->filters[] = $route_filter;
112 * 98 }
113 * @return $this 99
114 */ 100 /**
115 public function addRouteEnhancer(BaseRouteEnhancerInterface $route_enhancer, $priority = 0) { 101 * Adds a route enhancer.
116 $this->enhancers[$priority][] = $route_enhancer; 102 *
117 return $this; 103 * @param \Drupal\Core\Routing\EnhancerInterface $route_enhancer
118 } 104 * The route enhancer.
119 105 */
120 /** 106 public function addRouteEnhancer(EnhancerInterface $route_enhancer) {
121 * Adds a route filter to the list of used route filters. 107 $this->enhancers[] = $route_enhancer;
122 *
123 * @param \Symfony\Cmf\Component\Routing\NestedMatcher\RouteFilterInterface $route_filter
124 * A route filter.
125 * @param int $priority
126 * (optional) The priority of the filter. Higher number filters will be used
127 * first.
128 *
129 * @return $this
130 */
131 public function addRouteFilter(BaseRouteFilterInterface $route_filter, $priority = 0) {
132 $this->filters[$priority][] = $route_filter;
133
134 return $this;
135 } 108 }
136 109
137 /** 110 /**
138 * {@inheritdoc} 111 * {@inheritdoc}
139 */ 112 */
146 /** 119 /**
147 * {@inheritdoc} 120 * {@inheritdoc}
148 */ 121 */
149 public function matchRequest(Request $request) { 122 public function matchRequest(Request $request) {
150 $collection = $this->getInitialRouteCollection($request); 123 $collection = $this->getInitialRouteCollection($request);
124 if ($collection->count() === 0) {
125 throw new ResourceNotFoundException(sprintf('No routes found for "%s".', $this->currentPath->getPath()));
126 }
151 $collection = $this->applyRouteFilters($collection, $request); 127 $collection = $this->applyRouteFilters($collection, $request);
152 128
153 if ($ret = $this->matchCollection(rawurldecode($this->currentPath->getPath($request)), $collection)) { 129 if ($ret = $this->matchCollection(rawurldecode($this->currentPath->getPath($request)), $collection)) {
154 return $this->applyRouteEnhancers($ret, $request); 130 return $this->applyRouteEnhancers($ret, $request);
155 } 131 }
274 * The request attributes after applying the enhancers. This might consist 250 * The request attributes after applying the enhancers. This might consist
275 * raw values from the URL but also upcasted values, like entity objects, 251 * raw values from the URL but also upcasted values, like entity objects,
276 * from route enhancers. 252 * from route enhancers.
277 */ 253 */
278 protected function applyRouteEnhancers($defaults, Request $request) { 254 protected function applyRouteEnhancers($defaults, Request $request) {
279 foreach ($this->getRouteEnhancers() as $enhancer) { 255 foreach ($this->enhancers as $enhancer) {
256 if ($enhancer instanceof RouteEnhancerInterface && !$enhancer->applies($defaults[RouteObjectInterface::ROUTE_OBJECT])) {
257 continue;
258 }
280 $defaults = $enhancer->enhance($defaults, $request); 259 $defaults = $enhancer->enhance($defaults, $request);
281 } 260 }
282 261
283 return $defaults; 262 return $defaults;
284 }
285
286 /**
287 * Sorts the enhancers and flattens them.
288 *
289 * @return \Symfony\Cmf\Component\Routing\Enhancer\RouteEnhancerInterface[]
290 * The enhancers ordered by priority.
291 */
292 public function getRouteEnhancers() {
293 if (!isset($this->sortedEnhancers)) {
294 $this->sortedEnhancers = $this->sortRouteEnhancers();
295 }
296
297 return $this->sortedEnhancers;
298 }
299
300 /**
301 * Sort enhancers by priority.
302 *
303 * The highest priority number is the highest priority (reverse sorting).
304 *
305 * @return \Symfony\Cmf\Component\Routing\Enhancer\RouteEnhancerInterface[]
306 * The sorted enhancers.
307 */
308 protected function sortRouteEnhancers() {
309 $sortedEnhancers = [];
310 krsort($this->enhancers);
311
312 foreach ($this->enhancers as $enhancers) {
313 $sortedEnhancers = array_merge($sortedEnhancers, $enhancers);
314 }
315
316 return $sortedEnhancers;
317 } 263 }
318 264
319 /** 265 /**
320 * Applies all route filters to a given route collection. 266 * Applies all route filters to a given route collection.
321 * 267 *
331 * The filtered/sorted route collection. 277 * The filtered/sorted route collection.
332 */ 278 */
333 protected function applyRouteFilters(RouteCollection $collection, Request $request) { 279 protected function applyRouteFilters(RouteCollection $collection, Request $request) {
334 // Route filters are expected to throw an exception themselves if they 280 // Route filters are expected to throw an exception themselves if they
335 // end up filtering the list down to 0. 281 // end up filtering the list down to 0.
336 foreach ($this->getRouteFilters() as $filter) { 282 foreach ($this->filters as $filter) {
337 $collection = $filter->filter($collection, $request); 283 $collection = $filter->filter($collection, $request);
338 } 284 }
339 285
340 return $collection; 286 return $collection;
341 }
342
343 /**
344 * Sorts the filters and flattens them.
345 *
346 * @return \Symfony\Cmf\Component\Routing\NestedMatcher\RouteFilterInterface[]
347 * The filters ordered by priority
348 */
349 public function getRouteFilters() {
350 if (!isset($this->sortedFilters)) {
351 $this->sortedFilters = $this->sortFilters();
352 }
353
354 return $this->sortedFilters;
355 }
356
357 /**
358 * Sort filters by priority.
359 *
360 * The highest priority number is the highest priority (reverse sorting).
361 *
362 * @return \Symfony\Cmf\Component\Routing\NestedMatcher\RouteFilterInterface[]
363 * The sorted filters.
364 */
365 protected function sortFilters() {
366 $sortedFilters = [];
367 krsort($this->filters);
368
369 foreach ($this->filters as $filters) {
370 $sortedFilters = array_merge($sortedFilters, $filters);
371 }
372
373 return $sortedFilters;
374 } 287 }
375 288
376 /** 289 /**
377 * {@inheritdoc} 290 * {@inheritdoc}
378 */ 291 */