Version 1
[yaffs-website] / web / core / lib / Drupal / Core / RouteProcessor / RouteProcessorManager.php
1 <?php
2
3 namespace Drupal\Core\RouteProcessor;
4
5 use Drupal\Core\Render\BubbleableMetadata;
6 use Symfony\Component\Routing\Route;
7
8 /**
9  * Route processor manager.
10  *
11  * Holds an array of route processor objects and uses them to sequentially
12  * process an outbound route, in order of processor priority.
13  */
14 class RouteProcessorManager implements OutboundRouteProcessorInterface {
15
16   /**
17    * Holds the array of outbound processors to cycle through.
18    *
19    * @var array
20    *   An array whose keys are priorities and whose values are arrays of path
21    *   processor objects.
22    */
23   protected $outboundProcessors = [];
24
25   /**
26    * Holds the array of outbound processors, sorted by priority.
27    *
28    * @var array
29    *   An array of path processor objects.
30    */
31   protected $sortedOutbound = [];
32
33   /**
34    * Adds an outbound processor object to the $outboundProcessors property.
35    *
36    * @param \Drupal\Core\RouteProcessor\OutboundRouteProcessorInterface $processor
37    *   The processor object to add.
38    * @param int $priority
39    *   The priority of the processor being added.
40    */
41   public function addOutbound(OutboundRouteProcessorInterface $processor, $priority = 0) {
42     $this->outboundProcessors[$priority][] = $processor;
43     $this->sortedOutbound = [];
44   }
45
46   /**
47    * {@inheritdoc}
48    */
49   public function processOutbound($route_name, Route $route, array &$parameters, BubbleableMetadata $bubbleable_metadata = NULL) {
50     $processors = $this->getOutbound();
51     foreach ($processors as $processor) {
52       $processor->processOutbound($route_name, $route, $parameters, $bubbleable_metadata);
53     }
54   }
55
56   /**
57    * Returns the sorted array of outbound processors.
58    *
59    * @return array
60    *   An array of processor objects.
61    */
62   protected function getOutbound() {
63     if (empty($this->sortedOutbound)) {
64       $this->sortedOutbound = $this->sortProcessors();
65     }
66
67     return $this->sortedOutbound;
68   }
69
70   /**
71    * Sorts the processors according to priority.
72    */
73   protected function sortProcessors() {
74     $sorted = [];
75     krsort($this->outboundProcessors);
76
77     foreach ($this->outboundProcessors as $processors) {
78       $sorted = array_merge($sorted, $processors);
79     }
80     return $sorted;
81   }
82
83 }