Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 /*
|
Chris@0
|
4 * This file is part of the Symfony package.
|
Chris@0
|
5 *
|
Chris@0
|
6 * (c) Fabien Potencier <fabien@symfony.com>
|
Chris@0
|
7 *
|
Chris@0
|
8 * For the full copyright and license information, please view the LICENSE
|
Chris@0
|
9 * file that was distributed with this source code.
|
Chris@0
|
10 */
|
Chris@0
|
11
|
Chris@0
|
12 namespace Symfony\Component\Routing\Matcher;
|
Chris@0
|
13
|
Chris@0
|
14 use Symfony\Component\Routing\Exception\MethodNotAllowedException;
|
Chris@14
|
15 use Symfony\Component\Routing\Exception\NoConfigurationException;
|
Chris@0
|
16 use Symfony\Component\Routing\Exception\ResourceNotFoundException;
|
Chris@0
|
17 use Symfony\Component\Routing\RouteCollection;
|
Chris@0
|
18 use Symfony\Component\Routing\RequestContext;
|
Chris@0
|
19 use Symfony\Component\Routing\Route;
|
Chris@0
|
20 use Symfony\Component\HttpFoundation\Request;
|
Chris@0
|
21 use Symfony\Component\ExpressionLanguage\ExpressionLanguage;
|
Chris@0
|
22 use Symfony\Component\ExpressionLanguage\ExpressionFunctionProviderInterface;
|
Chris@0
|
23
|
Chris@0
|
24 /**
|
Chris@0
|
25 * UrlMatcher matches URL based on a set of routes.
|
Chris@0
|
26 *
|
Chris@0
|
27 * @author Fabien Potencier <fabien@symfony.com>
|
Chris@0
|
28 */
|
Chris@0
|
29 class UrlMatcher implements UrlMatcherInterface, RequestMatcherInterface
|
Chris@0
|
30 {
|
Chris@0
|
31 const REQUIREMENT_MATCH = 0;
|
Chris@0
|
32 const REQUIREMENT_MISMATCH = 1;
|
Chris@0
|
33 const ROUTE_MATCH = 2;
|
Chris@0
|
34
|
Chris@0
|
35 protected $context;
|
Chris@0
|
36 protected $allow = array();
|
Chris@0
|
37 protected $routes;
|
Chris@0
|
38 protected $request;
|
Chris@0
|
39 protected $expressionLanguage;
|
Chris@0
|
40
|
Chris@0
|
41 /**
|
Chris@0
|
42 * @var ExpressionFunctionProviderInterface[]
|
Chris@0
|
43 */
|
Chris@0
|
44 protected $expressionLanguageProviders = array();
|
Chris@0
|
45
|
Chris@0
|
46 public function __construct(RouteCollection $routes, RequestContext $context)
|
Chris@0
|
47 {
|
Chris@0
|
48 $this->routes = $routes;
|
Chris@0
|
49 $this->context = $context;
|
Chris@0
|
50 }
|
Chris@0
|
51
|
Chris@0
|
52 /**
|
Chris@0
|
53 * {@inheritdoc}
|
Chris@0
|
54 */
|
Chris@0
|
55 public function setContext(RequestContext $context)
|
Chris@0
|
56 {
|
Chris@0
|
57 $this->context = $context;
|
Chris@0
|
58 }
|
Chris@0
|
59
|
Chris@0
|
60 /**
|
Chris@0
|
61 * {@inheritdoc}
|
Chris@0
|
62 */
|
Chris@0
|
63 public function getContext()
|
Chris@0
|
64 {
|
Chris@0
|
65 return $this->context;
|
Chris@0
|
66 }
|
Chris@0
|
67
|
Chris@0
|
68 /**
|
Chris@0
|
69 * {@inheritdoc}
|
Chris@0
|
70 */
|
Chris@0
|
71 public function match($pathinfo)
|
Chris@0
|
72 {
|
Chris@0
|
73 $this->allow = array();
|
Chris@0
|
74
|
Chris@0
|
75 if ($ret = $this->matchCollection(rawurldecode($pathinfo), $this->routes)) {
|
Chris@0
|
76 return $ret;
|
Chris@0
|
77 }
|
Chris@0
|
78
|
Chris@14
|
79 if ('/' === $pathinfo && !$this->allow) {
|
Chris@14
|
80 throw new NoConfigurationException();
|
Chris@14
|
81 }
|
Chris@14
|
82
|
Chris@0
|
83 throw 0 < count($this->allow)
|
Chris@0
|
84 ? new MethodNotAllowedException(array_unique($this->allow))
|
Chris@0
|
85 : new ResourceNotFoundException(sprintf('No routes found for "%s".', $pathinfo));
|
Chris@0
|
86 }
|
Chris@0
|
87
|
Chris@0
|
88 /**
|
Chris@0
|
89 * {@inheritdoc}
|
Chris@0
|
90 */
|
Chris@0
|
91 public function matchRequest(Request $request)
|
Chris@0
|
92 {
|
Chris@0
|
93 $this->request = $request;
|
Chris@0
|
94
|
Chris@0
|
95 $ret = $this->match($request->getPathInfo());
|
Chris@0
|
96
|
Chris@0
|
97 $this->request = null;
|
Chris@0
|
98
|
Chris@0
|
99 return $ret;
|
Chris@0
|
100 }
|
Chris@0
|
101
|
Chris@0
|
102 public function addExpressionLanguageProvider(ExpressionFunctionProviderInterface $provider)
|
Chris@0
|
103 {
|
Chris@0
|
104 $this->expressionLanguageProviders[] = $provider;
|
Chris@0
|
105 }
|
Chris@0
|
106
|
Chris@0
|
107 /**
|
Chris@0
|
108 * Tries to match a URL with a set of routes.
|
Chris@0
|
109 *
|
Chris@0
|
110 * @param string $pathinfo The path info to be parsed
|
Chris@0
|
111 * @param RouteCollection $routes The set of routes
|
Chris@0
|
112 *
|
Chris@0
|
113 * @return array An array of parameters
|
Chris@0
|
114 *
|
Chris@14
|
115 * @throws NoConfigurationException If no routing configuration could be found
|
Chris@0
|
116 * @throws ResourceNotFoundException If the resource could not be found
|
Chris@0
|
117 * @throws MethodNotAllowedException If the resource was found but the request method is not allowed
|
Chris@0
|
118 */
|
Chris@0
|
119 protected function matchCollection($pathinfo, RouteCollection $routes)
|
Chris@0
|
120 {
|
Chris@0
|
121 foreach ($routes as $name => $route) {
|
Chris@0
|
122 $compiledRoute = $route->compile();
|
Chris@0
|
123
|
Chris@0
|
124 // check the static prefix of the URL first. Only use the more expensive preg_match when it matches
|
Chris@0
|
125 if ('' !== $compiledRoute->getStaticPrefix() && 0 !== strpos($pathinfo, $compiledRoute->getStaticPrefix())) {
|
Chris@0
|
126 continue;
|
Chris@0
|
127 }
|
Chris@0
|
128
|
Chris@0
|
129 if (!preg_match($compiledRoute->getRegex(), $pathinfo, $matches)) {
|
Chris@0
|
130 continue;
|
Chris@0
|
131 }
|
Chris@0
|
132
|
Chris@0
|
133 $hostMatches = array();
|
Chris@0
|
134 if ($compiledRoute->getHostRegex() && !preg_match($compiledRoute->getHostRegex(), $this->context->getHost(), $hostMatches)) {
|
Chris@0
|
135 continue;
|
Chris@0
|
136 }
|
Chris@0
|
137
|
Chris@14
|
138 $status = $this->handleRouteRequirements($pathinfo, $name, $route);
|
Chris@14
|
139
|
Chris@14
|
140 if (self::REQUIREMENT_MISMATCH === $status[0]) {
|
Chris@14
|
141 continue;
|
Chris@14
|
142 }
|
Chris@14
|
143
|
Chris@0
|
144 // check HTTP method requirement
|
Chris@0
|
145 if ($requiredMethods = $route->getMethods()) {
|
Chris@0
|
146 // HEAD and GET are equivalent as per RFC
|
Chris@0
|
147 if ('HEAD' === $method = $this->context->getMethod()) {
|
Chris@0
|
148 $method = 'GET';
|
Chris@0
|
149 }
|
Chris@0
|
150
|
Chris@0
|
151 if (!in_array($method, $requiredMethods)) {
|
Chris@14
|
152 if (self::REQUIREMENT_MATCH === $status[0]) {
|
Chris@14
|
153 $this->allow = array_merge($this->allow, $requiredMethods);
|
Chris@14
|
154 }
|
Chris@0
|
155
|
Chris@0
|
156 continue;
|
Chris@0
|
157 }
|
Chris@0
|
158 }
|
Chris@0
|
159
|
Chris@14
|
160 return $this->getAttributes($route, $name, array_replace($matches, $hostMatches, isset($status[1]) ? $status[1] : array()));
|
Chris@0
|
161 }
|
Chris@0
|
162 }
|
Chris@0
|
163
|
Chris@0
|
164 /**
|
Chris@0
|
165 * Returns an array of values to use as request attributes.
|
Chris@0
|
166 *
|
Chris@0
|
167 * As this method requires the Route object, it is not available
|
Chris@0
|
168 * in matchers that do not have access to the matched Route instance
|
Chris@0
|
169 * (like the PHP and Apache matcher dumpers).
|
Chris@0
|
170 *
|
Chris@0
|
171 * @param Route $route The route we are matching against
|
Chris@0
|
172 * @param string $name The name of the route
|
Chris@0
|
173 * @param array $attributes An array of attributes from the matcher
|
Chris@0
|
174 *
|
Chris@0
|
175 * @return array An array of parameters
|
Chris@0
|
176 */
|
Chris@0
|
177 protected function getAttributes(Route $route, $name, array $attributes)
|
Chris@0
|
178 {
|
Chris@0
|
179 $attributes['_route'] = $name;
|
Chris@0
|
180
|
Chris@0
|
181 return $this->mergeDefaults($attributes, $route->getDefaults());
|
Chris@0
|
182 }
|
Chris@0
|
183
|
Chris@0
|
184 /**
|
Chris@0
|
185 * Handles specific route requirements.
|
Chris@0
|
186 *
|
Chris@0
|
187 * @param string $pathinfo The path
|
Chris@0
|
188 * @param string $name The route name
|
Chris@0
|
189 * @param Route $route The route
|
Chris@0
|
190 *
|
Chris@0
|
191 * @return array The first element represents the status, the second contains additional information
|
Chris@0
|
192 */
|
Chris@0
|
193 protected function handleRouteRequirements($pathinfo, $name, Route $route)
|
Chris@0
|
194 {
|
Chris@0
|
195 // expression condition
|
Chris@12
|
196 if ($route->getCondition() && !$this->getExpressionLanguage()->evaluate($route->getCondition(), array('context' => $this->context, 'request' => $this->request ?: $this->createRequest($pathinfo)))) {
|
Chris@0
|
197 return array(self::REQUIREMENT_MISMATCH, null);
|
Chris@0
|
198 }
|
Chris@0
|
199
|
Chris@0
|
200 // check HTTP scheme requirement
|
Chris@0
|
201 $scheme = $this->context->getScheme();
|
Chris@0
|
202 $status = $route->getSchemes() && !$route->hasScheme($scheme) ? self::REQUIREMENT_MISMATCH : self::REQUIREMENT_MATCH;
|
Chris@0
|
203
|
Chris@0
|
204 return array($status, null);
|
Chris@0
|
205 }
|
Chris@0
|
206
|
Chris@0
|
207 /**
|
Chris@0
|
208 * Get merged default parameters.
|
Chris@0
|
209 *
|
Chris@0
|
210 * @param array $params The parameters
|
Chris@0
|
211 * @param array $defaults The defaults
|
Chris@0
|
212 *
|
Chris@0
|
213 * @return array Merged default parameters
|
Chris@0
|
214 */
|
Chris@0
|
215 protected function mergeDefaults($params, $defaults)
|
Chris@0
|
216 {
|
Chris@0
|
217 foreach ($params as $key => $value) {
|
Chris@0
|
218 if (!is_int($key)) {
|
Chris@0
|
219 $defaults[$key] = $value;
|
Chris@0
|
220 }
|
Chris@0
|
221 }
|
Chris@0
|
222
|
Chris@0
|
223 return $defaults;
|
Chris@0
|
224 }
|
Chris@0
|
225
|
Chris@0
|
226 protected function getExpressionLanguage()
|
Chris@0
|
227 {
|
Chris@0
|
228 if (null === $this->expressionLanguage) {
|
Chris@0
|
229 if (!class_exists('Symfony\Component\ExpressionLanguage\ExpressionLanguage')) {
|
Chris@0
|
230 throw new \RuntimeException('Unable to use expressions as the Symfony ExpressionLanguage component is not installed.');
|
Chris@0
|
231 }
|
Chris@0
|
232 $this->expressionLanguage = new ExpressionLanguage(null, $this->expressionLanguageProviders);
|
Chris@0
|
233 }
|
Chris@0
|
234
|
Chris@0
|
235 return $this->expressionLanguage;
|
Chris@0
|
236 }
|
Chris@12
|
237
|
Chris@12
|
238 /**
|
Chris@12
|
239 * @internal
|
Chris@12
|
240 */
|
Chris@12
|
241 protected function createRequest($pathinfo)
|
Chris@12
|
242 {
|
Chris@12
|
243 if (!class_exists('Symfony\Component\HttpFoundation\Request')) {
|
Chris@12
|
244 return null;
|
Chris@12
|
245 }
|
Chris@12
|
246
|
Chris@12
|
247 return Request::create($this->context->getScheme().'://'.$this->context->getHost().$this->context->getBaseUrl().$pathinfo, $this->context->getMethod(), $this->context->getParameters(), array(), array(), array(
|
Chris@12
|
248 'SCRIPT_FILENAME' => $this->context->getBaseUrl(),
|
Chris@12
|
249 'SCRIPT_NAME' => $this->context->getBaseUrl(),
|
Chris@12
|
250 ));
|
Chris@12
|
251 }
|
Chris@0
|
252 }
|