UrlMatcher matches URL based on a set of routes.
@author Fabien Potencier <fabien@symfony.com>
@api
Expanded class hierarchy of UrlMatcher
class UrlMatcher implements UrlMatcherInterface {
const REQUIREMENT_MATCH = 0;
const REQUIREMENT_MISMATCH = 1;
const ROUTE_MATCH = 2;
protected $context;
protected $allow;
private $routes;
/**
* Constructor.
*
* @param RouteCollection $routes A RouteCollection instance
* @param RequestContext $context The context
*
* @api
*/
public function __construct(RouteCollection $routes, RequestContext $context) {
$this->routes = $routes;
$this->context = $context;
}
/**
* {@inheritdoc}
*/
public function setContext(RequestContext $context) {
$this->context = $context;
}
/**
* {@inheritdoc}
*/
public function getContext() {
return $this->context;
}
/**
* {@inheritdoc}
*/
public function match($pathinfo) {
$this->allow = array();
if ($ret = $this
->matchCollection(rawurldecode($pathinfo), $this->routes)) {
return $ret;
}
throw 0 < count($this->allow) ? new MethodNotAllowedException(array_unique(array_map('strtoupper', $this->allow))) : new ResourceNotFoundException();
}
/**
* 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
*/
protected function matchCollection($pathinfo, RouteCollection $routes) {
foreach ($routes as $name => $route) {
if ($route instanceof RouteCollection) {
if (false === strpos($route
->getPrefix(), '{') && $route
->getPrefix() !== substr($pathinfo, 0, strlen($route
->getPrefix()))) {
continue;
}
if (!($ret = $this
->matchCollection($pathinfo, $route))) {
continue;
}
return $ret;
}
$compiledRoute = $route
->compile();
// check the static prefix of the URL first. Only use the more expensive preg_match when it matches
if ('' !== $compiledRoute
->getStaticPrefix() && 0 !== strpos($pathinfo, $compiledRoute
->getStaticPrefix())) {
continue;
}
if (!preg_match($compiledRoute
->getRegex(), $pathinfo, $matches)) {
continue;
}
// check HTTP method requirement
if ($req = $route
->getRequirement('_method')) {
// HEAD and GET are equivalent as per RFC
if ('HEAD' === ($method = $this->context
->getMethod())) {
$method = 'GET';
}
if (!in_array($method, $req = explode('|', strtoupper($req)))) {
$this->allow = array_merge($this->allow, $req);
continue;
}
}
$status = $this
->handleRouteRequirements($pathinfo, $name, $route);
if (self::ROUTE_MATCH === $status[0]) {
return $status[1];
}
if (self::REQUIREMENT_MISMATCH === $status[0]) {
continue;
}
return array_merge($this
->mergeDefaults($matches, $route
->getDefaults()), array(
'_route' => $name,
));
}
}
/**
* Handles specific route requirements.
*
* @param string $pathinfo The path
* @param string $name The route name
* @param Route $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,
);
}
/**
* Get merged default parameters.
*
* @param array $params The parameters
* @param array $defaults The defaults
*
* @return array Merged default parameters
*/
protected function mergeDefaults($params, $defaults) {
$parameters = $defaults;
foreach ($params as $key => $value) {
if (!is_int($key)) {
$parameters[$key] = $value;
}
}
return $parameters;
}
}
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
UrlMatcher:: |
protected | property | ||
UrlMatcher:: |
protected | property | ||
UrlMatcher:: |
private | property | ||
UrlMatcher:: |
public | function |
Gets the request context. Overrides RequestContextAwareInterface:: |
|
UrlMatcher:: |
protected | function | Handles specific route requirements. | 1 |
UrlMatcher:: |
public | function |
Tries to match a URL path with a set of routes. Overrides UrlMatcherInterface:: |
5 |
UrlMatcher:: |
protected | function | Tries to match a URL with a set of routes. | 1 |
UrlMatcher:: |
protected | function | Get merged default parameters. | |
UrlMatcher:: |
constant | |||
UrlMatcher:: |
constant | |||
UrlMatcher:: |
constant | |||
UrlMatcher:: |
public | function |
Sets the request context. Overrides RequestContextAwareInterface:: |
|
UrlMatcher:: |
public | function | Constructor. | 3 |