Drupal investigation

Candidates.php 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <?php
  2. /*
  3. * This file is part of the Symfony CMF package.
  4. *
  5. * (c) 2011-2015 Symfony CMF
  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\Cmf\Component\Routing\Candidates;
  11. use Symfony\Component\HttpFoundation\Request;
  12. /**
  13. * A straightforward strategy that splits the URL on "/".
  14. *
  15. * If locales is set, additionally generates candidates removing the locale if
  16. * it is one of the configured locales, for non-locale specific URLs.
  17. *
  18. * @author David Buchmann <mail@davidbu.ch>
  19. */
  20. class Candidates implements CandidatesInterface
  21. {
  22. /**
  23. * @var array
  24. */
  25. protected $locales;
  26. /**
  27. * A limit to apply to the number of candidates generated.
  28. *
  29. * This is to prevent abusive requests with a lot of "/". The limit is per
  30. * batch, that is if a locale matches you could get as many as 2 * $limit
  31. * candidates if the URL has that many slashes.
  32. *
  33. * @var int
  34. */
  35. protected $limit;
  36. /**
  37. * @param array $locales The locales to support.
  38. * @param int $limit A limit to apply to the candidates generated.
  39. */
  40. public function __construct(array $locales = array(), $limit = 20)
  41. {
  42. $this->setLocales($locales);
  43. $this->limit = $limit;
  44. }
  45. /**
  46. * Set the locales to support by this strategy.
  47. *
  48. * @param array $locales The locales to support.
  49. */
  50. public function setLocales(array $locales)
  51. {
  52. $this->locales = $locales;
  53. }
  54. /**
  55. * {@inheritdoc}
  56. *
  57. * Always returns true.
  58. */
  59. public function isCandidate($name)
  60. {
  61. return true;
  62. }
  63. /**
  64. * {@inheritdoc}
  65. *
  66. * Does nothing.
  67. */
  68. public function restrictQuery($queryBuilder)
  69. {
  70. }
  71. /**
  72. * {@inheritdoc}
  73. */
  74. public function getCandidates(Request $request)
  75. {
  76. $url = $request->getPathInfo();
  77. $candidates = $this->getCandidatesFor($url);
  78. $locale = $this->determineLocale($url);
  79. if ($locale) {
  80. $candidates = array_unique(array_merge($candidates, $this->getCandidatesFor(substr($url, strlen($locale) + 1))));
  81. }
  82. return $candidates;
  83. }
  84. /**
  85. * Determine the locale of this URL.
  86. *
  87. * @param string $url The url to determine the locale from.
  88. *
  89. * @return string|bool The locale if $url starts with one of the allowed locales.
  90. */
  91. protected function determineLocale($url)
  92. {
  93. if (!count($this->locales)) {
  94. return false;
  95. }
  96. $matches = array();
  97. if (preg_match('#('.implode('|', $this->locales).')(/|$)#', $url, $matches)) {
  98. return $matches[1];
  99. }
  100. return false;
  101. }
  102. /**
  103. * Handle a possible format extension and split the $url on "/".
  104. *
  105. * $prefix is prepended to every candidate generated.
  106. *
  107. * @param string $url The URL to split.
  108. * @param string $prefix A prefix to prepend to every pattern.
  109. *
  110. * @return array Paths that could represent routes that match $url and are
  111. * child of $prefix.
  112. */
  113. protected function getCandidatesFor($url, $prefix = '')
  114. {
  115. $candidates = array();
  116. if ('/' !== $url) {
  117. // handle format extension, like .html or .json
  118. if (preg_match('/(.+)\.[a-z]+$/i', $url, $matches)) {
  119. $candidates[] = $prefix.$url;
  120. $url = $matches[1];
  121. }
  122. $part = $url;
  123. $count = 0;
  124. while (false !== ($pos = strrpos($part, '/'))) {
  125. if (++$count > $this->limit) {
  126. return $candidates;
  127. }
  128. $candidates[] = $prefix.$part;
  129. $part = substr($url, 0, $pos);
  130. }
  131. }
  132. $candidates[] = $prefix ?: '/';
  133. return $candidates;
  134. }
  135. }