3 namespace Drupal\Core\PathProcessor;
5 use Drupal\Core\Render\BubbleableMetadata;
6 use Symfony\Component\HttpFoundation\Request;
9 * Path processor manager.
11 * Holds an array of path processor objects and uses them to sequentially process
12 * a path, in order of processor priority.
14 class PathProcessorManager implements InboundPathProcessorInterface, OutboundPathProcessorInterface {
17 * Holds the array of inbound processors to cycle through.
20 * An array whose keys are priorities and whose values are arrays of path
23 protected $inboundProcessors = [];
26 * Holds the array of inbound processors, sorted by priority.
29 * An array of path processor objects.
31 protected $sortedInbound = [];
35 * Holds the array of outbound processors to cycle through.
38 * An array whose keys are priorities and whose values are arrays of path
41 protected $outboundProcessors = [];
44 * Holds the array of outbound processors, sorted by priority.
47 * An array of path processor objects.
49 protected $sortedOutbound = [];
52 * Adds an inbound processor object to the $inboundProcessors property.
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.
59 public function addInbound(InboundPathProcessorInterface $processor, $priority = 0) {
60 $this->inboundProcessors[$priority][] = $processor;
61 $this->sortedInbound = [];
67 public function processInbound($path, Request $request) {
68 $processors = $this->getInbound();
69 foreach ($processors as $processor) {
70 $path = $processor->processInbound($path, $request);
76 * Returns the sorted array of inbound processors.
79 * An array of processor objects.
81 protected function getInbound() {
82 if (empty($this->sortedInbound)) {
83 $this->sortedInbound = $this->sortProcessors('inboundProcessors');
86 return $this->sortedInbound;
91 * Adds an outbound processor object to the $outboundProcessors property.
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.
98 public function addOutbound(OutboundPathProcessorInterface $processor, $priority = 0) {
99 $this->outboundProcessors[$priority][] = $processor;
100 $this->sortedOutbound = [];
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);
115 * Returns the sorted array of outbound processors.
118 * An array of processor objects.
120 protected function getOutbound() {
121 if (empty($this->sortedOutbound)) {
122 $this->sortedOutbound = $this->sortProcessors('outboundProcessors');
125 return $this->sortedOutbound;
129 * Sorts the processors according to priority.
131 * @param string $type
132 * The processor type to sort, e.g. 'inboundProcessors'.
134 protected function sortProcessors($type) {
136 krsort($this->{$type});
138 foreach ($this->{$type} as $processors) {
139 $sorted = array_merge($sorted, $processors);