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-03-21 12:56:53 +00:00
|
|
|
use Symfony\Component\Routing\Matcher\Exception\MethodNotAllowedException;
|
|
|
|
use Symfony\Component\Routing\Matcher\Exception\NotFoundException;
|
2011-01-15 13:29:43 +00:00
|
|
|
use Symfony\Component\Routing\Route;
|
|
|
|
use Symfony\Component\Routing\RouteCollection;
|
|
|
|
|
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>
|
2010-02-17 13:53:31 +00:00
|
|
|
*/
|
|
|
|
class UrlMatcher implements UrlMatcherInterface
|
|
|
|
{
|
2010-05-06 12:25:53 +01:00
|
|
|
protected $defaults;
|
|
|
|
protected $context;
|
|
|
|
|
2011-03-23 18:24:18 +00:00
|
|
|
private $routes;
|
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
/**
|
|
|
|
* Constructor.
|
|
|
|
*
|
|
|
|
* @param RouteCollection $routes A RouteCollection instance
|
|
|
|
* @param array $context The context
|
|
|
|
* @param array $defaults The default values
|
|
|
|
*/
|
|
|
|
public function __construct(RouteCollection $routes, array $context = array(), array $defaults = array())
|
2010-02-17 13:53:31 +00:00
|
|
|
{
|
2010-05-06 12:25:53 +01:00
|
|
|
$this->routes = $routes;
|
|
|
|
$this->context = $context;
|
|
|
|
$this->defaults = $defaults;
|
2010-02-17 13:53:31 +00:00
|
|
|
}
|
|
|
|
|
2011-01-24 15:59:32 +00:00
|
|
|
/**
|
|
|
|
* Sets the request context.
|
|
|
|
*
|
|
|
|
* @param array $context The context
|
|
|
|
*/
|
|
|
|
public function setContext(array $context = array())
|
|
|
|
{
|
|
|
|
$this->context = $context;
|
|
|
|
}
|
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
/**
|
|
|
|
* Tries to match a URL with a set of routes.
|
|
|
|
*
|
2011-03-09 16:25:44 +00:00
|
|
|
* @param string $pathinfo The path info to be parsed
|
2010-05-06 12:25:53 +01:00
|
|
|
*
|
2011-03-21 12:56:53 +00:00
|
|
|
* @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
|
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-03-21 12:56:53 +00:00
|
|
|
$allow = array();
|
|
|
|
|
2010-11-25 19:38:04 +00:00
|
|
|
foreach ($this->routes->all() 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;
|
|
|
|
}
|
|
|
|
|
2011-03-21 12:56:53 +00:00
|
|
|
// check HTTP method requirement
|
2011-03-22 19:56:55 +00:00
|
|
|
if (isset($this->context['method']) && $route->getRequirement('_method') && ($req = explode('|', $route->getRequirement('_method'))) && !in_array(strtolower($this->context['method']), array_map('strtolower', $req))) {
|
2011-03-21 12:56:53 +00:00
|
|
|
$allow = array_merge($allow, $req);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
return array_merge($this->mergeDefaults($matches, $route->getDefaults()), array('_route' => $name));
|
|
|
|
}
|
|
|
|
|
2011-03-21 12:56:53 +00:00
|
|
|
throw 0 < count($allow)
|
|
|
|
? new MethodNotAllowedException(array_unique(array_map('strtolower', $allow)))
|
|
|
|
: new NotFoundException();
|
2010-02-17 13:53:31 +00:00
|
|
|
}
|
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
protected function mergeDefaults($params, $defaults)
|
2010-02-17 13:53:31 +00:00
|
|
|
{
|
2010-05-06 12:25:53 +01:00
|
|
|
$parameters = array_merge($this->defaults, $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)) {
|
2011-03-09 22:57:26 +00:00
|
|
|
$parameters[$key] = urldecode($value);
|
2010-05-06 12:25:53 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $parameters;
|
2010-02-17 13:53:31 +00:00
|
|
|
}
|
|
|
|
}
|