Drupal investigation

DumperPrefixCollection.php 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Routing\Matcher\Dumper;
  11. /**
  12. * Prefix tree of routes preserving routes order.
  13. *
  14. * @author Arnaud Le Blanc <arnaud.lb@gmail.com>
  15. *
  16. * @internal
  17. */
  18. class DumperPrefixCollection extends DumperCollection
  19. {
  20. /**
  21. * @var string
  22. */
  23. private $prefix = '';
  24. /**
  25. * Returns the prefix.
  26. *
  27. * @return string The prefix
  28. */
  29. public function getPrefix()
  30. {
  31. return $this->prefix;
  32. }
  33. /**
  34. * Sets the prefix.
  35. *
  36. * @param string $prefix The prefix
  37. */
  38. public function setPrefix($prefix)
  39. {
  40. $this->prefix = $prefix;
  41. }
  42. /**
  43. * Adds a route in the tree.
  44. *
  45. * @param DumperRoute $route The route
  46. *
  47. * @return self
  48. *
  49. * @throws \LogicException
  50. */
  51. public function addPrefixRoute(DumperRoute $route)
  52. {
  53. $prefix = $route->getRoute()->compile()->getStaticPrefix();
  54. for ($collection = $this; null !== $collection; $collection = $collection->getParent()) {
  55. // Same prefix, add to current leave
  56. if ($collection->prefix === $prefix) {
  57. $collection->add($route);
  58. return $collection;
  59. }
  60. // Prefix starts with route's prefix
  61. if ('' === $collection->prefix || 0 === strpos($prefix, $collection->prefix)) {
  62. $child = new self();
  63. $child->setPrefix(substr($prefix, 0, strlen($collection->prefix) + 1));
  64. $collection->add($child);
  65. return $child->addPrefixRoute($route);
  66. }
  67. }
  68. // Reached only if the root has a non empty prefix
  69. throw new \LogicException('The collection root must not have a prefix');
  70. }
  71. /**
  72. * Merges nodes whose prefix ends with a slash.
  73. *
  74. * Children of a node whose prefix ends with a slash are moved to the parent node
  75. */
  76. public function mergeSlashNodes()
  77. {
  78. $children = array();
  79. foreach ($this as $child) {
  80. if ($child instanceof self) {
  81. $child->mergeSlashNodes();
  82. if ('/' === substr($child->prefix, -1)) {
  83. $children = array_merge($children, $child->all());
  84. } else {
  85. $children[] = $child;
  86. }
  87. } else {
  88. $children[] = $child;
  89. }
  90. }
  91. $this->setAll($children);
  92. }
  93. }