ApacheUrlMatcher.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. * @author Fabien Potencier <fabien@symfony.com>
  16. */
  17. class ApacheUrlMatcher extends UrlMatcher
  18. {
  19. /**
  20. * Tries to match a URL based on Apache mod_rewrite matching.
  21. *
  22. * Returns false if no route matches the URL.
  23. *
  24. * @param string $pathinfo The pathinfo to be parsed
  25. *
  26. * @return array An array of parameters
  27. *
  28. * @throws MethodNotAllowedException If the current method is not allowed
  29. */
  30. public function match($pathinfo)
  31. {
  32. $parameters = array();
  33. $allow = array();
  34. $match = false;
  35. foreach ($_SERVER as $key => $value) {
  36. $name = $key;
  37. if (0 === strpos($name, 'REDIRECT_')) {
  38. $name = substr($name, 9);
  39. }
  40. if (0 === strpos($name, '_ROUTING_')) {
  41. $name = substr($name, 9);
  42. } else {
  43. continue;
  44. }
  45. if ('_route' == $name) {
  46. $match = true;
  47. } elseif (0 === strpos($name, '_allow_')) {
  48. $allow[] = substr($name, 7);
  49. } else {
  50. $parameters[$name] = $value;
  51. }
  52. unset($_SERVER[$key]);
  53. }
  54. if ($match) {
  55. return $parameters;
  56. } elseif (0 < count($allow)) {
  57. throw new MethodNotAllowedException($allow);
  58. } else {
  59. return parent::match($pathinfo);
  60. }
  61. }
  62. }