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
|
|
|
*
|
|
|
|
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
|
|
|
|
*
|
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;
|
|
|
|
|
|
|
|
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.
|
|
|
|
*
|
2010-10-17 12:45:15 +01:00
|
|
|
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
|
2010-02-17 13:53:31 +00:00
|
|
|
*/
|
|
|
|
class UrlMatcher implements UrlMatcherInterface
|
|
|
|
{
|
2010-05-06 12:25:53 +01:00
|
|
|
protected $routes;
|
|
|
|
protected $defaults;
|
|
|
|
protected $context;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
}
|
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
/**
|
|
|
|
* Tries to match a URL with a set of routes.
|
|
|
|
*
|
|
|
|
* Returns false if no route matches the URL.
|
|
|
|
*
|
|
|
|
* @param string $url URL to be parsed
|
|
|
|
*
|
|
|
|
* @return array|false An array of parameters or false if no route matches
|
|
|
|
*/
|
|
|
|
public function match($url)
|
2010-02-17 13:53:31 +00:00
|
|
|
{
|
2010-05-06 12:25:53 +01:00
|
|
|
$url = $this->normalizeUrl($url);
|
2010-02-17 13:53:31 +00:00
|
|
|
|
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 HTTP method requirement
|
2010-11-25 14:49:33 +00:00
|
|
|
|
|
|
|
if (isset($this->context['method']) && (($req = $route->getRequirement('_method')) && !preg_match(sprintf('#^(%s)$#xi', $req), $this->context['method']))) {
|
2010-05-06 12:25:53 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// check the static prefix of the URL first. Only use the more expensive preg_match when it matches
|
2010-05-07 15:09:11 +01:00
|
|
|
if ('' !== $compiledRoute->getStaticPrefix() && 0 !== strpos($url, $compiledRoute->getStaticPrefix())) {
|
2010-05-06 12:25:53 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2010-05-07 15:09:11 +01:00
|
|
|
if (!preg_match($compiledRoute->getRegex(), $url, $matches)) {
|
2010-05-06 12:25:53 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
return array_merge($this->mergeDefaults($matches, $route->getDefaults()), array('_route' => $name));
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
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)) {
|
2010-05-06 12:25:53 +01:00
|
|
|
$parameters[$key] = urldecode($value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $parameters;
|
2010-02-17 13:53:31 +00:00
|
|
|
}
|
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
protected function normalizeUrl($url)
|
|
|
|
{
|
|
|
|
// ensure that the URL starts with a /
|
2010-05-07 15:09:11 +01:00
|
|
|
if ('/' !== substr($url, 0, 1)) {
|
2010-05-06 12:25:53 +01:00
|
|
|
$url = '/'.$url;
|
|
|
|
}
|
|
|
|
|
|
|
|
// remove the query string
|
2010-05-07 15:09:11 +01:00
|
|
|
if (false !== $pos = strpos($url, '?')) {
|
2010-05-06 12:25:53 +01:00
|
|
|
$url = substr($url, 0, $pos);
|
|
|
|
}
|
|
|
|
|
|
|
|
// remove multiple /
|
|
|
|
return preg_replace('#/+#', '/', $url);
|
|
|
|
}
|
2010-02-17 13:53:31 +00:00
|
|
|
}
|