Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 464439d

Browse filesBrowse files
committed
[HttpFoundation] added a way to override the Request class
1 parent 091a96c commit 464439d
Copy full SHA for 464439d

File tree

Expand file treeCollapse file tree

5 files changed

+54
-6
lines changed
Filter options
Expand file treeCollapse file tree

5 files changed

+54
-6
lines changed

‎src/Symfony/Component/HttpFoundation/Request.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/HttpFoundation/Request.php
+33-2Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,8 @@ class Request
187187
*/
188188
protected static $formats;
189189

190+
protected static $requestFactory;
191+
190192
/**
191193
* Constructor.
192194
*
@@ -252,7 +254,7 @@ public function initialize(array $query = array(), array $request = array(), arr
252254
*/
253255
public static function createFromGlobals()
254256
{
255-
$request = new static($_GET, $_POST, array(), $_COOKIE, $_FILES, $_SERVER);
257+
$request = self::createRequestFromFactory($_GET, $_POST, array(), $_COOKIE, $_FILES, $_SERVER);
256258

257259
if (0 === strpos($request->headers->get('CONTENT_TYPE'), 'application/x-www-form-urlencoded')
258260
&& 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
361363
$server['REQUEST_URI'] = $components['path'].('' !== $queryString ? '?'.$queryString : '');
362364
$server['QUERY_STRING'] = $queryString;
363365

364-
return new static($query, $request, array(), $cookies, $files, $server, $content);
366+
return self::createRequestFromFactory($query, $request, array(), $cookies, $files, $server, $content);
367+
}
368+
369+
/**
370+
* Sets a callable able to create a Request instance.
371+
*
372+
* This is mainly useful when you need to override the Request class
373+
* to keep BC with an existing system. It should not be used for any
374+
* other purpose.
375+
*
376+
* @param callable $callable A PHP callable
377+
*/
378+
public static function setFactory($callable)
379+
{
380+
self::$requestFactory = $callable;
365381
}
366382

367383
/**
@@ -1786,4 +1802,19 @@ private function getUrlencodedPrefix($string, $prefix)
17861802

17871803
return false;
17881804
}
1805+
1806+
private static function createRequestFromFactory(array $query = array(), array $request = array(), array $attributes = array(), array $cookies = array(), array $files = array(), array $server = array(), $content = null)
1807+
{
1808+
if (self::$requestFactory) {
1809+
$request = call_user_func(self::$requestFactory, $query, $request, $attributes, $cookies, $files, $server, $content);
1810+
1811+
if (!$request instanceof Request) {
1812+
throw new \LogicException('The Request factory must return an instance of Symfony\Component\HttpFoundation\Request.');
1813+
}
1814+
1815+
return $request;
1816+
}
1817+
1818+
return new static($query, $request, $attributes, $cookies, $files, $server, $content);
1819+
}
17891820
}

‎src/Symfony/Component/HttpFoundation/Tests/RequestTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/HttpFoundation/Tests/RequestTest.php
+18-1Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1465,7 +1465,7 @@ public function testIISRequestUri($headers, $server, $expectedRequestUri)
14651465
$this->assertEquals($expectedRequestUri, $request->getRequestUri(), '->getRequestUri() is correct');
14661466

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

@@ -1572,6 +1572,15 @@ public function testTrustedHosts()
15721572
// reset request for following tests
15731573
Request::setTrustedHosts(array());
15741574
}
1575+
1576+
public function testFactory()
1577+
{
1578+
Request::setFactory(function (array $query = array(), array $request = array(), array $attributes = array(), array $cookies = array(), array $files = array(), array $server = array(), $content = null) {
1579+
return new NewRequest();
1580+
});
1581+
1582+
$this->assertEquals('foo', Request::create('/')->getFoo());
1583+
}
15751584
}
15761585

15771586
class RequestContentProxy extends Request
@@ -1581,3 +1590,11 @@ public function getContent($asResource = false)
15811590
return http_build_query(array('_method' => 'PUT', 'content' => 'mycontent'));
15821591
}
15831592
}
1593+
1594+
class NewRequest extends Request
1595+
{
1596+
public function getFoo()
1597+
{
1598+
return 'foo';
1599+
}
1600+
}

‎src/Symfony/Component/HttpKernel/Fragment/InlineFragmentRenderer.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/HttpKernel/Fragment/InlineFragmentRenderer.php
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ protected function createSubRequest($uri, Request $request)
132132

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

135-
$subRequest = $request::create($uri, 'get', array(), $cookies, array(), $server);
135+
$subRequest = Request::create($uri, 'get', array(), $cookies, array(), $server);
136136
if ($request->headers->has('Surrogate-Capability')) {
137137
$subRequest->headers->set('Surrogate-Capability', $request->headers->get('Surrogate-Capability'));
138138
}

‎src/Symfony/Component/HttpKernel/HttpCache/HttpCache.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/HttpKernel/HttpCache/HttpCache.php
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ protected function invalidate(Request $request, $catch = false)
268268
// As per the RFC, invalidate Location and Content-Location URLs if present
269269
foreach (array('Location', 'Content-Location') as $header) {
270270
if ($uri = $response->headers->get($header)) {
271-
$subRequest = $request::create($uri, 'get', array(), array(), array(), $request->server->all());
271+
$subRequest = Request::create($uri, 'get', array(), array(), array(), $request->server->all());
272272

273273
$this->store->invalidate($subRequest);
274274
}

‎src/Symfony/Component/Security/Http/HttpUtils.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Security/Http/HttpUtils.php
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public function createRedirectResponse(Request $request, $path, $status = 302)
7070
*/
7171
public function createRequest(Request $request, $path)
7272
{
73-
$newRequest = $request::create($this->generateUri($request, $path), 'get', array(), $request->cookies->all(), array(), $request->server->all());
73+
$newRequest = Request::create($this->generateUri($request, $path), 'get', array(), $request->cookies->all(), array(), $request->server->all());
7474
if ($request->hasSession()) {
7575
$newRequest->setSession($request->getSession());
7676
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.