2010-02-17 13:53:31 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
2011-01-15 13:29:43 +00:00
|
|
|
* This file is part of the Symfony package.
|
2010-02-17 13:53:31 +00:00
|
|
|
*
|
2011-03-06 11:40:06 +00:00
|
|
|
* (c) Fabien Potencier <fabien@symfony.com>
|
2010-02-17 13:53:31 +00:00
|
|
|
*
|
2011-01-15 13:29:43 +00:00
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
2010-02-17 13:53:31 +00:00
|
|
|
*/
|
|
|
|
|
2011-01-15 13:29:43 +00:00
|
|
|
namespace Symfony\Component\Routing\Matcher;
|
|
|
|
|
2011-05-17 09:11:27 +01:00
|
|
|
use Symfony\Component\Routing\Exception\MethodNotAllowedException;
|
2011-05-17 15:51:56 +01:00
|
|
|
use Symfony\Component\Routing\Exception\ResourceNotFoundException;
|
2011-01-15 13:29:43 +00:00
|
|
|
use Symfony\Component\Routing\RouteCollection;
|
2011-04-20 12:54:39 +01:00
|
|
|
use Symfony\Component\Routing\RequestContext;
|
2012-02-07 16:15:28 +00:00
|
|
|
use Symfony\Component\Routing\Route;
|
2011-01-15 13:29:43 +00:00
|
|
|
|
2010-02-17 13:53:31 +00:00
|
|
|
/**
|
|
|
|
* UrlMatcher matches URL based on a set of routes.
|
|
|
|
*
|
2011-03-06 11:40:06 +00:00
|
|
|
* @author Fabien Potencier <fabien@symfony.com>
|
2011-06-14 14:35:32 +01:00
|
|
|
*
|
|
|
|
* @api
|
2010-02-17 13:53:31 +00:00
|
|
|
*/
|
|
|
|
class UrlMatcher implements UrlMatcherInterface
|
|
|
|
{
|
2012-02-07 16:15:28 +00:00
|
|
|
const REQUIREMENT_MATCH = 0;
|
|
|
|
const REQUIREMENT_MISMATCH = 1;
|
|
|
|
const ROUTE_MATCH = 2;
|
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
protected $context;
|
2012-02-07 16:43:55 +00:00
|
|
|
protected $allow;
|
2010-05-06 12:25:53 +01:00
|
|
|
|
2011-03-23 18:24:18 +00:00
|
|
|
private $routes;
|
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
/**
|
|
|
|
* Constructor.
|
|
|
|
*
|
2011-04-23 16:05:44 +01:00
|
|
|
* @param RouteCollection $routes A RouteCollection instance
|
|
|
|
* @param RequestContext $context The context
|
2011-06-14 14:35:32 +01:00
|
|
|
*
|
|
|
|
* @api
|
2010-05-06 12:25:53 +01:00
|
|
|
*/
|
2011-04-20 21:49:56 +01:00
|
|
|
public function __construct(RouteCollection $routes, RequestContext $context)
|
2010-02-17 13:53:31 +00:00
|
|
|
{
|
2010-05-06 12:25:53 +01:00
|
|
|
$this->routes = $routes;
|
|
|
|
$this->context = $context;
|
2010-02-17 13:53:31 +00:00
|
|
|
}
|
|
|
|
|
2011-01-24 15:59:32 +00:00
|
|
|
/**
|
|
|
|
* Sets the request context.
|
|
|
|
*
|
2011-04-20 12:54:39 +01:00
|
|
|
* @param RequestContext $context The context
|
2011-06-14 14:35:32 +01:00
|
|
|
*
|
|
|
|
* @api
|
2011-01-24 15:59:32 +00:00
|
|
|
*/
|
2011-04-20 12:54:39 +01:00
|
|
|
public function setContext(RequestContext $context)
|
2011-01-24 15:59:32 +00:00
|
|
|
{
|
|
|
|
$this->context = $context;
|
|
|
|
}
|
|
|
|
|
2011-04-21 20:20:27 +01:00
|
|
|
/**
|
|
|
|
* Gets the request context.
|
|
|
|
*
|
|
|
|
* @return RequestContext The context
|
|
|
|
*/
|
|
|
|
public function getContext()
|
|
|
|
{
|
|
|
|
return $this->context;
|
|
|
|
}
|
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
/**
|
2012-03-19 14:59:52 +00:00
|
|
|
* Tries to match a URL with a set of routes.
|
|
|
|
*
|
|
|
|
* @param string $pathinfo The path info to be parsed (raw format, i.e. not urldecoded)
|
|
|
|
*
|
|
|
|
* @return array An array of parameters
|
|
|
|
*
|
|
|
|
* @throws ResourceNotFoundException If the resource could not be found
|
|
|
|
* @throws MethodNotAllowedException If the resource was found but the request method is not allowed
|
2011-06-14 14:35:32 +01:00
|
|
|
*
|
|
|
|
* @api
|
2010-05-06 12:25:53 +01:00
|
|
|
*/
|
2011-03-09 16:25:44 +00:00
|
|
|
public function match($pathinfo)
|
2010-02-17 13:53:31 +00:00
|
|
|
{
|
2011-04-25 16:39:10 +01:00
|
|
|
$this->allow = array();
|
|
|
|
|
2012-03-19 14:59:52 +00:00
|
|
|
if ($ret = $this->matchCollection(rawurldecode($pathinfo), $this->routes)) {
|
2011-04-25 16:39:10 +01:00
|
|
|
return $ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
throw 0 < count($this->allow)
|
2011-06-04 17:59:57 +01:00
|
|
|
? new MethodNotAllowedException(array_unique(array_map('strtoupper', $this->allow)))
|
2011-05-17 15:51:56 +01:00
|
|
|
: new ResourceNotFoundException();
|
2011-04-25 16:39:10 +01:00
|
|
|
}
|
|
|
|
|
2012-02-07 16:15:28 +00:00
|
|
|
/**
|
|
|
|
* Tries to match a URL with a set of routes.
|
|
|
|
*
|
|
|
|
* @param string $pathinfo The path info to be parsed
|
|
|
|
* @param RouteCollection $routes The set of routes
|
|
|
|
*
|
|
|
|
* @return array An array of parameters
|
|
|
|
*
|
|
|
|
* @throws ResourceNotFoundException If the resource could not be found
|
|
|
|
* @throws MethodNotAllowedException If the resource was found but the request method is not allowed
|
|
|
|
*/
|
2011-04-25 16:39:10 +01:00
|
|
|
protected function matchCollection($pathinfo, RouteCollection $routes)
|
|
|
|
{
|
|
|
|
foreach ($routes as $name => $route) {
|
|
|
|
if ($route instanceof RouteCollection) {
|
2011-06-04 18:44:04 +01:00
|
|
|
if (false === strpos($route->getPrefix(), '{') && $route->getPrefix() !== substr($pathinfo, 0, strlen($route->getPrefix()))) {
|
2011-04-25 16:39:10 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$ret = $this->matchCollection($pathinfo, $route)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $ret;
|
|
|
|
}
|
2011-03-21 12:56:53 +00:00
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
$compiledRoute = $route->compile();
|
2010-02-17 13:53:31 +00:00
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
// check the static prefix of the URL first. Only use the more expensive preg_match when it matches
|
2011-03-09 16:25:44 +00:00
|
|
|
if ('' !== $compiledRoute->getStaticPrefix() && 0 !== strpos($pathinfo, $compiledRoute->getStaticPrefix())) {
|
2010-05-06 12:25:53 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2011-03-09 16:25:44 +00:00
|
|
|
if (!preg_match($compiledRoute->getRegex(), $pathinfo, $matches)) {
|
2010-05-06 12:25:53 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2011-03-21 12:56:53 +00:00
|
|
|
// check HTTP method requirement
|
2011-06-04 11:28:01 +01:00
|
|
|
if ($req = $route->getRequirement('_method')) {
|
|
|
|
// HEAD and GET are equivalent as per RFC
|
2011-06-04 17:59:57 +01:00
|
|
|
if ('HEAD' === $method = $this->context->getMethod()) {
|
|
|
|
$method = 'GET';
|
2011-06-04 11:28:01 +01:00
|
|
|
}
|
2011-04-25 16:39:10 +01:00
|
|
|
|
2011-06-04 17:59:57 +01:00
|
|
|
if (!in_array($method, $req = explode('|', strtoupper($req)))) {
|
2011-06-04 11:28:01 +01:00
|
|
|
$this->allow = array_merge($this->allow, $req);
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
2011-03-21 12:56:53 +00:00
|
|
|
}
|
|
|
|
|
2012-02-07 16:15:28 +00:00
|
|
|
$status = $this->handleRouteRequirements($pathinfo, $name, $route);
|
2011-10-22 07:04:10 +01:00
|
|
|
|
2012-02-07 16:15:28 +00:00
|
|
|
if (self::ROUTE_MATCH === $status[0]) {
|
|
|
|
return $status[1];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (self::REQUIREMENT_MISMATCH === $status[0]) {
|
|
|
|
continue;
|
2011-10-22 07:04:10 +01:00
|
|
|
}
|
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
return array_merge($this->mergeDefaults($matches, $route->getDefaults()), array('_route' => $name));
|
|
|
|
}
|
2010-02-17 13:53:31 +00:00
|
|
|
}
|
|
|
|
|
2012-02-07 16:15:28 +00:00
|
|
|
/**
|
|
|
|
* Handles specific route requirements.
|
|
|
|
*
|
|
|
|
* @param string $pathinfo The path
|
|
|
|
* @param string $name The route name
|
|
|
|
* @param string $route The route
|
|
|
|
*
|
|
|
|
* @return array The first element represents the status, the second contains additional information
|
|
|
|
*/
|
|
|
|
protected function handleRouteRequirements($pathinfo, $name, Route $route)
|
|
|
|
{
|
|
|
|
// check HTTP scheme requirement
|
|
|
|
$scheme = $route->getRequirement('_scheme');
|
|
|
|
$status = $scheme && $scheme !== $this->context->getScheme() ? self::REQUIREMENT_MISMATCH : self::REQUIREMENT_MATCH;
|
|
|
|
|
|
|
|
return array($status, null);
|
|
|
|
}
|
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
protected function mergeDefaults($params, $defaults)
|
2010-02-17 13:53:31 +00:00
|
|
|
{
|
2011-04-20 21:49:56 +01:00
|
|
|
$parameters = $defaults;
|
2010-05-07 15:09:11 +01:00
|
|
|
foreach ($params as $key => $value) {
|
2010-05-08 14:32:30 +01:00
|
|
|
if (!is_int($key)) {
|
2012-03-19 14:59:52 +00:00
|
|
|
$parameters[$key] = $value;
|
2010-05-06 12:25:53 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $parameters;
|
2010-02-17 13:53:31 +00:00
|
|
|
}
|
|
|
|
}
|