Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

[HttpFoundation] added a way to override the Request class

  • Loading branch information...
commit 464439d195fc895079176498cb28d182dc8aef14 1 parent 091a96c
@fabpot fabpot authored
View
35 src/Symfony/Component/HttpFoundation/Request.php
@@ -187,6 +187,8 @@ class Request
*/
protected static $formats;
+ protected static $requestFactory;
+
/**
* Constructor.
*
@@ -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'))
@@ -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;
}
/**
@@ -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);
+ }
}
View
19 src/Symfony/Component/HttpFoundation/Tests/RequestTest.php
@@ -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');
}
@@ -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
@@ -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';
+ }
+}
View
2  src/Symfony/Component/HttpKernel/Fragment/InlineFragmentRenderer.php
@@ -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'));
}
View
2  src/Symfony/Component/HttpKernel/HttpCache/HttpCache.php
@@ -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);
}
View
2  src/Symfony/Component/Security/Http/HttpUtils.php
@@ -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());
}
Please sign in to comment.
Something went wrong with that request. Please try again.