replaced array for request context in Routing by a RequestContext class

This commit is contained in:
Fabien Potencier 2011-04-20 13:54:39 +02:00
parent 07aae98495
commit 117321d3c6
14 changed files with 247 additions and 81 deletions

View File

@ -21,6 +21,7 @@ use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\Routing\Matcher\Exception\MethodNotAllowedException; use Symfony\Component\Routing\Matcher\Exception\MethodNotAllowedException;
use Symfony\Component\Routing\Matcher\Exception\NotFoundException; use Symfony\Component\Routing\Matcher\Exception\NotFoundException;
use Symfony\Component\Routing\RouterInterface; use Symfony\Component\Routing\RouterInterface;
use Symfony\Component\Routing\RequestContext;
/** /**
* RequestListener. * RequestListener.
@ -76,13 +77,13 @@ class RequestListener
if ($master) { if ($master) {
// set the context even if the parsing does not need to be done // set the context even if the parsing does not need to be done
// to have correct link generation // to have correct link generation
$this->router->setContext(array( $this->router->setContext(new RequestContext(
'base_url' => $request->getBaseUrl(), $request->getBaseUrl(),
'method' => $request->getMethod(), $request->getMethod(),
'host' => $request->getHost(), $request->getHost(),
'scheme' => $request->getScheme(), $request->getScheme(),
'http_port' => $this->httpPort, $this->httpPort,
'https_port' => $this->httpsPort, $this->httpsPort
)); ));
} }

View File

@ -35,8 +35,8 @@ class RedirectableUrlMatcher extends UrlMatcher implements RedirectableUrlMatche
'path' => $path, 'path' => $path,
'permanent' => true, 'permanent' => true,
'scheme' => $scheme, 'scheme' => $scheme,
'httpPort' => isset($this->context['http_port']) ? $this->context['http_port'] : 80, 'httpPort' => $this->context->getHttpPort(),
'httpsPort' => isset($this->context['https_port']) ? $this->context['https_port'] : 443, 'httpsPort' => $this->context->getHttpsPort(),
'_route' => $route, '_route' => $route,
); );
} }

View File

@ -107,6 +107,8 @@ EOF;
return <<<EOF return <<<EOF
<?php <?php
use Symfony\Component\Routing\RequestContext;
/** /**
* $class * $class
* *
@ -129,7 +131,7 @@ EOF;
/** /**
* Constructor. * Constructor.
*/ */
public function __construct(array \$context = array(), array \$defaults = array()) public function __construct(RequestContext \$context, array \$defaults = array())
{ {
\$this->context = \$context; \$this->context = \$context;
\$this->defaults = \$defaults; \$this->defaults = \$defaults;

View File

@ -13,6 +13,7 @@ namespace Symfony\Component\Routing\Generator;
use Symfony\Component\Routing\Route; use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\RouteCollection; use Symfony\Component\Routing\RouteCollection;
use Symfony\Component\Routing\RequestContext;
/** /**
* UrlGenerator generates URL based on a set of routes. * UrlGenerator generates URL based on a set of routes.
@ -31,10 +32,10 @@ class UrlGenerator implements UrlGeneratorInterface
* Constructor. * Constructor.
* *
* @param RouteCollection $routes A RouteCollection instance * @param RouteCollection $routes A RouteCollection instance
* @param array $context The context * @param RequestContext $context The context
* @param array $defaults The default values * @param array $defaults The default values
*/ */
public function __construct(RouteCollection $routes, array $context = array(), array $defaults = array()) public function __construct(RouteCollection $routes, RequestContext $context, array $defaults = array())
{ {
$this->routes = $routes; $this->routes = $routes;
$this->context = $context; $this->context = $context;
@ -45,9 +46,9 @@ class UrlGenerator implements UrlGeneratorInterface
/** /**
* Sets the request context. * Sets the request context.
* *
* @param array $context The context * @param RequestContext $context The context
*/ */
public function setContext(array $context = array()) public function setContext(RequestContext $context)
{ {
$this->context = $context; $this->context = $context;
} }
@ -127,10 +128,10 @@ class UrlGenerator implements UrlGeneratorInterface
$url .= '?'.http_build_query($extra); $url .= '?'.http_build_query($extra);
} }
$url = (isset($this->context['base_url']) ? $this->context['base_url'] : '').$url; $url = $this->context->getBaseUrl().$url;
if (isset($this->context['host'])) { if ($this->context->getHost()) {
$scheme = isset($this->context['scheme']) ? $this->context['scheme'] : 'http'; $scheme = $this->context->getScheme();
if (isset($requirements['_scheme']) && ($req = strtolower($requirements['_scheme'])) && $scheme != $req) { if (isset($requirements['_scheme']) && ($req = strtolower($requirements['_scheme'])) && $scheme != $req) {
$absolute = true; $absolute = true;
$scheme = $req; $scheme = $req;
@ -138,13 +139,13 @@ class UrlGenerator implements UrlGeneratorInterface
if ($absolute) { if ($absolute) {
$port = ''; $port = '';
if ('http' === $scheme && 80 != ($httpPort = isset($this->context['http_port']) ? $this->context['http_port'] : 80)) { if ('http' === $scheme && 80 != $this->context->getHttpPort()) {
$port = ':'.$httpPort; $port = ':'.$this->context->getHttpPort();
} elseif ('https' === $scheme && 443 != ($httpsPort = isset($this->context['https_port']) ? $this->context['https_port'] : 443)) { } elseif ('https' === $scheme && 443 != $this->context->getHttpsPort()) {
$port = ':'.$httpsPort; $port = ':'.$this->context->getHttpsPort();
} }
$url = $scheme.'://'.$this->context['host'].$port.$url; $url = $scheme.'://'.$this->context->getHost().$port.$url;
} }
} }

View File

@ -94,7 +94,7 @@ EOF;
if ($req = $route->getRequirement('_method')) { if ($req = $route->getRequirement('_method')) {
$req = implode('\', \'', array_map('strtolower', explode('|', $req))); $req = implode('\', \'', array_map('strtolower', explode('|', $req)));
$code[] = <<<EOF $code[] = <<<EOF
if (isset(\$this->context['method']) && !in_array(strtolower(\$this->context['method']), array('$req'))) { if (!in_array(\$this->context->getMethod(), array('$req'))) {
\$allow = array_merge(\$allow, array('$req')); \$allow = array_merge(\$allow, array('$req'));
goto $gotoname; goto $gotoname;
} }
@ -116,7 +116,7 @@ EOF
} }
$code[] = sprintf(<<<EOF $code[] = sprintf(<<<EOF
if (isset(\$this->context['scheme']) && \$this->context['scheme'] !== '$scheme') { if (\$this->context->getScheme() !== '$scheme') {
return \$this->redirect(\$pathinfo, '%s', '$scheme'); return \$this->redirect(\$pathinfo, '%s', '$scheme');
} }
EOF EOF
@ -165,6 +165,7 @@ EOF;
use Symfony\Component\Routing\Matcher\Exception\MethodNotAllowedException; use Symfony\Component\Routing\Matcher\Exception\MethodNotAllowedException;
use Symfony\Component\Routing\Matcher\Exception\NotFoundException; use Symfony\Component\Routing\Matcher\Exception\NotFoundException;
use Symfony\Component\Routing\RequestContext;
/** /**
* $class * $class
@ -184,7 +185,7 @@ EOF;
/** /**
* Constructor. * Constructor.
*/ */
public function __construct(array \$context = array(), array \$defaults = array()) public function __construct(RequestContext \$context, array \$defaults = array())
{ {
\$this->context = \$context; \$this->context = \$context;
\$this->defaults = \$defaults; \$this->defaults = \$defaults;

View File

@ -15,6 +15,7 @@ use Symfony\Component\Routing\Matcher\Exception\MethodNotAllowedException;
use Symfony\Component\Routing\Matcher\Exception\NotFoundException; use Symfony\Component\Routing\Matcher\Exception\NotFoundException;
use Symfony\Component\Routing\Route; use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\RouteCollection; use Symfony\Component\Routing\RouteCollection;
use Symfony\Component\Routing\RequestContext;
/** /**
* UrlMatcher matches URL based on a set of routes. * UrlMatcher matches URL based on a set of routes.
@ -32,10 +33,10 @@ class UrlMatcher implements UrlMatcherInterface
* Constructor. * Constructor.
* *
* @param RouteCollection $routes A RouteCollection instance * @param RouteCollection $routes A RouteCollection instance
* @param array $context The context * @param RequestContext $context The context
* @param array $defaults The default values * @param array $defaults The default values
*/ */
public function __construct(RouteCollection $routes, array $context = array(), array $defaults = array()) public function __construct(RouteCollection $routes, RequestContext $context, array $defaults = array())
{ {
$this->routes = $routes; $this->routes = $routes;
$this->context = $context; $this->context = $context;
@ -45,9 +46,9 @@ class UrlMatcher implements UrlMatcherInterface
/** /**
* Sets the request context. * Sets the request context.
* *
* @param array $context The context * @param RequestContext $context The context
*/ */
public function setContext(array $context = array()) public function setContext(RequestContext $context)
{ {
$this->context = $context; $this->context = $context;
} }
@ -79,7 +80,7 @@ class UrlMatcher implements UrlMatcherInterface
} }
// check HTTP method requirement // check HTTP method requirement
if (isset($this->context['method']) && $route->getRequirement('_method') && ($req = explode('|', $route->getRequirement('_method'))) && !in_array(strtolower($this->context['method']), array_map('strtolower', $req))) { if ($route->getRequirement('_method') && ($req = explode('|', $route->getRequirement('_method'))) && !in_array($this->context->getMethod(), array_map('strtolower', $req))) {
$allow = array_merge($allow, $req); $allow = array_merge($allow, $req);
continue; continue;
} }

View File

@ -0,0 +1,167 @@
<?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;
/**
* Holds information about the current request.
*
* @author Fabien Potencier <fabien@symfony.com>
*/
class RequestContext
{
private $baseUrl;
private $method;
private $host;
private $scheme;
private $httpPort;
private $httpsPort;
/**
* Constructor.
*
* @param string $baseUrl The base URL
* @param string $method The HTTP method
* @param string $host The HTTP host name
* @param string $scheme The HTTP scheme
* @param integer $httpPort The HTTP port
* @param integer $httpsPort The HTTPS port
*/
public function __construct($baseUrl = '', $method = 'get', $host = 'localhost', $scheme = 'http', $httpPort = 80, $httpsPort = 443)
{
$this->baseUrl = $baseUrl;
$this->method = strtolower($method);
$this->host = $host;
$this->scheme = strtolower($scheme);
$this->httpPort = $httpPort;
$this->httpsPort = $httpsPort;
}
/**
* Gets the base URL.
*
* @return string The base URL
*/
public function getBaseUrl()
{
return $this->baseUrl;
}
/**
* Sets the base URL.
*
* @param string $baseUrl The base URL
*/
public function setBaseUrl($baseUrl)
{
$this->baseUrl = $baseUrl;
}
/**
* Gets the HTTP method.
*
* @return string The HTTP method
*/
public function getMethod()
{
return $this->method;
}
/**
* Sets the HTTP method.
*
* @param string $method The HTTP method
*/
public function setMethod($method)
{
$this->method = strtolower($method);
}
/**
* Gets the HTTP host.
*
* @return string The HTTP host
*/
public function getHost()
{
return $this->host;
}
/**
* Sets the HTTP host.
*
* @param string $host The HTTP host
*/
public function setHost($host)
{
$this->host = $host;
}
/**
* Gets the HTTP scheme.
*
* @return string The HTTP scheme
*/
public function getScheme()
{
return $this->scheme;
}
/**
* Sets the HTTP scheme.
*
* @param string $scheme The HTTP scheme
*/
public function setScheme($scheme)
{
$this->scheme = strtolower($scheme);
}
/**
* Gets the HTTP port.
*
* @return string The HTTP port
*/
public function getHttpPort()
{
return $this->httpPort;
}
/**
* Sets the HTTP port.
*
* @param string $httpPort The HTTP port
*/
public function setHttpPort($httpPort)
{
$this->httpPort = $httpPort;
}
/**
* Gets the HTTPS port.
*
* @return string The HTTPS port
*/
public function getHttpsPort()
{
return $this->httpsPort;
}
/**
* Sets the HTTPS port.
*
* @param string $httpsPort The HTTPS port
*/
public function setHttpsPort($httpsPort)
{
$this->httpsPort = $httpsPort;
}
}

View File

@ -48,11 +48,11 @@ class Router implements RouterInterface
* *
* @throws \InvalidArgumentException When unsupported option is provided * @throws \InvalidArgumentException When unsupported option is provided
*/ */
public function __construct(LoaderInterface $loader, $resource, array $options = array(), array $context = array(), array $defaults = array()) public function __construct(LoaderInterface $loader, $resource, array $options = array(), RequestContext $context = null, array $defaults = array())
{ {
$this->loader = $loader; $this->loader = $loader;
$this->resource = $resource; $this->resource = $resource;
$this->context = $context; $this->context = null === $context ? new RequestContext() : $context;
$this->defaults = $defaults; $this->defaults = $defaults;
$this->options = array( $this->options = array(
'cache_dir' => null, 'cache_dir' => null,
@ -102,9 +102,9 @@ class Router implements RouterInterface
/** /**
* Sets the request context. * Sets the request context.
* *
* @param array $context The context * @param RequestContext $context The context
*/ */
public function setContext(array $context = array()) public function setContext(RequestContext $context)
{ {
$this->getMatcher()->setContext($context); $this->getMatcher()->setContext($context);
$this->getGenerator()->setContext($context); $this->getGenerator()->setContext($context);

View File

@ -2,6 +2,7 @@
use Symfony\Component\Routing\Matcher\Exception\MethodNotAllowedException; use Symfony\Component\Routing\Matcher\Exception\MethodNotAllowedException;
use Symfony\Component\Routing\Matcher\Exception\NotFoundException; use Symfony\Component\Routing\Matcher\Exception\NotFoundException;
use Symfony\Component\Routing\RequestContext;
/** /**
* ProjectUrlMatcher * ProjectUrlMatcher
@ -14,7 +15,7 @@ class ProjectUrlMatcher extends Symfony\Component\Routing\Matcher\UrlMatcher
/** /**
* Constructor. * Constructor.
*/ */
public function __construct(array $context = array(), array $defaults = array()) public function __construct(RequestContext $context, array $defaults = array())
{ {
$this->context = $context; $this->context = $context;
$this->defaults = $defaults; $this->defaults = $defaults;
@ -31,7 +32,7 @@ class ProjectUrlMatcher extends Symfony\Component\Routing\Matcher\UrlMatcher
// bar // bar
if (0 === strpos($pathinfo, '/bar') && preg_match('#^/bar/(?P<foo>[^/\.]+?)$#x', $pathinfo, $matches)) { if (0 === strpos($pathinfo, '/bar') && preg_match('#^/bar/(?P<foo>[^/\.]+?)$#x', $pathinfo, $matches)) {
if (isset($this->context['method']) && !in_array(strtolower($this->context['method']), array('get', 'head'))) { if (!in_array($this->context->getMethod(), array('get', 'head'))) {
$allow = array_merge($allow, array('get', 'head')); $allow = array_merge($allow, array('get', 'head'));
goto not_bar; goto not_bar;
} }
@ -63,7 +64,7 @@ class ProjectUrlMatcher extends Symfony\Component\Routing\Matcher\UrlMatcher
// baz5 // baz5
if (0 === strpos($pathinfo, '/test') && preg_match('#^/test/(?P<foo>[^/\.]+?)/$#x', $pathinfo, $matches)) { if (0 === strpos($pathinfo, '/test') && preg_match('#^/test/(?P<foo>[^/\.]+?)/$#x', $pathinfo, $matches)) {
if (isset($this->context['method']) && !in_array(strtolower($this->context['method']), array('post'))) { if (!in_array($this->context->getMethod(), array('post'))) {
$allow = array_merge($allow, array('post')); $allow = array_merge($allow, array('post'));
goto not_baz5; goto not_baz5;
} }
@ -74,7 +75,7 @@ class ProjectUrlMatcher extends Symfony\Component\Routing\Matcher\UrlMatcher
// baz.baz6 // baz.baz6
if (0 === strpos($pathinfo, '/test') && preg_match('#^/test/(?P<foo>[^/\.]+?)/$#x', $pathinfo, $matches)) { if (0 === strpos($pathinfo, '/test') && preg_match('#^/test/(?P<foo>[^/\.]+?)/$#x', $pathinfo, $matches)) {
if (isset($this->context['method']) && !in_array(strtolower($this->context['method']), array('put'))) { if (!in_array($this->context->getMethod(), array('put'))) {
$allow = array_merge($allow, array('put')); $allow = array_merge($allow, array('put'));
goto not_bazbaz6; goto not_bazbaz6;
} }

View File

@ -2,6 +2,7 @@
use Symfony\Component\Routing\Matcher\Exception\MethodNotAllowedException; use Symfony\Component\Routing\Matcher\Exception\MethodNotAllowedException;
use Symfony\Component\Routing\Matcher\Exception\NotFoundException; use Symfony\Component\Routing\Matcher\Exception\NotFoundException;
use Symfony\Component\Routing\RequestContext;
/** /**
* ProjectUrlMatcher * ProjectUrlMatcher
@ -14,7 +15,7 @@ class ProjectUrlMatcher extends Symfony\Tests\Component\Routing\Fixtures\Redirec
/** /**
* Constructor. * Constructor.
*/ */
public function __construct(array $context = array(), array $defaults = array()) public function __construct(RequestContext $context, array $defaults = array())
{ {
$this->context = $context; $this->context = $context;
$this->defaults = $defaults; $this->defaults = $defaults;
@ -31,7 +32,7 @@ class ProjectUrlMatcher extends Symfony\Tests\Component\Routing\Fixtures\Redirec
// bar // bar
if (0 === strpos($pathinfo, '/bar') && preg_match('#^/bar/(?P<foo>[^/\.]+?)$#x', $pathinfo, $matches)) { if (0 === strpos($pathinfo, '/bar') && preg_match('#^/bar/(?P<foo>[^/\.]+?)$#x', $pathinfo, $matches)) {
if (isset($this->context['method']) && !in_array(strtolower($this->context['method']), array('get', 'head'))) { if (!in_array($this->context->getMethod(), array('get', 'head'))) {
$allow = array_merge($allow, array('get', 'head')); $allow = array_merge($allow, array('get', 'head'));
goto not_bar; goto not_bar;
} }
@ -69,7 +70,7 @@ class ProjectUrlMatcher extends Symfony\Tests\Component\Routing\Fixtures\Redirec
// baz5 // baz5
if (0 === strpos($pathinfo, '/test') && preg_match('#^/test/(?P<foo>[^/\.]+?)/?$#x', $pathinfo, $matches)) { if (0 === strpos($pathinfo, '/test') && preg_match('#^/test/(?P<foo>[^/\.]+?)/?$#x', $pathinfo, $matches)) {
if (isset($this->context['method']) && !in_array(strtolower($this->context['method']), array('post'))) { if (!in_array($this->context->getMethod(), array('post'))) {
$allow = array_merge($allow, array('post')); $allow = array_merge($allow, array('post'));
goto not_baz5; goto not_baz5;
} }
@ -83,7 +84,7 @@ class ProjectUrlMatcher extends Symfony\Tests\Component\Routing\Fixtures\Redirec
// baz.baz6 // baz.baz6
if (0 === strpos($pathinfo, '/test') && preg_match('#^/test/(?P<foo>[^/\.]+?)/?$#x', $pathinfo, $matches)) { if (0 === strpos($pathinfo, '/test') && preg_match('#^/test/(?P<foo>[^/\.]+?)/?$#x', $pathinfo, $matches)) {
if (isset($this->context['method']) && !in_array(strtolower($this->context['method']), array('put'))) { if (!in_array($this->context->getMethod(), array('put'))) {
$allow = array_merge($allow, array('put')); $allow = array_merge($allow, array('put'));
goto not_bazbaz6; goto not_bazbaz6;
} }
@ -102,7 +103,7 @@ class ProjectUrlMatcher extends Symfony\Tests\Component\Routing\Fixtures\Redirec
// secure // secure
if ($pathinfo === '/secure') { if ($pathinfo === '/secure') {
if (isset($this->context['scheme']) && $this->context['scheme'] !== 'https') { if ($this->context->getScheme() !== 'https') {
return $this->redirect($pathinfo, 'secure', 'https'); return $this->redirect($pathinfo, 'secure', 'https');
} }
return array('_route' => 'secure'); return array('_route' => 'secure');
@ -110,7 +111,7 @@ class ProjectUrlMatcher extends Symfony\Tests\Component\Routing\Fixtures\Redirec
// nonsecure // nonsecure
if ($pathinfo === '/nonsecure') { if ($pathinfo === '/nonsecure') {
if (isset($this->context['scheme']) && $this->context['scheme'] !== 'http') { if ($this->context->getScheme() !== 'http') {
return $this->redirect($pathinfo, 'nonsecure', 'http'); return $this->redirect($pathinfo, 'nonsecure', 'http');
} }
return array('_route' => 'nonsecure'); return array('_route' => 'nonsecure');

View File

@ -14,6 +14,7 @@ namespace Symfony\Tests\Component\Routing\Generator\Dumper\PhpGeneratorDumper;
use Symfony\Component\Routing\RouteCollection; use Symfony\Component\Routing\RouteCollection;
use Symfony\Component\Routing\Route; use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\Generator\Dumper\PhpGeneratorDumper; use Symfony\Component\Routing\Generator\Dumper\PhpGeneratorDumper;
use Symfony\Component\Routing\RequestContext;
class PhpGeneratorDumperTest extends \PHPUnit_Framework_TestCase class PhpGeneratorDumperTest extends \PHPUnit_Framework_TestCase
{ {
@ -37,7 +38,7 @@ class PhpGeneratorDumperTest extends \PHPUnit_Framework_TestCase
parent::setUp(); parent::setUp();
$this->routeCollection = new RouteCollection(); $this->routeCollection = new RouteCollection();
$this->generatorDumper = new PhpGeneratorDumper($this->routeCollection); $this->generatorDumper = new PhpGeneratorDumper($this->routeCollection, new RequestContext());
$this->testTmpFilepath = sys_get_temp_dir().DIRECTORY_SEPARATOR.'php_generator.php'; $this->testTmpFilepath = sys_get_temp_dir().DIRECTORY_SEPARATOR.'php_generator.php';
@unlink($this->testTmpFilepath); @unlink($this->testTmpFilepath);
} }
@ -57,12 +58,7 @@ class PhpGeneratorDumperTest extends \PHPUnit_Framework_TestCase
file_put_contents($this->testTmpFilepath, $this->generatorDumper->dump()); file_put_contents($this->testTmpFilepath, $this->generatorDumper->dump());
include ($this->testTmpFilepath); include ($this->testTmpFilepath);
$projectUrlGenerator = new \ProjectUrlGenerator(array( $projectUrlGenerator = new \ProjectUrlGenerator(new RequestContext('/app.php'));
'base_url' => '/app.php',
'method' => 'GET',
'host' => 'localhost',
'scheme' => 'http',
));
$absoluteUrlWithParameter = $projectUrlGenerator->generate('Test', array('foo' => 'bar'), true); $absoluteUrlWithParameter = $projectUrlGenerator->generate('Test', array('foo' => 'bar'), true);
$absoluteUrlWithoutParameter = $projectUrlGenerator->generate('Test2', array(), true); $absoluteUrlWithoutParameter = $projectUrlGenerator->generate('Test2', array(), true);
@ -83,12 +79,7 @@ class PhpGeneratorDumperTest extends \PHPUnit_Framework_TestCase
file_put_contents($this->testTmpFilepath, $this->generatorDumper->dump(array('class' => 'WithoutRoutesUrlGenerator'))); file_put_contents($this->testTmpFilepath, $this->generatorDumper->dump(array('class' => 'WithoutRoutesUrlGenerator')));
include ($this->testTmpFilepath); include ($this->testTmpFilepath);
$projectUrlGenerator = new \WithoutRoutesUrlGenerator(array( $projectUrlGenerator = new \WithoutRoutesUrlGenerator(new RequestContext('/app.php'));
'base_url' => '/app.php',
'method' => 'GET',
'host' => 'localhost',
'scheme' => 'http',
));
$projectUrlGenerator->generate('Test', array()); $projectUrlGenerator->generate('Test', array());
} }
@ -100,7 +91,7 @@ class PhpGeneratorDumperTest extends \PHPUnit_Framework_TestCase
file_put_contents($this->testTmpFilepath, $this->generatorDumper->dump(array('class' => 'DefaultRoutesUrlGenerator'))); file_put_contents($this->testTmpFilepath, $this->generatorDumper->dump(array('class' => 'DefaultRoutesUrlGenerator')));
include ($this->testTmpFilepath); include ($this->testTmpFilepath);
$projectUrlGenerator = new \DefaultRoutesUrlGenerator(array()); $projectUrlGenerator = new \DefaultRoutesUrlGenerator(new RequestContext());
$url = $projectUrlGenerator->generate('Test', array()); $url = $projectUrlGenerator->generate('Test', array());
$this->assertEquals($url, '/testing'); $this->assertEquals($url, '/testing');

View File

@ -14,6 +14,7 @@ namespace Symfony\Tests\Component\Routing\Generator;
use Symfony\Component\Routing\RouteCollection; use Symfony\Component\Routing\RouteCollection;
use Symfony\Component\Routing\Route; use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\Generator\UrlGenerator; use Symfony\Component\Routing\Generator\UrlGenerator;
use Symfony\Component\Routing\RequestContext;
class UrlGeneratorTest extends \PHPUnit_Framework_TestCase class UrlGeneratorTest extends \PHPUnit_Framework_TestCase
{ {
@ -36,7 +37,7 @@ class UrlGeneratorTest extends \PHPUnit_Framework_TestCase
public function testAbsoluteUrlWithNonStandardPort() public function testAbsoluteUrlWithNonStandardPort()
{ {
$routes = $this->getRoutes('test', new Route('/testing')); $routes = $this->getRoutes('test', new Route('/testing'));
$url = $this->getGenerator($routes, array('http_port' => 8080))->generate('test', array(), true); $url = $this->getGenerator($routes, array('httpPort' => 8080))->generate('test', array(), true);
$this->assertEquals('http://localhost:8080/app.php/testing', $url); $this->assertEquals('http://localhost:8080/app.php/testing', $url);
} }
@ -44,7 +45,7 @@ class UrlGeneratorTest extends \PHPUnit_Framework_TestCase
public function testAbsoluteSecureUrlWithNonStandardPort() public function testAbsoluteSecureUrlWithNonStandardPort()
{ {
$routes = $this->getRoutes('test', new Route('/testing')); $routes = $this->getRoutes('test', new Route('/testing'));
$url = $this->getGenerator($routes, array('https_port' => 8080, 'scheme' => 'https'))->generate('test', array(), true); $url = $this->getGenerator($routes, array('httpsPort' => 8080, 'scheme' => 'https'))->generate('test', array(), true);
$this->assertEquals('https://localhost:8080/app.php/testing', $url); $this->assertEquals('https://localhost:8080/app.php/testing', $url);
} }
@ -143,17 +144,14 @@ class UrlGeneratorTest extends \PHPUnit_Framework_TestCase
$this->assertEquals('http://localhost/app.php/', $this->getGenerator($routes, array('scheme' => 'https'))->generate('test')); $this->assertEquals('http://localhost/app.php/', $this->getGenerator($routes, array('scheme' => 'https'))->generate('test'));
} }
protected function getGenerator(RouteCollection $routes, array $context = array()) protected function getGenerator(RouteCollection $routes, array $parameters = array())
{ {
$generator = new UrlGenerator($routes); $context = new RequestContext('/app.php');
$generator->setContext(array_replace(array( foreach ($parameters as $key => $value) {
'base_url' => '/app.php', $method = 'set'.$key;
'method' => 'GET', $context->$method($value);
'host' => 'localhost', }
'http_port' => 80, $generator = new UrlGenerator($routes, $context);
'https_port' => 443,
'scheme' => 'http',
), $context));
return $generator; return $generator;
} }

View File

@ -14,6 +14,7 @@ namespace Symfony\Tests\Component\Routing;
use Symfony\Component\Routing\Matcher\Dumper\PhpMatcherDumper; use Symfony\Component\Routing\Matcher\Dumper\PhpMatcherDumper;
use Symfony\Component\Routing\Route; use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\RouteCollection; use Symfony\Component\Routing\RouteCollection;
use Symfony\Component\Routing\RequestContext;
class PhpMatcherDumperTest extends \PHPUnit_Framework_TestCase class PhpMatcherDumperTest extends \PHPUnit_Framework_TestCase
{ {
@ -67,7 +68,7 @@ class PhpMatcherDumperTest extends \PHPUnit_Framework_TestCase
array('def' => 'test') array('def' => 'test')
)); ));
$dumper = new PhpMatcherDumper($collection); $dumper = new PhpMatcherDumper($collection, new RequestContext());
$this->assertStringEqualsFile(__DIR__.'/../../Fixtures/dumper/url_matcher1.php', $dumper->dump(), '->dump() dumps basic routes to the correct PHP file.'); $this->assertStringEqualsFile(__DIR__.'/../../Fixtures/dumper/url_matcher1.php', $dumper->dump(), '->dump() dumps basic routes to the correct PHP file.');
// force HTTPS redirection // force HTTPS redirection
@ -98,7 +99,7 @@ class PhpMatcherDumperTest extends \PHPUnit_Framework_TestCase
array(), array(),
array('_scheme' => 'https') array('_scheme' => 'https')
)); ));
$dumper = new PhpMatcherDumper($collection); $dumper = new PhpMatcherDumper($collection, new RequestContext());
$dumper->dump(); $dumper->dump();
} }
} }

View File

@ -16,6 +16,7 @@ use Symfony\Component\Routing\Matcher\Exception\NotFoundException;
use Symfony\Component\Routing\Matcher\UrlMatcher; use Symfony\Component\Routing\Matcher\UrlMatcher;
use Symfony\Component\Routing\Route; use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\RouteCollection; use Symfony\Component\Routing\RouteCollection;
use Symfony\Component\Routing\RequestContext;
class UrlMatcherTest extends \PHPUnit_Framework_TestCase class UrlMatcherTest extends \PHPUnit_Framework_TestCase
{ {
@ -24,7 +25,7 @@ class UrlMatcherTest extends \PHPUnit_Framework_TestCase
$coll = new RouteCollection(); $coll = new RouteCollection();
$coll->add('foo', new Route('/foo')); $coll->add('foo', new Route('/foo'));
$matcher = new UrlMatcher($coll, array('method' => 'get')); $matcher = new UrlMatcher($coll, new RequestContext());
$matcher->match('/foo'); $matcher->match('/foo');
} }
@ -33,7 +34,7 @@ class UrlMatcherTest extends \PHPUnit_Framework_TestCase
$coll = new RouteCollection(); $coll = new RouteCollection();
$coll->add('foo', new Route('/foo', array(), array('_method' => 'post'))); $coll->add('foo', new Route('/foo', array(), array('_method' => 'post')));
$matcher = new UrlMatcher($coll, array('method' => 'get')); $matcher = new UrlMatcher($coll, new RequestContext());
try { try {
$matcher->match('/foo'); $matcher->match('/foo');
@ -49,7 +50,7 @@ class UrlMatcherTest extends \PHPUnit_Framework_TestCase
$coll->add('foo1', new Route('/foo', array(), array('_method' => 'post'))); $coll->add('foo1', new Route('/foo', array(), array('_method' => 'post')));
$coll->add('foo2', new Route('/foo', array(), array('_method' => 'put|delete'))); $coll->add('foo2', new Route('/foo', array(), array('_method' => 'put|delete')));
$matcher = new UrlMatcher($coll, array('method' => 'get')); $matcher = new UrlMatcher($coll, new RequestContext());
try { try {
$matcher->match('/foo'); $matcher->match('/foo');
@ -64,7 +65,7 @@ class UrlMatcherTest extends \PHPUnit_Framework_TestCase
// test the patterns are matched are parameters are returned // test the patterns are matched are parameters are returned
$collection = new RouteCollection(); $collection = new RouteCollection();
$collection->add('foo', new Route('/foo/{bar}')); $collection->add('foo', new Route('/foo/{bar}'));
$matcher = new UrlMatcher($collection, array(), array()); $matcher = new UrlMatcher($collection, new RequestContext(), array());
try { try {
$matcher->match('/no-match'); $matcher->match('/no-match');
$this->fail(); $this->fail();
@ -74,26 +75,26 @@ class UrlMatcherTest extends \PHPUnit_Framework_TestCase
// test that defaults are merged // test that defaults are merged
$collection = new RouteCollection(); $collection = new RouteCollection();
$collection->add('foo', new Route('/foo/{bar}', array('def' => 'test'))); $collection->add('foo', new Route('/foo/{bar}', array('def' => 'test')));
$matcher = new UrlMatcher($collection, array(), array()); $matcher = new UrlMatcher($collection, new RequestContext(), array());
$this->assertEquals(array('_route' => 'foo', 'bar' => 'baz', 'def' => 'test'), $matcher->match('/foo/baz')); $this->assertEquals(array('_route' => 'foo', 'bar' => 'baz', 'def' => 'test'), $matcher->match('/foo/baz'));
// test that route "method" is ignored if no method is given in the context // test that route "method" is ignored if no method is given in the context
$collection = new RouteCollection(); $collection = new RouteCollection();
$collection->add('foo', new Route('/foo', array(), array('_method' => 'GET|head'))); $collection->add('foo', new Route('/foo', array(), array('_method' => 'GET|head')));
$matcher = new UrlMatcher($collection, array(), array()); $matcher = new UrlMatcher($collection, new RequestContext(), array());
$this->assertInternalType('array', $matcher->match('/foo')); $this->assertInternalType('array', $matcher->match('/foo'));
// route does not match with POST method context // route does not match with POST method context
$matcher = new UrlMatcher($collection, array('method' => 'POST'), array()); $matcher = new UrlMatcher($collection, new RequestContext('', 'post'), array());
try { try {
$matcher->match('/foo'); $matcher->match('/foo');
$this->fail(); $this->fail();
} catch (MethodNotAllowedException $e) {} } catch (MethodNotAllowedException $e) {}
// route does match with GET or HEAD method context // route does match with GET or HEAD method context
$matcher = new UrlMatcher($collection, array('method' => 'GET'), array()); $matcher = new UrlMatcher($collection, new RequestContext(), array());
$this->assertInternalType('array', $matcher->match('/foo')); $this->assertInternalType('array', $matcher->match('/foo'));
$matcher = new UrlMatcher($collection, array('method' => 'HEAD'), array()); $matcher = new UrlMatcher($collection, new RequestContext('', 'head'), array());
$this->assertInternalType('array', $matcher->match('/foo')); $this->assertInternalType('array', $matcher->match('/foo'));
} }
} }