[Routing] removed code that was not ready to be pushed

This commit is contained in:
Fabien Potencier 2012-03-30 20:36:59 +02:00
parent 5178e76d4e
commit 0c7b2911bc
2 changed files with 0 additions and 285 deletions

View File

@ -1,140 +0,0 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Routing\Matcher;
use Symfony\Component\Routing\Exception\MethodNotAllowedException;
use Symfony\Component\Routing\Exception\ResourceNotFoundException;
use Symfony\Component\Routing\RequestContext;
/**
* @author Fabien Potencier <fabien@symfony.com>
*/
class ArrayUrlMatcher implements UrlMatcherInterface
{
protected $context;
protected $allow;
private $routes;
/**
* Constructor.
*
* @param array $routes An array of routes as generated by PhpArrayMatcherDumper
* @param RequestContext $context The context
*
* @api
*/
public function __construct(array $routes, RequestContext $context)
{
$this->routes = $routes;
$this->context = $context;
}
/**
* Sets the request context.
*
* @param RequestContext $context The context
*
* @api
*/
public function setContext(RequestContext $context)
{
$this->context = $context;
}
/**
* Gets the request context.
*
* @return RequestContext The context
*/
public function getContext()
{
return $this->context;
}
/**
* {@inheritDoc}
*
* @api
*/
public function match($pathinfo)
{
$this->allow = array();
$pathinfo = urldecode($pathinfo);
foreach ($this->routes as $prefix => $route) {
if (!is_int($prefix)) {
if (0 !== strpos($pathinfo, $prefix)) {
continue;
}
foreach ($route as $r) {
if ($ret = $this->matchRoute($r)) {
return $ret;
}
}
} elseif ($ret = $this->matchRoute($route)) {
return $ret;
}
}
throw 0 < count($this->allow)
? new MethodNotAllowedException(array_unique(array_map('strtoupper', $this->allow)))
: new ResourceNotFoundException();
}
public function matchRoute($route)
{
if (isset($route['methods']) && !in_array($this->context->getMethod(), $route['methods'])) {
$this->allow = array_merge($this->allow, $route['methods']);
return;
}
if (isset($route['scheme']) && $this->context->getScheme() != $route['scheme']) {
return;
}
if (isset($route['static'])) {
if ($pathinfo === $route['static']) {
$matches = isset($route['defaults']) ? $route['defaults'] : array();
$matches['_route'] = $route['name'];
return $matches;
}
return;
}
if (preg_match($route['regex'], $pathinfo, $matches)) {
$matches['_route'] = $route['name'];
if (isset($route('defaults'))) {
$matches = $this->mergeDefaults($matches, $route['defaults']);
}
return $matches;
}
}
protected function mergeDefaults($params, $defaults)
{
$parameters = $defaults;
foreach ($params as $key => $value) {
if (!is_int($key)) {
$parameters[$key] = rawurldecode($value);
}
}
return $parameters;
}
}

View File

@ -1,145 +0,0 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Routing\Matcher;
use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\RouteCollection;
/**
* RouteCollection creates a PHP array representation of a RouteCollection instance.
*
* This representation is used by Matcher instances.
*
* @author Fabien Potencier <fabien@symfony.com>
*/
class RouteCollectionCompiler
{
/**
* Compiles a RouteCollection instance.
*
* @param RouteCollection $routes A RouteCollection instance
*
* @return array A PHP array representing the matcher data
*/
public function compile(RouteCollection $routes)
{
// we need to deep clone the routes as we modify the structure to optimize the dump
return $this->compileRoutes(clone $routes);
}
private function compileRoutes(RouteCollection $routes, $parentPrefix = null)
{
$code = array();
$routeIterator = $routes->getIterator();
$keys = array_keys($routeIterator->getArrayCopy());
$keysCount = count($keys);
$i = 0;
foreach ($routeIterator as $name => $route) {
$i++;
if (!$route instanceof RouteCollection) {
$code[] = $this->compileRoute($route, $name, $parentPrefix);
continue;
}
$prefix = $route->getPrefix();
$optimizable = $prefix && count($route->all()) > 1 && false === strpos($route->getPrefix(), '{');
if (!$optimizable) {
$code = array_merge($code, $this->compileRoutes($route, $prefix));
continue;
}
for ($j = $i; $j < $keysCount; $j++) {
if (null === $keys[$j]) {
continue;
}
$testRoute = $routeIterator->offsetGet($keys[$j]);
$testPrefix = $testRoute instanceof RouteCollection ? $testRoute->getPrefix() : $testRoute->getPattern();
if (0 === strpos($testPrefix, $prefix)) {
$routeIterator->offsetUnset($keys[$j]);
if ($testRoute instanceof RouteCollection) {
$route->addCollection($testRoute);
} else {
$route->add($keys[$j], $testRoute);
}
$i++;
$keys[$j] = null;
}
}
if ($prefix !== $parentPrefix) {
$code[$prefix] = $this->compileRoutes($route, $prefix);
}
}
return $code;
}
private function compileRoute(Route $route, $name, $parentPrefix = null)
{
$compiledRoute = $route->compile();
$methods = array();
if ($req = $route->getRequirement('_method')) {
$methods = explode('|', strtoupper($req));
// GET and HEAD are equivalent
if (in_array('GET', $methods) && !in_array('HEAD', $methods)) {
$methods[] = 'HEAD';
}
}
$supportsTrailingSlash = !$methods || in_array('HEAD', $methods);
$dump = array(
'name' => $name,
);
if ($methods) {
$dump['methods'] = $methods;
}
if ($route->getRequirement('_scheme')) {
$dump['scheme'] = $route->getRequirement('_scheme');
}
if ($route->getDefaults()) {
$dump['defaults'] = $route->getDefaults();
}
if (!count($compiledRoute->getVariables()) && false !== preg_match('#^(.)\^(?P<url>.*?)\$\1#', str_replace(array("\n", ' '), '', $compiledRoute->getRegex()), $m)) {
$dump['static'] = true;
$dump['prefix'] = str_replace('\\', '', $m['url']);
if ($supportsTrailingSlash && '/' === substr($dump['prefix'], -1)) {
$dump['prefix'] = rtrim($dump['prefix']);
$dump['trailing_slash'] = true;
}
} else {
if ($compiledRoute->getStaticPrefix() && $compiledRoute->getStaticPrefix() != $parentPrefix) {
$dump['prefix'] = $compiledRoute->getStaticPrefix();
}
$regex = str_replace(array("\n", ' '), '', $compiledRoute->getRegex());
if ($supportsTrailingSlash && $pos = strpos($regex, '/$')) {
$regex = substr($regex, 0, $pos).'/?$'.substr($regex, $pos + 2);
$dump['trailing_slash'] = true;
}
$dump['regex'] = $regex;
}
return $dump;
}
}