Pull merge.
[yaffs-website] / web / core / lib / Drupal / Core / PathProcessor / PathProcessorManager.php
1 <?php
2
3 namespace Drupal\Core\PathProcessor;
4
5 use Drupal\Core\Render\BubbleableMetadata;
6 use Symfony\Component\HttpFoundation\Request;
7
8 /**
9  * Path processor manager.
10  *
11  * Holds an array of path processor objects and uses them to sequentially process
12  * a path, in order of processor priority.
13  */
14 class PathProcessorManager implements InboundPathProcessorInterface, OutboundPathProcessorInterface {
15
16   /**
17    * Holds the array of inbound 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 $inboundProcessors = [];
24
25   /**
26    * Holds the array of inbound processors, sorted by priority.
27    *
28    * @var array
29    *   An array of path processor objects.
30    */
31   protected $sortedInbound = [];
32
33
34   /**
35    * Holds the array of outbound processors to cycle through.
36    *
37    * @var array
38    *   An array whose keys are priorities and whose values are arrays of path
39    *   processor objects.
40    */
41   protected $outboundProcessors = [];
42
43   /**
44    * Holds the array of outbound processors, sorted by priority.
45    *
46    * @var array
47    *   An array of path processor objects.
48    */
49   protected $sortedOutbound = [];
50
51   /**
52    * Adds an inbound processor object to the $inboundProcessors property.
53    *
54    * @param \Drupal\Core\PathProcessor\InboundPathProcessorInterface $processor
55    *   The processor object to add.
56    * @param int $priority
57    *   The priority of the processor being added.
58    */
59   public function addInbound(InboundPathProcessorInterface $processor, $priority = 0) {
60     $this->inboundProcessors[$priority][] = $processor;
61     $this->sortedInbound = [];
62   }
63
64   /**
65    * {@inheritdoc}
66    */
67   public function processInbound($path, Request $request) {
68     $processors = $this->getInbound();
69     foreach ($processors as $processor) {
70       $path = $processor->processInbound($path, $request);
71     }
72     return $path;
73   }
74
75   /**
76    * Returns the sorted array of inbound processors.
77    *
78    * @return array
79    *   An array of processor objects.
80    */
81   protected function getInbound() {
82     if (empty($this->sortedInbound)) {
83       $this->sortedInbound = $this->sortProcessors('inboundProcessors');
84     }
85
86     return $this->sortedInbound;
87   }
88
89   /**
90    * Adds an outbound processor object to the $outboundProcessors property.
91    *
92    * @param \Drupal\Core\PathProcessor\OutboundPathProcessorInterface $processor
93    *   The processor object to add.
94    * @param int $priority
95    *   The priority of the processor being added.
96    */
97   public function addOutbound(OutboundPathProcessorInterface $processor, $priority = 0) {
98     $this->outboundProcessors[$priority][] = $processor;
99     $this->sortedOutbound = [];
100   }
101
102   /**
103    * {@inheritdoc}
104    */
105   public function processOutbound($path, &$options = [], Request $request = NULL, BubbleableMetadata $bubbleable_metadata = NULL) {
106     $processors = $this->getOutbound();
107     foreach ($processors as $processor) {
108       $path = $processor->processOutbound($path, $options, $request, $bubbleable_metadata);
109     }
110     return $path;
111   }
112
113   /**
114    * Returns the sorted array of outbound processors.
115    *
116    * @return array
117    *   An array of processor objects.
118    */
119   protected function getOutbound() {
120     if (empty($this->sortedOutbound)) {
121       $this->sortedOutbound = $this->sortProcessors('outboundProcessors');
122     }
123
124     return $this->sortedOutbound;
125   }
126
127   /**
128    * Sorts the processors according to priority.
129    *
130    * @param string $type
131    *   The processor type to sort, e.g. 'inboundProcessors'.
132    */
133   protected function sortProcessors($type) {
134     $sorted = [];
135     krsort($this->{$type});
136
137     foreach ($this->{$type} as $processors) {
138       $sorted = array_merge($sorted, $processors);
139     }
140     return $sorted;
141   }
142
143 }