PathProcessorManager.php 3.72 KB
Newer Older
1 2 3 4
<?php

namespace Drupal\Core\PathProcessor;

5
use Drupal\Core\Render\BubbleableMetadata;
6 7 8 9 10 11 12 13
use Symfony\Component\HttpFoundation\Request;

/**
 * Path processor manager.
 *
 * Holds an array of path processor objects and uses them to sequentially process
 * a path, in order of processor priority.
 */
14
class PathProcessorManager implements InboundPathProcessorInterface, OutboundPathProcessorInterface {
15 16

  /**
17
   * Holds the array of inbound processors to cycle through.
18 19 20 21 22
   *
   * @var array
   *   An array whose keys are priorities and whose values are arrays of path
   *   processor objects.
   */
23
  protected $inboundProcessors = [];
24 25

  /**
26
   * Holds the array of inbound processors, sorted by priority.
27 28 29 30
   *
   * @var array
   *   An array of path processor objects.
   */
31
  protected $sortedInbound = [];
32

33 34 35 36 37 38 39 40

  /**
   * Holds the array of outbound processors to cycle through.
   *
   * @var array
   *   An array whose keys are priorities and whose values are arrays of path
   *   processor objects.
   */
41
  protected $outboundProcessors = [];
42 43 44 45 46 47 48

  /**
   * Holds the array of outbound processors, sorted by priority.
   *
   * @var array
   *   An array of path processor objects.
   */
49
  protected $sortedOutbound = [];
50

51 52 53 54 55 56 57 58 59 60
  /**
   * Adds an inbound processor object to the $inboundProcessors property.
   *
   * @param \Drupal\Core\PathProcessor\InboundPathProcessorInterface $processor
   *   The processor object to add.
   * @param int $priority
   *   The priority of the processor being added.
   */
  public function addInbound(InboundPathProcessorInterface $processor, $priority = 0) {
    $this->inboundProcessors[$priority][] = $processor;
61
    $this->sortedInbound = [];
62 63 64
  }

  /**
65
   * {@inheritdoc}
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
   */
  public function processInbound($path, Request $request) {
    $processors = $this->getInbound();
    foreach ($processors as $processor) {
      $path = $processor->processInbound($path, $request);
    }
    return $path;
  }

  /**
   * Returns the sorted array of inbound processors.
   *
   * @return array
   *   An array of processor objects.
   */
  protected function getInbound() {
    if (empty($this->sortedInbound)) {
      $this->sortedInbound = $this->sortProcessors('inboundProcessors');
    }

    return $this->sortedInbound;
  }

89 90 91 92 93 94 95 96 97 98 99

  /**
   * Adds an outbound processor object to the $outboundProcessors property.
   *
   * @param \Drupal\Core\PathProcessor\OutboundPathProcessorInterface $processor
   *   The processor object to add.
   * @param int $priority
   *   The priority of the processor being added.
   */
  public function addOutbound(OutboundPathProcessorInterface $processor, $priority = 0) {
    $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 108
    $processors = $this->getOutbound();
    foreach ($processors as $processor) {
109
      $path = $processor->processOutbound($path, $options, $request, $bubbleable_metadata);
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
    }
    return $path;
  }

  /**
   * Returns the sorted array of outbound processors.
   *
   * @return array
   *   An array of processor objects.
   */
  protected function getOutbound() {
    if (empty($this->sortedOutbound)) {
      $this->sortedOutbound = $this->sortProcessors('outboundProcessors');
    }

    return $this->sortedOutbound;
  }

128 129 130 131 132 133 134
  /**
   * Sorts the processors according to priority.
   *
   * @param string $type
   *   The processor type to sort, e.g. 'inboundProcessors'.
   */
  protected function sortProcessors($type) {
135
    $sorted = [];
136 137 138 139 140 141 142
    krsort($this->{$type});

    foreach ($this->{$type} as $processors) {
      $sorted = array_merge($sorted, $processors);
    }
    return $sorted;
  }
143

144
}