2010-02-17 13:53:31 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Symfony\Components\Routing\Matcher;
|
|
|
|
|
|
|
|
use Symfony\Components\Routing\Route;
|
|
|
|
use Symfony\Components\Routing\RouteCollection;
|
|
|
|
|
|
|
|
/*
|
2010-04-07 01:51:29 +01:00
|
|
|
* This file is part of the Symfony framework.
|
2010-02-17 13:53:31 +00:00
|
|
|
*
|
|
|
|
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
|
|
|
|
*
|
|
|
|
* This source file is subject to the MIT license that is bundled
|
|
|
|
* with this source code in the file LICENSE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* UrlMatcher matches URL based on a set of routes.
|
|
|
|
*
|
2010-04-07 01:51:29 +01:00
|
|
|
* @package Symfony
|
|
|
|
* @subpackage Components_Routing
|
2010-02-17 13:53:31 +00:00
|
|
|
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
|
|
|
|
*/
|
|
|
|
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-05-06 12:25:53 +01:00
|
|
|
foreach ($this->routes->getRoutes() as $name => $route)
|
|
|
|
{
|
|
|
|
$compiledRoute = $route->compile();
|
2010-02-17 13:53:31 +00:00
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
// check HTTP method requirement
|
|
|
|
if (isset($this->context['method']) && (($req = $route->getRequirement('_method')) && !in_array(strtolower($this->context['method']), array_map('strtolower', (array) $req))))
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// check the static prefix of the URL first. Only use the more expensive preg_match when it matches
|
|
|
|
if ('' !== $compiledRoute->getStaticPrefix() && 0 !== strpos($url, $compiledRoute->getStaticPrefix()))
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!preg_match($compiledRoute->getRegex(), $url, $matches))
|
|
|
|
{
|
|
|
|
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);
|
|
|
|
foreach ($params as $key => $value)
|
|
|
|
{
|
|
|
|
if (!is_int($key))
|
|
|
|
{
|
|
|
|
$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 /
|
|
|
|
if ('/' !== substr($url, 0, 1))
|
|
|
|
{
|
|
|
|
$url = '/'.$url;
|
|
|
|
}
|
|
|
|
|
|
|
|
// remove the query string
|
|
|
|
if (false !== $pos = strpos($url, '?'))
|
|
|
|
{
|
|
|
|
$url = substr($url, 0, $pos);
|
|
|
|
}
|
|
|
|
|
|
|
|
// remove multiple /
|
|
|
|
return preg_replace('#/+#', '/', $url);
|
|
|
|
}
|
2010-02-17 13:53:31 +00:00
|
|
|
}
|