replaced array for request context in Routing by a RequestContext class
This commit is contained in:
parent
07aae98495
commit
117321d3c6
@ -21,6 +21,7 @@ use Symfony\Component\DependencyInjection\ContainerInterface;
|
||||
use Symfony\Component\Routing\Matcher\Exception\MethodNotAllowedException;
|
||||
use Symfony\Component\Routing\Matcher\Exception\NotFoundException;
|
||||
use Symfony\Component\Routing\RouterInterface;
|
||||
use Symfony\Component\Routing\RequestContext;
|
||||
|
||||
/**
|
||||
* RequestListener.
|
||||
@ -76,13 +77,13 @@ class RequestListener
|
||||
if ($master) {
|
||||
// set the context even if the parsing does not need to be done
|
||||
// to have correct link generation
|
||||
$this->router->setContext(array(
|
||||
'base_url' => $request->getBaseUrl(),
|
||||
'method' => $request->getMethod(),
|
||||
'host' => $request->getHost(),
|
||||
'scheme' => $request->getScheme(),
|
||||
'http_port' => $this->httpPort,
|
||||
'https_port' => $this->httpsPort,
|
||||
$this->router->setContext(new RequestContext(
|
||||
$request->getBaseUrl(),
|
||||
$request->getMethod(),
|
||||
$request->getHost(),
|
||||
$request->getScheme(),
|
||||
$this->httpPort,
|
||||
$this->httpsPort
|
||||
));
|
||||
}
|
||||
|
||||
|
@ -35,8 +35,8 @@ class RedirectableUrlMatcher extends UrlMatcher implements RedirectableUrlMatche
|
||||
'path' => $path,
|
||||
'permanent' => true,
|
||||
'scheme' => $scheme,
|
||||
'httpPort' => isset($this->context['http_port']) ? $this->context['http_port'] : 80,
|
||||
'httpsPort' => isset($this->context['https_port']) ? $this->context['https_port'] : 443,
|
||||
'httpPort' => $this->context->getHttpPort(),
|
||||
'httpsPort' => $this->context->getHttpsPort(),
|
||||
'_route' => $route,
|
||||
);
|
||||
}
|
||||
|
@ -107,6 +107,8 @@ EOF;
|
||||
return <<<EOF
|
||||
<?php
|
||||
|
||||
use Symfony\Component\Routing\RequestContext;
|
||||
|
||||
/**
|
||||
* $class
|
||||
*
|
||||
@ -129,7 +131,7 @@ EOF;
|
||||
/**
|
||||
* Constructor.
|
||||
*/
|
||||
public function __construct(array \$context = array(), array \$defaults = array())
|
||||
public function __construct(RequestContext \$context, array \$defaults = array())
|
||||
{
|
||||
\$this->context = \$context;
|
||||
\$this->defaults = \$defaults;
|
||||
|
@ -13,6 +13,7 @@ namespace Symfony\Component\Routing\Generator;
|
||||
|
||||
use Symfony\Component\Routing\Route;
|
||||
use Symfony\Component\Routing\RouteCollection;
|
||||
use Symfony\Component\Routing\RequestContext;
|
||||
|
||||
/**
|
||||
* UrlGenerator generates URL based on a set of routes.
|
||||
@ -31,10 +32,10 @@ class UrlGenerator implements UrlGeneratorInterface
|
||||
* Constructor.
|
||||
*
|
||||
* @param RouteCollection $routes A RouteCollection instance
|
||||
* @param array $context The context
|
||||
* @param RequestContext $context The context
|
||||
* @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->context = $context;
|
||||
@ -45,9 +46,9 @@ class UrlGenerator implements UrlGeneratorInterface
|
||||
/**
|
||||
* 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;
|
||||
}
|
||||
@ -127,10 +128,10 @@ class UrlGenerator implements UrlGeneratorInterface
|
||||
$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'])) {
|
||||
$scheme = isset($this->context['scheme']) ? $this->context['scheme'] : 'http';
|
||||
if ($this->context->getHost()) {
|
||||
$scheme = $this->context->getScheme();
|
||||
if (isset($requirements['_scheme']) && ($req = strtolower($requirements['_scheme'])) && $scheme != $req) {
|
||||
$absolute = true;
|
||||
$scheme = $req;
|
||||
@ -138,13 +139,13 @@ class UrlGenerator implements UrlGeneratorInterface
|
||||
|
||||
if ($absolute) {
|
||||
$port = '';
|
||||
if ('http' === $scheme && 80 != ($httpPort = isset($this->context['http_port']) ? $this->context['http_port'] : 80)) {
|
||||
$port = ':'.$httpPort;
|
||||
} elseif ('https' === $scheme && 443 != ($httpsPort = isset($this->context['https_port']) ? $this->context['https_port'] : 443)) {
|
||||
$port = ':'.$httpsPort;
|
||||
if ('http' === $scheme && 80 != $this->context->getHttpPort()) {
|
||||
$port = ':'.$this->context->getHttpPort();
|
||||
} elseif ('https' === $scheme && 443 != $this->context->getHttpsPort()) {
|
||||
$port = ':'.$this->context->getHttpsPort();
|
||||
}
|
||||
|
||||
$url = $scheme.'://'.$this->context['host'].$port.$url;
|
||||
$url = $scheme.'://'.$this->context->getHost().$port.$url;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -94,7 +94,7 @@ EOF;
|
||||
if ($req = $route->getRequirement('_method')) {
|
||||
$req = implode('\', \'', array_map('strtolower', explode('|', $req)));
|
||||
$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'));
|
||||
goto $gotoname;
|
||||
}
|
||||
@ -116,7 +116,7 @@ EOF
|
||||
}
|
||||
|
||||
$code[] = sprintf(<<<EOF
|
||||
if (isset(\$this->context['scheme']) && \$this->context['scheme'] !== '$scheme') {
|
||||
if (\$this->context->getScheme() !== '$scheme') {
|
||||
return \$this->redirect(\$pathinfo, '%s', '$scheme');
|
||||
}
|
||||
EOF
|
||||
@ -165,6 +165,7 @@ EOF;
|
||||
|
||||
use Symfony\Component\Routing\Matcher\Exception\MethodNotAllowedException;
|
||||
use Symfony\Component\Routing\Matcher\Exception\NotFoundException;
|
||||
use Symfony\Component\Routing\RequestContext;
|
||||
|
||||
/**
|
||||
* $class
|
||||
@ -184,7 +185,7 @@ EOF;
|
||||
/**
|
||||
* Constructor.
|
||||
*/
|
||||
public function __construct(array \$context = array(), array \$defaults = array())
|
||||
public function __construct(RequestContext \$context, array \$defaults = array())
|
||||
{
|
||||
\$this->context = \$context;
|
||||
\$this->defaults = \$defaults;
|
||||
|
@ -15,6 +15,7 @@ use Symfony\Component\Routing\Matcher\Exception\MethodNotAllowedException;
|
||||
use Symfony\Component\Routing\Matcher\Exception\NotFoundException;
|
||||
use Symfony\Component\Routing\Route;
|
||||
use Symfony\Component\Routing\RouteCollection;
|
||||
use Symfony\Component\Routing\RequestContext;
|
||||
|
||||
/**
|
||||
* UrlMatcher matches URL based on a set of routes.
|
||||
@ -32,10 +33,10 @@ class UrlMatcher implements UrlMatcherInterface
|
||||
* Constructor.
|
||||
*
|
||||
* @param RouteCollection $routes A RouteCollection instance
|
||||
* @param array $context The context
|
||||
* @param RequestContext $context The context
|
||||
* @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->context = $context;
|
||||
@ -45,9 +46,9 @@ class UrlMatcher implements UrlMatcherInterface
|
||||
/**
|
||||
* 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;
|
||||
}
|
||||
@ -79,7 +80,7 @@ class UrlMatcher implements UrlMatcherInterface
|
||||
}
|
||||
|
||||
// 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);
|
||||
continue;
|
||||
}
|
||||
|
167
src/Symfony/Component/Routing/RequestContext.php
Normal file
167
src/Symfony/Component/Routing/RequestContext.php
Normal 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;
|
||||
}
|
||||
}
|
@ -48,11 +48,11 @@ class Router implements RouterInterface
|
||||
*
|
||||
* @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->resource = $resource;
|
||||
$this->context = $context;
|
||||
$this->context = null === $context ? new RequestContext() : $context;
|
||||
$this->defaults = $defaults;
|
||||
$this->options = array(
|
||||
'cache_dir' => null,
|
||||
@ -102,9 +102,9 @@ class Router implements RouterInterface
|
||||
/**
|
||||
* 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->getGenerator()->setContext($context);
|
||||
|
@ -2,6 +2,7 @@
|
||||
|
||||
use Symfony\Component\Routing\Matcher\Exception\MethodNotAllowedException;
|
||||
use Symfony\Component\Routing\Matcher\Exception\NotFoundException;
|
||||
use Symfony\Component\Routing\RequestContext;
|
||||
|
||||
/**
|
||||
* ProjectUrlMatcher
|
||||
@ -14,7 +15,7 @@ class ProjectUrlMatcher extends Symfony\Component\Routing\Matcher\UrlMatcher
|
||||
/**
|
||||
* Constructor.
|
||||
*/
|
||||
public function __construct(array $context = array(), array $defaults = array())
|
||||
public function __construct(RequestContext $context, array $defaults = array())
|
||||
{
|
||||
$this->context = $context;
|
||||
$this->defaults = $defaults;
|
||||
@ -31,7 +32,7 @@ class ProjectUrlMatcher extends Symfony\Component\Routing\Matcher\UrlMatcher
|
||||
|
||||
// bar
|
||||
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'));
|
||||
goto not_bar;
|
||||
}
|
||||
@ -63,7 +64,7 @@ class ProjectUrlMatcher extends Symfony\Component\Routing\Matcher\UrlMatcher
|
||||
|
||||
// baz5
|
||||
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'));
|
||||
goto not_baz5;
|
||||
}
|
||||
@ -74,7 +75,7 @@ class ProjectUrlMatcher extends Symfony\Component\Routing\Matcher\UrlMatcher
|
||||
|
||||
// baz.baz6
|
||||
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'));
|
||||
goto not_bazbaz6;
|
||||
}
|
||||
|
@ -2,6 +2,7 @@
|
||||
|
||||
use Symfony\Component\Routing\Matcher\Exception\MethodNotAllowedException;
|
||||
use Symfony\Component\Routing\Matcher\Exception\NotFoundException;
|
||||
use Symfony\Component\Routing\RequestContext;
|
||||
|
||||
/**
|
||||
* ProjectUrlMatcher
|
||||
@ -14,7 +15,7 @@ class ProjectUrlMatcher extends Symfony\Tests\Component\Routing\Fixtures\Redirec
|
||||
/**
|
||||
* Constructor.
|
||||
*/
|
||||
public function __construct(array $context = array(), array $defaults = array())
|
||||
public function __construct(RequestContext $context, array $defaults = array())
|
||||
{
|
||||
$this->context = $context;
|
||||
$this->defaults = $defaults;
|
||||
@ -31,7 +32,7 @@ class ProjectUrlMatcher extends Symfony\Tests\Component\Routing\Fixtures\Redirec
|
||||
|
||||
// bar
|
||||
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'));
|
||||
goto not_bar;
|
||||
}
|
||||
@ -69,7 +70,7 @@ class ProjectUrlMatcher extends Symfony\Tests\Component\Routing\Fixtures\Redirec
|
||||
|
||||
// baz5
|
||||
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'));
|
||||
goto not_baz5;
|
||||
}
|
||||
@ -83,7 +84,7 @@ class ProjectUrlMatcher extends Symfony\Tests\Component\Routing\Fixtures\Redirec
|
||||
|
||||
// baz.baz6
|
||||
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'));
|
||||
goto not_bazbaz6;
|
||||
}
|
||||
@ -102,7 +103,7 @@ class ProjectUrlMatcher extends Symfony\Tests\Component\Routing\Fixtures\Redirec
|
||||
|
||||
// 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 array('_route' => 'secure');
|
||||
@ -110,7 +111,7 @@ class ProjectUrlMatcher extends Symfony\Tests\Component\Routing\Fixtures\Redirec
|
||||
|
||||
// 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 array('_route' => 'nonsecure');
|
||||
|
@ -14,6 +14,7 @@ namespace Symfony\Tests\Component\Routing\Generator\Dumper\PhpGeneratorDumper;
|
||||
use Symfony\Component\Routing\RouteCollection;
|
||||
use Symfony\Component\Routing\Route;
|
||||
use Symfony\Component\Routing\Generator\Dumper\PhpGeneratorDumper;
|
||||
use Symfony\Component\Routing\RequestContext;
|
||||
|
||||
class PhpGeneratorDumperTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
@ -37,7 +38,7 @@ class PhpGeneratorDumperTest extends \PHPUnit_Framework_TestCase
|
||||
parent::setUp();
|
||||
|
||||
$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';
|
||||
@unlink($this->testTmpFilepath);
|
||||
}
|
||||
@ -57,12 +58,7 @@ class PhpGeneratorDumperTest extends \PHPUnit_Framework_TestCase
|
||||
file_put_contents($this->testTmpFilepath, $this->generatorDumper->dump());
|
||||
include ($this->testTmpFilepath);
|
||||
|
||||
$projectUrlGenerator = new \ProjectUrlGenerator(array(
|
||||
'base_url' => '/app.php',
|
||||
'method' => 'GET',
|
||||
'host' => 'localhost',
|
||||
'scheme' => 'http',
|
||||
));
|
||||
$projectUrlGenerator = new \ProjectUrlGenerator(new RequestContext('/app.php'));
|
||||
|
||||
$absoluteUrlWithParameter = $projectUrlGenerator->generate('Test', array('foo' => 'bar'), 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')));
|
||||
include ($this->testTmpFilepath);
|
||||
|
||||
$projectUrlGenerator = new \WithoutRoutesUrlGenerator(array(
|
||||
'base_url' => '/app.php',
|
||||
'method' => 'GET',
|
||||
'host' => 'localhost',
|
||||
'scheme' => 'http',
|
||||
));
|
||||
$projectUrlGenerator = new \WithoutRoutesUrlGenerator(new RequestContext('/app.php'));
|
||||
|
||||
$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')));
|
||||
include ($this->testTmpFilepath);
|
||||
|
||||
$projectUrlGenerator = new \DefaultRoutesUrlGenerator(array());
|
||||
$projectUrlGenerator = new \DefaultRoutesUrlGenerator(new RequestContext());
|
||||
$url = $projectUrlGenerator->generate('Test', array());
|
||||
|
||||
$this->assertEquals($url, '/testing');
|
||||
|
@ -14,6 +14,7 @@ namespace Symfony\Tests\Component\Routing\Generator;
|
||||
use Symfony\Component\Routing\RouteCollection;
|
||||
use Symfony\Component\Routing\Route;
|
||||
use Symfony\Component\Routing\Generator\UrlGenerator;
|
||||
use Symfony\Component\Routing\RequestContext;
|
||||
|
||||
class UrlGeneratorTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
@ -36,7 +37,7 @@ class UrlGeneratorTest extends \PHPUnit_Framework_TestCase
|
||||
public function testAbsoluteUrlWithNonStandardPort()
|
||||
{
|
||||
$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);
|
||||
}
|
||||
@ -44,7 +45,7 @@ class UrlGeneratorTest extends \PHPUnit_Framework_TestCase
|
||||
public function testAbsoluteSecureUrlWithNonStandardPort()
|
||||
{
|
||||
$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);
|
||||
}
|
||||
@ -143,17 +144,14 @@ class UrlGeneratorTest extends \PHPUnit_Framework_TestCase
|
||||
$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);
|
||||
$generator->setContext(array_replace(array(
|
||||
'base_url' => '/app.php',
|
||||
'method' => 'GET',
|
||||
'host' => 'localhost',
|
||||
'http_port' => 80,
|
||||
'https_port' => 443,
|
||||
'scheme' => 'http',
|
||||
), $context));
|
||||
$context = new RequestContext('/app.php');
|
||||
foreach ($parameters as $key => $value) {
|
||||
$method = 'set'.$key;
|
||||
$context->$method($value);
|
||||
}
|
||||
$generator = new UrlGenerator($routes, $context);
|
||||
|
||||
return $generator;
|
||||
}
|
||||
|
@ -14,6 +14,7 @@ namespace Symfony\Tests\Component\Routing;
|
||||
use Symfony\Component\Routing\Matcher\Dumper\PhpMatcherDumper;
|
||||
use Symfony\Component\Routing\Route;
|
||||
use Symfony\Component\Routing\RouteCollection;
|
||||
use Symfony\Component\Routing\RequestContext;
|
||||
|
||||
class PhpMatcherDumperTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
@ -67,7 +68,7 @@ class PhpMatcherDumperTest extends \PHPUnit_Framework_TestCase
|
||||
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.');
|
||||
|
||||
// force HTTPS redirection
|
||||
@ -98,7 +99,7 @@ class PhpMatcherDumperTest extends \PHPUnit_Framework_TestCase
|
||||
array(),
|
||||
array('_scheme' => 'https')
|
||||
));
|
||||
$dumper = new PhpMatcherDumper($collection);
|
||||
$dumper = new PhpMatcherDumper($collection, new RequestContext());
|
||||
$dumper->dump();
|
||||
}
|
||||
}
|
||||
|
@ -16,6 +16,7 @@ use Symfony\Component\Routing\Matcher\Exception\NotFoundException;
|
||||
use Symfony\Component\Routing\Matcher\UrlMatcher;
|
||||
use Symfony\Component\Routing\Route;
|
||||
use Symfony\Component\Routing\RouteCollection;
|
||||
use Symfony\Component\Routing\RequestContext;
|
||||
|
||||
class UrlMatcherTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
@ -24,7 +25,7 @@ class UrlMatcherTest extends \PHPUnit_Framework_TestCase
|
||||
$coll = new RouteCollection();
|
||||
$coll->add('foo', new Route('/foo'));
|
||||
|
||||
$matcher = new UrlMatcher($coll, array('method' => 'get'));
|
||||
$matcher = new UrlMatcher($coll, new RequestContext());
|
||||
$matcher->match('/foo');
|
||||
}
|
||||
|
||||
@ -33,7 +34,7 @@ class UrlMatcherTest extends \PHPUnit_Framework_TestCase
|
||||
$coll = new RouteCollection();
|
||||
$coll->add('foo', new Route('/foo', array(), array('_method' => 'post')));
|
||||
|
||||
$matcher = new UrlMatcher($coll, array('method' => 'get'));
|
||||
$matcher = new UrlMatcher($coll, new RequestContext());
|
||||
|
||||
try {
|
||||
$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('foo2', new Route('/foo', array(), array('_method' => 'put|delete')));
|
||||
|
||||
$matcher = new UrlMatcher($coll, array('method' => 'get'));
|
||||
$matcher = new UrlMatcher($coll, new RequestContext());
|
||||
|
||||
try {
|
||||
$matcher->match('/foo');
|
||||
@ -64,7 +65,7 @@ class UrlMatcherTest extends \PHPUnit_Framework_TestCase
|
||||
// test the patterns are matched are parameters are returned
|
||||
$collection = new RouteCollection();
|
||||
$collection->add('foo', new Route('/foo/{bar}'));
|
||||
$matcher = new UrlMatcher($collection, array(), array());
|
||||
$matcher = new UrlMatcher($collection, new RequestContext(), array());
|
||||
try {
|
||||
$matcher->match('/no-match');
|
||||
$this->fail();
|
||||
@ -74,26 +75,26 @@ class UrlMatcherTest extends \PHPUnit_Framework_TestCase
|
||||
// test that defaults are merged
|
||||
$collection = new RouteCollection();
|
||||
$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'));
|
||||
|
||||
// test that route "method" is ignored if no method is given in the context
|
||||
$collection = new RouteCollection();
|
||||
$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'));
|
||||
|
||||
// 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 {
|
||||
$matcher->match('/foo');
|
||||
$this->fail();
|
||||
} catch (MethodNotAllowedException $e) {}
|
||||
|
||||
// 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'));
|
||||
$matcher = new UrlMatcher($collection, array('method' => 'HEAD'), array());
|
||||
$matcher = new UrlMatcher($collection, new RequestContext('', 'head'), array());
|
||||
$this->assertInternalType('array', $matcher->match('/foo'));
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user