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

112 lines
3.1 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\Route;
use Symfony\Component\Routing\RouteCollection;
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 $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
{
$this->routes = $routes;
$this->context = $context;
$this->defaults = $defaults;
2010-02-17 13:53:31 +00:00
}
/**
* Sets the request context.
*
* @param array $context The context
*/
public function setContext(array $context = array())
{
$this->context = $context;
}
/**
* 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
{
$url = $this->normalizeUrl($url);
2010-02-17 13:53:31 +00:00
foreach ($this->routes->all() as $name => $route) {
$compiledRoute = $route->compile();
2010-02-17 13:53:31 +00:00
// check HTTP method requirement
if (isset($this->context['method']) && (($req = $route->getRequirement('_method')) && !preg_match(sprintf('#^(%s)$#xi', $req), $this->context['method']))) {
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
}
protected function mergeDefaults($params, $defaults)
2010-02-17 13:53:31 +00:00
{
$parameters = array_merge($this->defaults, $defaults);
foreach ($params as $key => $value) {
if (!is_int($key)) {
// / are double-encoded as %2F is not valid in a URL (see UrlGenerator)
$parameters[$key] = str_replace('%2F', '/', urldecode($value));
}
}
return $parameters;
2010-02-17 13:53:31 +00:00
}
protected function normalizeUrl($url)
{
// remove the query string
if (false !== $pos = strpos($url, '?')) {
$url = substr($url, 0, $pos);
}
return $url;
}
2010-02-17 13:53:31 +00:00
}