Upgraded drupal core with security updates
[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   /**
91    * Adds an outbound processor object to the $outboundProcessors property.
92    *
93    * @param \Drupal\Core\PathProcessor\OutboundPathProcessorInterface $processor
94    *   The processor object to add.
95    * @param int $priority
96    *   The priority of the processor being added.
97    */
98   public function addOutbound(OutboundPathProcessorInterface $processor, $priority = 0) {
99     $this->outboundProcessors[$priority][] = $processor;
100     $this->sortedOutbound = [];
101   }
102
103   /**
104    * {@inheritdoc}
105    */
106   public function processOutbound($path, &$options = [], Request $request = NULL, BubbleableMetadata $bubbleable_metadata = NULL) {
107     $processors = $this->getOutbound();
108     foreach ($processors as $processor) {
109       $path = $processor->processOutbound($path, $options, $request, $bubbleable_metadata);
110     }
111     return $path;
112   }
113
114   /**
115    * Returns the sorted array of outbound processors.
116    *
117    * @return array
118    *   An array of processor objects.
119    */
120   protected function getOutbound() {
121     if (empty($this->sortedOutbound)) {
122       $this->sortedOutbound = $this->sortProcessors('outboundProcessors');
123     }
124
125     return $this->sortedOutbound;
126   }
127
128   /**
129    * Sorts the processors according to priority.
130    *
131    * @param string $type
132    *   The processor type to sort, e.g. 'inboundProcessors'.
133    */
134   protected function sortProcessors($type) {
135     $sorted = [];
136     krsort($this->{$type});
137
138     foreach ($this->{$type} as $processors) {
139       $sorted = array_merge($sorted, $processors);
140     }
141     return $sorted;
142   }
143
144 }