8000 [Routing] fix type inconsistencies in RequestContext by Tobion · Pull Request #12190 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Routing] fix type inconsistencies in RequestContext #12190

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 9, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
J 8000 ump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8000
55 changes: 31 additions & 24 deletions src/Symfony/Component/Routing/RequestContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
* Holds information about the current request.
*
* @author Fabien Potencier <fabien@symfony.com>
* @author Tobias Schultze <http://tobion.de>
*
* @api
*/
Expand Down Expand Up @@ -52,16 +53,21 @@ class RequestContext
*/
public function __construct($baseUrl = '', $method = 'GET', $host = 'localhost', $scheme = 'http', $httpPort = 80, $httpsPort = 443, $path = '/', $queryString = '')
{
$this->baseUrl = $baseUrl;
$this->method = strtoupper($method);
$this->host = $host;
$this->scheme = strtolower($scheme);
$this->httpPort = $httpPort;
$this->httpsPort = $httpsPort;
$this->pathInfo = $path;
$this->queryString = $queryString;
$this->setBaseUrl($baseUrl);
$this->setMethod($method);
$this->setHost($host);
$this->setScheme($scheme);
$this->setHttpPort($httpPort);
10000 $this->setHttpsPort($httpsPort);
$this->setPathInfo($path);
$this->setQueryString($queryString);
}

/**
* Updates the RequestContext information based on a HttpFoundation Request.
*
* @param Request $request A Request instance
*/
public function fromRequest(Request $request)
{
$this->setBaseUrl($request->getBaseUrl());
Expand All @@ -71,7 +77,7 @@ public function fromRequest(Request $request)
$this->setScheme($request->getScheme());
$this->setHttpPort($request->isSecure() ? $this->httpPort : $request->getPort());
$this->setHttpsPort($request->isSecure() ? $request->getPort() : $this->httpsPort);
$this->setQueryString($request->server->get('QUERY_STRING'));
$this->setQueryString($request->server->get('QUERY_STRING', ''));
}

/**
Expand Down Expand Up @@ -143,6 +149,8 @@ public function setMethod($method)
/**
* Gets the HTTP host.
*
* The host is always lowercased because it must be treated case-insensitive.
*
* @return string The HTTP host
*/
public function getHost()
Expand All @@ -159,7 +167,7 @@ public function getHost()
*/
public function setHost($host)
{
$this->host = $host;
$this->host = strtolower($host);
}

/**
Expand Down Expand Up @@ -187,7 +195,7 @@ public function setScheme($scheme)
/**
* Gets the HTTP port.
*
* @return string The HTTP port
* @return int The HTTP port
*/
public function getHttpPort()
{
Expand All @@ -197,19 +205,19 @@ public function getHttpPort()
/**
* Sets the HTTP port.
*
* @param string $httpPort The HTTP port
* @param int $httpPort The HTTP port
*
* @api
*/
public function setHttpPort($httpPort)
{
$this->httpPort = $httpPort;
$this->httpPort = (int) $httpPort;
}

/**
* Gets the HTTPS port.
*
* @return string The HTTPS port
* @return int The HTTPS port
*/
public function getHttpsPort()
{
Expand All @@ -219,19 +227,19 @@ public function getHttpsPort()
/**
* Sets the HTTPS port.
*
* @param string $httpsPort The HTTPS port
* @param int $httpsPort The HTTPS port
*
* @api
*/
public function setHttpsPort($httpsPort)
{
$this->httpsPort = $httpsPort;
$this->httpsPort = (int) $httpsPort;
}

/**
* Gets the query string.
*
* @return string The query string
* @return string The query string without the "?"
*/
public function getQueryString()
{
Expand All @@ -241,13 +249,14 @@ public function getQueryString()
/**
* Sets the query string.
*
* @param string $queryString The query string
* @param string $queryString The query string (after "?")
*
* @api
*/
public function setQueryString($queryString)
{
$this->queryString = $queryString;
// string cast to be fault-tolerant, accepting null
$this->queryString = (string) $queryString;
}

/**
Expand All @@ -263,11 +272,9 @@ public function getParameters()
/**
* Sets the parameters.
*
* This method implements a fluent interface.
*
* @param array $parameters The parameters
*
* @return Route The current Route instance
* @return RequestContext The current instance, implementing a fluent interface
*/
public function setParameters(array $parameters)
{
Expand All @@ -281,7 +288,7 @@ public function setParameters(array $parameters)
*
* @param string $name A parameter name
*
* @return mixed The parameter value
* @return mixed The parameter value or null if nonexistent
*/
public function getParameter($name)
{
Expand All @@ -293,7 +300,7 @@ public function getParameter($name)
*
* @param string $name A parameter name
*
* @return bool true if the parameter value is set, false otherwise
* @return bool True if the parameter value is set, false otherwise
*/
public function hasParameter($name)
{
Expand Down
42 changes: 42 additions & 0 deletions src/Symfony/Component/Routing/Tests/RequestContextTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,4 +98,46 @@ public function testSetParameter()

$this->assertEquals('bar', $requestContext->getParameter('foo'));
}

public function testMethod()
{
$requestContext = new RequestContext();
$requestContext->setMethod('post');

$this->assertSame('POST', $requestContext->getMethod());
}

public function testScheme()
{
$requestContext = new RequestContext();
$requestContext->setScheme('HTTPS');

$this->assertSame('https', $requestContext->getScheme());
}

public function testHost()
{
$requestContext = new RequestContext();
$requestContext->setHost('eXampLe.com');

$this->assertSame('example.com', $requestContext->getHost());
}

public function testQueryString()
{
$requestContext = new RequestContext();
$requestContext->setQueryString(null);

$this->assertSame('', $requestContext->getQueryString());
}

public function testPort()
{
$requestContext = new RequestContext();
$requestContext->setHttpPort('123');
$requestContext->setHttpsPort('456');

$this->assertSame(123, $requestContext->getHttpPort());
$this->assertSame(456, $requestContext->getHttpsPort());
}
}
0