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;
|
|
|
|
|
2012-11-12 15:14:50 +00:00
|
|
|
/**
|
|
|
|
* @var RequestContext
|
|
|
|
*/
|
2010-05-06 12:25:53 +01:00
|
|
|
protected $context;
|
|
|
|
|
2012-11-12 15:14:50 +00:00
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $allow = array();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var RouteCollection
|
|
|
|
*/
|
2012-11-23 08:30:05 +00:00
|
|
|
protected $routes;
|
2011-03-23 18:24:18 +00:00
|
|
|
|
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
|
|
|
/**
|
2012-05-05 00:43:00 +01:00
|
|
|
* {@inheritdoc}
|
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
|
|
|
/**
|
2012-05-05 00:43:00 +01:00
|
|
|
* {@inheritdoc}
|
2011-04-21 20:20:27 +01:00
|
|
|
*/
|
|
|
|
public function getContext()
|
|
|
|
{
|
|
|
|
return $this->context;
|
|
|
|
}
|
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
/**
|
2012-05-05 00:43:00 +01:00
|
|
|
* {@inheritdoc}
|
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) {
|
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;
|
|
|
|
}
|
2012-04-25 02:07:09 +01:00
|
|
|
|
2012-11-11 18:07:05 +00:00
|
|
|
$hostnameMatches = array();
|
2012-04-25 02:07:09 +01:00
|
|
|
if ($compiledRoute->getHostnameRegex() && !preg_match($compiledRoute->getHostnameRegex(), $this->context->getHost(), $hostnameMatches)) {
|
|
|
|
continue;
|
|
|
|
}
|
2010-05-06 12:25:53 +01:00
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2012-11-23 08:30:05 +00:00
|
|
|
return $this->getAttributes($route, $name, array_replace($matches, $hostnameMatches));
|
2010-05-06 12:25:53 +01:00
|
|
|
}
|
2010-02-17 13:53:31 +00:00
|
|
|
}
|
|
|
|
|
2012-11-23 08:30:05 +00:00
|
|
|
/**
|
2012-12-03 22:11:37 +00:00
|
|
|
* Returns an array of values to use as request attributes.
|
2012-11-23 08:30:05 +00:00
|
|
|
*
|
2012-12-03 22:11:37 +00:00
|
|
|
* As this method requires the Route object, it is not available
|
|
|
|
* in matchers that do not have access to the matched Route instance
|
|
|
|
* (like the PHP and Apache matcher dumpers).
|
|
|
|
*
|
|
|
|
* @param Route $route The route we are matching against
|
|
|
|
* @param string $name The name of the route
|
|
|
|
* @param array $attributes An array of attributes from the matcher
|
2012-11-23 08:30:05 +00:00
|
|
|
*
|
|
|
|
* @return array An array of parameters
|
|
|
|
*/
|
|
|
|
protected function getAttributes(Route $route, $name, array $attributes)
|
|
|
|
{
|
|
|
|
$attributes['_route'] = $name;
|
2012-12-03 22:11:37 +00:00
|
|
|
|
2012-11-23 08:30:05 +00:00
|
|
|
return $this->mergeDefaults($attributes, $route->getDefaults());
|
|
|
|
}
|
|
|
|
|
2012-02-07 16:15:28 +00:00
|
|
|
/**
|
|
|
|
* Handles specific route requirements.
|
|
|
|
*
|
|
|
|
* @param string $pathinfo The path
|
|
|
|
* @param string $name The route name
|
2012-07-28 17:07:17 +01:00
|
|
|
* @param Route $route The route
|
2012-02-07 16:15:28 +00:00
|
|
|
*
|
|
|
|
* @return array The first element represents the status, the second contains additional information
|
|
|
|
*/
|
|
|
|
protected function handleRouteRequirements($pathinfo, $name, Route $route)
|
|
|
|
{
|
2012-07-01 17:56:00 +01:00
|
|
|
// check HTTP scheme requirement
|
|
|
|
$scheme = $route->getRequirement('_scheme');
|
|
|
|
$status = $scheme && $scheme !== $this->context->getScheme() ? self::REQUIREMENT_MISMATCH : self::REQUIREMENT_MATCH;
|
2012-02-07 16:15:28 +00:00
|
|
|
|
2012-07-01 17:56:00 +01:00
|
|
|
return array($status, null);
|
2012-06-02 12:54:03 +01:00
|
|
|
}
|
2012-02-07 16:15:28 +00:00
|
|
|
|
2012-06-02 12:54:03 +01:00
|
|
|
/**
|
|
|
|
* Get merged default parameters.
|
2012-07-09 13:50:58 +01:00
|
|
|
*
|
2012-06-02 12:54:03 +01:00
|
|
|
* @param array $params The parameters
|
|
|
|
* @param array $defaults The defaults
|
2012-07-09 13:50:58 +01:00
|
|
|
*
|
2012-06-02 12:54:03 +01:00
|
|
|
* @return array Merged default parameters
|
|
|
|
*/
|
2010-05-06 12:25:53 +01:00
|
|
|
protected function mergeDefaults($params, $defaults)
|
2010-02-17 13:53:31 +00:00
|
|
|
{
|
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-10-26 10:26:42 +01:00
|
|
|
$defaults[$key] = $value;
|
2010-05-06 12:25:53 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-26 10:26:42 +01:00
|
|
|
return $defaults;
|
2010-02-17 13:53:31 +00:00
|
|
|
}
|
|
|
|
}
|