PathProcessorManager.php 3.7 KB

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