Drupal investigation

ApacheUrlMatcher.php 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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;
  11. @trigger_error('The '.__NAMESPACE__.'\ApacheUrlMatcher class is deprecated since version 2.5 and will be removed in 3.0. It\'s hard to replicate the behaviour of the PHP implementation and the performance gains are minimal.', E_USER_DEPRECATED);
  12. use Symfony\Component\Routing\Exception\MethodNotAllowedException;
  13. /**
  14. * ApacheUrlMatcher matches URL based on Apache mod_rewrite matching (see ApacheMatcherDumper).
  15. *
  16. * @deprecated since version 2.5, to be removed in 3.0.
  17. * The performance gains are minimal and it's very hard to replicate
  18. * the behavior of PHP implementation.
  19. *
  20. * @author Fabien Potencier <fabien@symfony.com>
  21. * @author Arnaud Le Blanc <arnaud.lb@gmail.com>
  22. */
  23. class ApacheUrlMatcher extends UrlMatcher
  24. {
  25. /**
  26. * Tries to match a URL based on Apache mod_rewrite matching.
  27. *
  28. * Returns false if no route matches the URL.
  29. *
  30. * @param string $pathinfo The pathinfo to be parsed
  31. *
  32. * @return array An array of parameters
  33. *
  34. * @throws MethodNotAllowedException If the current method is not allowed
  35. */
  36. public function match($pathinfo)
  37. {
  38. $parameters = array();
  39. $defaults = array();
  40. $allow = array();
  41. $route = null;
  42. foreach ($this->denormalizeValues($_SERVER) as $key => $value) {
  43. $name = $key;
  44. // skip non-routing variables
  45. // this improves performance when $_SERVER contains many usual
  46. // variables like HTTP_*, DOCUMENT_ROOT, REQUEST_URI, ...
  47. if (false === strpos($name, '_ROUTING_')) {
  48. continue;
  49. }
  50. while (0 === strpos($name, 'REDIRECT_')) {
  51. $name = substr($name, 9);
  52. }
  53. // expect _ROUTING_<type>_<name>
  54. // or _ROUTING_<type>
  55. if (0 !== strpos($name, '_ROUTING_')) {
  56. continue;
  57. }
  58. if (false !== $pos = strpos($name, '_', 9)) {
  59. $type = substr($name, 9, $pos - 9);
  60. $name = substr($name, $pos + 1);
  61. } else {
  62. $type = substr($name, 9);
  63. }
  64. if ('param' === $type) {
  65. if ('' !== $value) {
  66. $parameters[$name] = $value;
  67. }
  68. } elseif ('default' === $type) {
  69. $defaults[$name] = $value;
  70. } elseif ('route' === $type) {
  71. $route = $value;
  72. } elseif ('allow' === $type) {
  73. $allow[] = $name;
  74. }
  75. unset($_SERVER[$key]);
  76. }
  77. if (null !== $route) {
  78. $parameters['_route'] = $route;
  79. return $this->mergeDefaults($parameters, $defaults);
  80. } elseif (0 < count($allow)) {
  81. throw new MethodNotAllowedException($allow);
  82. } else {
  83. return parent::match($pathinfo);
  84. }
  85. }
  86. /**
  87. * Denormalizes an array of values.
  88. *
  89. * @param string[] $values
  90. *
  91. * @return array
  92. */
  93. private function denormalizeValues(array $values)
  94. {
  95. $normalizedValues = array();
  96. foreach ($values as $key => $value) {
  97. if (preg_match('~^(.*)\[(\d+)\]$~', $key, $matches)) {
  98. if (!isset($normalizedValues[$matches[1]])) {
  99. $normalizedValues[$matches[1]] = array();
  100. }
  101. $normalizedValues[$matches[1]][(int) $matches[2]] = $value;
  102. } else {
  103. $normalizedValues[$key] = $value;
  104. }
  105. }
  106. return $normalizedValues;
  107. }
  108. }