This repository has been archived on 2023-08-20. You can view files and clone it, but cannot push or open issues or pull requests.
symfony/src/Symfony/Component/Routing/Matcher/UrlMatcher.php

135 lines
3.8 KiB
PHP
Raw Normal View History

2010-02-17 13:53:31 +00:00
<?php
/*
* This file is part of the Symfony package.
2010-02-17 13:53:31 +00:00
*
* (c) Fabien Potencier <fabien@symfony.com>
2010-02-17 13:53:31 +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
*/
namespace Symfony\Component\Routing\Matcher;
use Symfony\Component\Routing\Matcher\Exception\MethodNotAllowedException;
use Symfony\Component\Routing\Matcher\Exception\NotFoundException;
use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\RouteCollection;
use Symfony\Component\Routing\RequestContext;
2010-02-17 13:53:31 +00:00
/**
* UrlMatcher matches URL based on a set of routes.
*
* @author Fabien Potencier <fabien@symfony.com>
2010-02-17 13:53:31 +00:00
*/
class UrlMatcher implements UrlMatcherInterface
{
protected $context;
2011-03-23 18:24:18 +00:00
private $routes;
/**
* Constructor.
*
2011-04-23 16:05:44 +01:00
* @param RouteCollection $routes A RouteCollection instance
* @param RequestContext $context The context
*/
public function __construct(RouteCollection $routes, RequestContext $context)
2010-02-17 13:53:31 +00:00
{
$this->routes = $routes;
$this->context = $context;
2010-02-17 13:53:31 +00:00
}
/**
* Sets the request context.
*
* @param RequestContext $context The context
*/
public function setContext(RequestContext $context)
{
$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;
}
/**
* Tries to match a URL with a set of routes.
*
* @param string $pathinfo The path info to be parsed
*
* @return array An array of parameters
*
* @throws NotFoundException If the resource could not be found
* @throws MethodNotAllowedException If the resource was found but the request method is not allowed
*/
public function match($pathinfo)
2010-02-17 13:53:31 +00:00
{
2011-04-25 16:39:10 +01:00
$this->allow = array();
if ($ret = $this->matchCollection($pathinfo, $this->routes)) {
return $ret;
}
throw 0 < count($this->allow)
? new MethodNotAllowedException(array_unique(array_map('strtolower', $this->allow)))
: new NotFoundException();
}
protected function matchCollection($pathinfo, RouteCollection $routes)
{
foreach ($routes as $name => $route) {
if ($route instanceof RouteCollection) {
if ($route->getPrefix() !== substr($pathinfo, 0, strlen($route->getPrefix()))) {
continue;
}
if (!$ret = $this->matchCollection($pathinfo, $route)) {
continue;
}
return $ret;
}
$compiledRoute = $route->compile();
2010-02-17 13:53:31 +00:00
// 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 ($route->getRequirement('_method') && ($req = explode('|', $route->getRequirement('_method'))) && !in_array($this->context->getMethod(), array_map('strtolower', $req))) {
2011-04-25 16:39:10 +01:00
$this->allow = array_merge($this->allow, $req);
continue;
}
return array_merge($this->mergeDefaults($matches, $route->getDefaults()), array('_route' => $name));
}
2010-02-17 13:53:31 +00:00
}
protected function mergeDefaults($params, $defaults)
2010-02-17 13:53:31 +00:00
{
$parameters = $defaults;
foreach ($params as $key => $value) {
if (!is_int($key)) {
$parameters[$key] = urldecode($value);
}
}
return $parameters;
2010-02-17 13:53:31 +00:00
}
}