Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
replaced array for request context in Routing by a RequestContext class
- Loading branch information
Showing
14 changed files
with
247 additions
and
81 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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. | ||
|
@@ -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 @@ 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; | ||
} | ||
|
@@ -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.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
fabpot
Author
Member
|
||
$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; | ||
} | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
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?