No Description

ApacheUrlMatcher.php 3.6KB

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