Skip to content

[HttpFoundation] added a way to override the Request class #8957

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 1 commit into from
Oct 1, 2013
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
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 33 additions & 2 deletions src/Symfony/Component/HttpFoundation/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,8 @@ class Request
*/
protected static $formats;

protected static $requestFactory;

/**
* Constructor.
*
Expand Down Expand Up @@ -252,7 +254,7 @@ public function initialize(array $query = array(), array $request = array(), arr
*/
public static function createFromGlobals()
{
$request = new static($_GET, $_POST, array(), $_COOKIE, $_FILES, $_SERVER);
$request = self::createRequestFromFactory($_GET, $_POST, array(), $_COOKIE, $_FILES, $_SERVER);

if (0 === strpos($request->headers->get('CONTENT_TYPE'), 'application/x-www-form-urlencoded')
&& in_array(strtoupper($request->server->get('REQUEST_METHOD', 'GET')), array('PUT', 'DELETE', 'PATCH'))
Expand Down Expand Up @@ -361,7 +363,21 @@ public static function create($uri, $method = 'GET', $parameters = array(), $coo
$server['REQUEST_URI'] = $components['path'].('' !== $queryString ? '?'.$queryString : '');
$server['QUERY_STRING'] = $queryString;

return new static($query, $request, array(), $cookies, $files, $server, $content);
return self::createRequestFromFactory($query, $request, array(), $cookies, $files, $server, $content);
}

/**
* Sets a callable able to create a Request instance.
*
* This is mainly useful when you need to override the Request class
* to keep BC with an existing system. It should not be used for any
* other purpose.
*
* @param callable $callable A PHP callable
*/
public static function setFactory($callable)
{
self::$requestFactory = $callable;
}

/**
Expand Down Expand Up @@ -1786,4 +1802,19 @@ private function getUrlencodedPrefix($string, $prefix)

return false;
}

private static function createRequestFromFactory(array $query = array(), array $request = array(), array $attributes = array(), array $cookies = array(), array $files = array(), array $server = array(), $content = null)
{
if (self::$requestFactory) {
$request = call_user_func(self::$requestFactory, $query, $request, $attributes, $cookies, $files, $server, $content);

if (!$request instanceof Request) {
throw new \LogicException('The Request factory must return an instance of Symfony\Component\HttpFoundation\Request.');
}

return $request;
}

return new static($query, $request, $attributes, $cookies, $files, $server, $content);
}
}
19 changes: 18 additions & 1 deletion src/Symfony/Component/HttpFoundation/Tests/RequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1465,7 +1465,7 @@ public function testIISRequestUri($headers, $server, $expectedRequestUri)
$this->assertEquals($expectedRequestUri, $request->getRequestUri(), '->getRequestUri() is correct');

$subRequestUri = '/bar/foo';
$subRequest = $request::create($subRequestUri, 'get', array(), array(), array(), $request->server->all());
$subRequest = Request::create($subRequestUri, 'get', array(), array(), array(), $request->server->all());
$this->assertEquals($subRequestUri, $subRequest->getRequestUri(), '->getRequestUri() is correct in sub request');
}

Expand Down Expand Up @@ -1572,6 +1572,15 @@ public function testTrustedHosts()
// reset request for following tests
Request::setTrustedHosts(array());
}

public function testFactory()
{
Request::setFactory(function (array $query = array(), array $request = array(), array $attributes = array(), array $cookies = array(), array $files = array(), array $server = array(), $content = null) {
return new NewRequest();
});

$this->assertEquals('foo', Request::create('/')->getFoo());
}
}

class RequestContentProxy extends Request
Expand All @@ -1581,3 +1590,11 @@ public function getContent($asResource = false)
return http_build_query(array('_method' => 'PUT', 'content' => 'mycontent'));
}
}

class NewRequest extends Request
{
public function getFoo()
{
return 'foo';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ protected function createSubRequest($uri, Request $request)

$server['REMOTE_ADDR'] = '127.0.0.1';

$subRequest = $request::create($uri, 'get', array(), $cookies, array(), $server);
$subRequest = Request::create($uri, 'get', array(), $cookies, array(), $server);
if ($request->headers->has('Surrogate-Capability')) {
$subRequest->headers->set('Surrogate-Capability', $request->headers->get('Surrogate-Capability'));
}
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/HttpKernel/HttpCache/HttpCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ protected function invalidate(Request $request, $catch = false)
// As per the RFC, invalidate Location and Content-Location URLs if present
foreach (array('Location', 'Content-Location') as $header) {
if ($uri = $response->headers->get($header)) {
$subRequest = $request::create($uri, 'get', array(), array(), array(), $request->server->all());
$subRequest = Request::create($uri, 'get', array(), array(), array(), $request->server->all());

$this->store->invalidate($subRequest);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/Security/Http/HttpUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public function createRedirectResponse(Request $request, $path, $status = 302)
*/
public function createRequest(Request $request, $path)
{
$newRequest = $request::create($this->generateUri($request, $path), 'get', array(), $request->cookies->all(), array(), $request->server->all());
$newRequest = Request::create($this->generateUri($request, $path), 'get', array(), $request->cookies->all(), array(), $request->server->all());
if ($request->hasSession()) {
$newRequest->setSession($request->getSession());
}
Expand Down