Skip to content

Commit

Permalink
replaced array for request context in Routing by a RequestContext class
Browse files Browse the repository at this point in the history
  • Loading branch information
fabpot committed Apr 20, 2011
1 parent 07aae98 commit 117321d
Show file tree
Hide file tree
Showing 14 changed files with 247 additions and 81 deletions.
15 changes: 8 additions & 7 deletions src/Symfony/Bundle/FrameworkBundle/RequestListener.php
Expand Up @@ -21,6 +21,7 @@
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.
Expand Down Expand Up @@ -76,13 +77,13 @@ protected function initializeRequestAttributes(Request $request, $master)
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
));
}

Expand Down
Expand Up @@ -35,8 +35,8 @@ public function redirect($path, $route, $scheme = null)
'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,
);
}
Expand Down
Expand Up @@ -107,6 +107,8 @@ private function startClass($class, $baseClass)
return <<<EOF
<?php
use Symfony\Component\Routing\RequestContext;
/**
* $class
*
Expand All @@ -129,7 +131,7 @@ private function addConstructor()
/**
* Constructor.
*/
public function __construct(array \$context = array(), array \$defaults = array())
public function __construct(RequestContext \$context, array \$defaults = array())
{
\$this->context = \$context;
\$this->defaults = \$defaults;
Expand Down
25 changes: 13 additions & 12 deletions src/Symfony/Component/Routing/Generator/UrlGenerator.php
Expand Up @@ -13,6 +13,7 @@

use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\RouteCollection;
use Symfony\Component\Routing\RequestContext;

/**
* UrlGenerator generates URL based on a set of routes.
Expand All @@ -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;
Expand All @@ -45,9 +46,9 @@ public function __construct(RouteCollection $routes, array $context = array(), a
/**
* 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;
}
Expand Down Expand Up @@ -127,24 +128,24 @@ protected function doGenerate($variables, $defaults, $requirements, $tokens, $pa
$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;
}

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()) {

This comment has been minimized.

Copy link
@snc

snc Apr 23, 2011

Contributor

I'm developing under a different port than 80 but $this->context->getHttpPort() always returns 80. Maybe thats because inside of the RequestListener#initializeRequestAttributes(), the $request->getPort() is not called? Or do we have to configure the http(s) ports for the new scheme feature? If so how?

This comment has been minimized.

Copy link
@fabpot

fabpot Apr 23, 2011

Author Member

You can configure it via the http_port option of the router configuration.

This comment has been minimized.

Copy link
@snc

snc Apr 23, 2011

Contributor

Thanks works as expected :-)

$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;
}
}

Expand Down
Expand Up @@ -94,7 +94,7 @@ private function addMatcher($supportsRedirections)
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;
}
Expand All @@ -116,7 +116,7 @@ private function addMatcher($supportsRedirections)
}

$code[] = sprintf(<<<EOF
if (isset(\$this->context['scheme']) && \$this->context['scheme'] !== '$scheme') {
if (\$this->context->getScheme() !== '$scheme') {
return \$this->redirect(\$pathinfo, '%s', '$scheme');
}
EOF
Expand Down Expand Up @@ -165,6 +165,7 @@ private function startClass($class, $baseClass)
use Symfony\Component\Routing\Matcher\Exception\MethodNotAllowedException;
use Symfony\Component\Routing\Matcher\Exception\NotFoundException;
use Symfony\Component\Routing\RequestContext;
/**
* $class
Expand All @@ -184,7 +185,7 @@ private function addConstructor()
/**
* Constructor.
*/
public function __construct(array \$context = array(), array \$defaults = array())
public function __construct(RequestContext \$context, array \$defaults = array())
{
\$this->context = \$context;
\$this->defaults = \$defaults;
Expand Down
11 changes: 6 additions & 5 deletions src/Symfony/Component/Routing/Matcher/UrlMatcher.php
Expand Up @@ -15,6 +15,7 @@
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.
Expand All @@ -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;
Expand All @@ -45,9 +46,9 @@ public function __construct(RouteCollection $routes, array $context = array(), a
/**
* 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;
}
Expand Down Expand Up @@ -79,7 +80,7 @@ public function match($pathinfo)
}

// 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;
}
Expand Down
167 changes: 167 additions & 0 deletions src/Symfony/Component/Routing/RequestContext.php
@@ -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;
}
}
8 changes: 4 additions & 4 deletions src/Symfony/Component/Routing/Router.php
Expand Up @@ -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,
Expand Down Expand Up @@ -102,9 +102,9 @@ public function getRouteCollection()
/**
* 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);
Expand Down

0 comments on commit 117321d

Please sign in to comment.