]>
Commit | Line | Data |
---|---|---|
4f5b44bd NL |
1 | <?php |
2 | ||
3 | /* | |
4 | * This file is part of the Symfony package. | |
5 | * | |
6 | * (c) Fabien Potencier <fabien@symfony.com> | |
7 | * | |
8 | * For the full copyright and license information, please view the LICENSE | |
9 | * file that was distributed with this source code. | |
10 | */ | |
11 | ||
12 | namespace Symfony\Component\Routing\Matcher; | |
13 | ||
14 | use Symfony\Component\Routing\Exception\MethodNotAllowedException; | |
15 | ||
16 | /** | |
17 | * ApacheUrlMatcher matches URL based on Apache mod_rewrite matching (see ApacheMatcherDumper). | |
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 | ||
42 | foreach ($_SERVER as $key => $value) { | |
43 | $name = $key; | |
44 | ||
45 | // skip non-routing variables | |
46 | // this improves performance when $_SERVER contains many usual | |
47 | // variables like HTTP_*, DOCUMENT_ROOT, REQUEST_URI, ... | |
48 | if (false === strpos($name, '_ROUTING_')) { | |
49 | continue; | |
50 | } | |
51 | ||
52 | while (0 === strpos($name, 'REDIRECT_')) { | |
53 | $name = substr($name, 9); | |
54 | } | |
55 | ||
56 | // expect _ROUTING_<type>_<name> | |
57 | // or _ROUTING_<type> | |
58 | ||
59 | if (0 !== strpos($name, '_ROUTING_')) { | |
60 | continue; | |
61 | } | |
62 | if (false !== $pos = strpos($name, '_', 9)) { | |
63 | $type = substr($name, 9, $pos-9); | |
64 | $name = substr($name, $pos+1); | |
65 | } else { | |
66 | $type = substr($name, 9); | |
67 | } | |
68 | ||
69 | if ('param' === $type) { | |
70 | if ('' !== $value) { | |
71 | $parameters[$name] = $value; | |
72 | } | |
73 | } elseif ('default' === $type) { | |
74 | $defaults[$name] = $value; | |
75 | } elseif ('route' === $type) { | |
76 | $route = $value; | |
77 | } elseif ('allow' === $type) { | |
78 | $allow[] = $name; | |
79 | } | |
80 | ||
81 | unset($_SERVER[$key]); | |
82 | } | |
83 | ||
84 | if (null !== $route) { | |
85 | $parameters['_route'] = $route; | |
86 | ||
87 | return $this->mergeDefaults($parameters, $defaults); | |
88 | } elseif (0 < count($allow)) { | |
89 | throw new MethodNotAllowedException($allow); | |
90 | } else { | |
91 | return parent::match($pathinfo); | |
92 | } | |
93 | } | |
94 | } |