c0474f22fd55adfeb45d15e8ce063da07885b02c
[yaffs-website] / vendor / symfony / routing / Matcher / ApacheUrlMatcher.php
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 @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);
15
16 use Symfony\Component\Routing\Exception\MethodNotAllowedException;
17
18 /**
19  * ApacheUrlMatcher matches URL based on Apache mod_rewrite matching (see ApacheMatcherDumper).
20  *
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.
24  *
25  * @author Fabien Potencier <fabien@symfony.com>
26  * @author Arnaud Le Blanc <arnaud.lb@gmail.com>
27  */
28 class ApacheUrlMatcher extends UrlMatcher
29 {
30     /**
31      * Tries to match a URL based on Apache mod_rewrite matching.
32      *
33      * Returns false if no route matches the URL.
34      *
35      * @param string $pathinfo The pathinfo to be parsed
36      *
37      * @return array An array of parameters
38      *
39      * @throws MethodNotAllowedException If the current method is not allowed
40      */
41     public function match($pathinfo)
42     {
43         $parameters = array();
44         $defaults = array();
45         $allow = array();
46         $route = null;
47
48         foreach ($this->denormalizeValues($_SERVER) as $key => $value) {
49             $name = $key;
50
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_')) {
55                 continue;
56             }
57
58             while (0 === strpos($name, 'REDIRECT_')) {
59                 $name = substr($name, 9);
60             }
61
62             // expect _ROUTING_<type>_<name>
63             // or _ROUTING_<type>
64
65             if (0 !== strpos($name, '_ROUTING_')) {
66                 continue;
67             }
68             if (false !== $pos = strpos($name, '_', 9)) {
69                 $type = substr($name, 9, $pos - 9);
70                 $name = substr($name, $pos + 1);
71             } else {
72                 $type = substr($name, 9);
73             }
74
75             if ('param' === $type) {
76                 if ('' !== $value) {
77                     $parameters[$name] = $value;
78                 }
79             } elseif ('default' === $type) {
80                 $defaults[$name] = $value;
81             } elseif ('route' === $type) {
82                 $route = $value;
83             } elseif ('allow' === $type) {
84                 $allow[] = $name;
85             }
86
87             unset($_SERVER[$key]);
88         }
89
90         if (null !== $route) {
91             $parameters['_route'] = $route;
92
93             return $this->mergeDefaults($parameters, $defaults);
94         } elseif (0 < count($allow)) {
95             throw new MethodNotAllowedException($allow);
96         } else {
97             return parent::match($pathinfo);
98         }
99     }
100
101     /**
102      * Denormalizes an array of values.
103      *
104      * @param string[] $values
105      *
106      * @return array
107      */
108     private function denormalizeValues(array $values)
109     {
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();
115                 }
116                 $normalizedValues[$matches[1]][(int) $matches[2]] = $value;
117             } else {
118                 $normalizedValues[$key] = $value;
119             }
120         }
121
122         return $normalizedValues;
123     }
124 }