4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Routing\Matcher;
14 @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);
16 use Symfony\Component\Routing\Exception\MethodNotAllowedException;
19 * ApacheUrlMatcher matches URL based on Apache mod_rewrite matching (see ApacheMatcherDumper).
21 * @deprecated since version 2.5, to be removed in 3.0.
22 * The performance gains are minimal and it's very hard to replicate
23 * the behavior of PHP implementation.
25 * @author Fabien Potencier <fabien@symfony.com>
26 * @author Arnaud Le Blanc <arnaud.lb@gmail.com>
28 class ApacheUrlMatcher extends UrlMatcher
31 * Tries to match a URL based on Apache mod_rewrite matching.
33 * Returns false if no route matches the URL.
35 * @param string $pathinfo The pathinfo to be parsed
37 * @return array An array of parameters
39 * @throws MethodNotAllowedException If the current method is not allowed
41 public function match($pathinfo)
43 $parameters = array();
48 foreach ($this->denormalizeValues($_SERVER) as $key => $value) {
51 // skip non-routing variables
52 // this improves performance when $_SERVER contains many usual
53 // variables like HTTP_*, DOCUMENT_ROOT, REQUEST_URI, ...
54 if (false === strpos($name, '_ROUTING_')) {
58 while (0 === strpos($name, 'REDIRECT_')) {
59 $name = substr($name, 9);
62 // expect _ROUTING_<type>_<name>
65 if (0 !== strpos($name, '_ROUTING_')) {
68 if (false !== $pos = strpos($name, '_', 9)) {
69 $type = substr($name, 9, $pos - 9);
70 $name = substr($name, $pos + 1);
72 $type = substr($name, 9);
75 if ('param' === $type) {
77 $parameters[$name] = $value;
79 } elseif ('default' === $type) {
80 $defaults[$name] = $value;
81 } elseif ('route' === $type) {
83 } elseif ('allow' === $type) {
87 unset($_SERVER[$key]);
90 if (null !== $route) {
91 $parameters['_route'] = $route;
93 return $this->mergeDefaults($parameters, $defaults);
94 } elseif (0 < count($allow)) {
95 throw new MethodNotAllowedException($allow);
97 return parent::match($pathinfo);
102 * Denormalizes an array of values.
104 * @param string[] $values
108 private function denormalizeValues(array $values)
110 $normalizedValues = array();
111 foreach ($values as $key => $value) {
112 if (preg_match('~^(.*)\[(\d+)\]$~', $key, $matches)) {
113 if (!isset($normalizedValues[$matches[1]])) {
114 $normalizedValues[$matches[1]] = array();
116 $normalizedValues[$matches[1]][(int) $matches[2]] = $value;
118 $normalizedValues[$key] = $value;
122 return $normalizedValues;