Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2cd6e00

Browse files
committed
feature#8957 [HttpFoundation] added a way to override the Request class (fabpot)
This PR was merged into the master branch. Discussion ---------- [HttpFoundation] added a way to override the Request class | Q | A | ------------- | --- | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #7461, #7453 | License | MIT | Doc PR | symfony/symfony-docs#3021 This is an alternative implementation for #7461. I've also reverted #7381 and #7390 as these changes are not needed anymore. Todo: - [ ] add some tests Commits ------- 464439d [HttpFoundation] added a way to override the Request class
2 parents d80e840 + 464439d commit 2cd6e00

File tree

5 files changed

+54
-6
lines changed

5 files changed

+54
-6
lines changed

src/Symfony/Component/HttpFoundation/Request.php

Lines 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'))
@@ -370,7 +372,21 @@ public static function create($uri, $method = 'GET', $parameters = array(), $coo
370372
$server['REQUEST_URI'] = $components['path'].('' !== $queryString ? '?'.$queryString : '');
371373
$server['QUERY_STRING'] = $queryString;
372374

373-
return new static($query, $request, array(), $cookies, $files, $server, $content);
375+
return self::createRequestFromFactory($query, $request, array(), $cookies, $files, $server, $content);
376+
}
377+
378+
/**
379+
* Sets a callable able to create a Request instance.
380+
*
381+
* This is mainly useful when you need to override the Request class
382+
* to keep BC with an existing system. It should not be used for any
383+
* other purpose.
384+
*
385+
* @param callable $callable A PHP callable
386+
*/
387+
public static function setFactory($callable)
388+
{
389+
self::$requestFactory = $callable;
374390
}
375391

376392
/**
@@ -1807,4 +1823,19 @@ private function getUrlencodedPrefix($string, $prefix)
18071823

18081824
return false;
18091825
}
1826+
1827+
private static function createRequestFromFactory(array $query = array(), array $request = array(), array $attributes = array(), array $cookies = array(), array $files = array(), array $server = array(), $content = null)
1828+
{
1829+
if (self::$requestFactory) {
1830+
$request = call_user_func(self::$requestFactory, $query, $request, $attributes, $cookies, $files, $server, $content);
1831+
1832+
if (!$request instanceof Request) {
1833+
throw new \LogicException('The Request factory must return an instance of Symfony\Component\HttpFoundation\Request.');
1834+
}
1835+
1836+
return $request;
1837+
}
1838+
1839+
return new static($query, $request, $attributes, $cookies, $files, $server, $content);
1840+
}
18101841
}

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

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1507,7 +1507,7 @@ public function testIISRequestUri($headers, $server, $expectedRequestUri)
15071507
$this->assertEquals($expectedRequestUri, $request->getRequestUri(), '->getRequestUri() is correct');
15081508

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

@@ -1626,6 +1626,15 @@ public function testTrustedHosts()
16261626
// reset request for following tests
16271627
Request::setTrustedHosts(array());
16281628
}
1629+
1630+
public function testFactory()
1631+
{
1632+
Request::setFactory(function (array $query = array(), array $request = array(), array $attributes = array(), array $cookies = array(), array $files = array(), array $server = array(), $content = null) {
1633+
return new NewRequest();
1634+
});
1635+
1636+
$this->assertEquals('foo', Request::create('/')->getFoo());
1637+
}
16291638
}
16301639

16311640
class RequestContentProxy extends Request
@@ -1635,3 +1644,11 @@ public function getContent($asResource = false)
16351644
return http_build_query(array('_method' => 'PUT', 'content' => 'mycontent'));
16361645
}
16371646
}
1647+
1648+
class NewRequest extends Request
1649+
{
1650+
public function getFoo()
1651+
{
1652+
return 'foo';
1653+
}
1654+
}

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

Lines 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

Lines 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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public function createRedirectResponse(Request $request, $path, $status = 302)
7272
*/
7373
public function createRequest(Request $request, $path)
7474
{
75-
$newRequest = $request::create($this->generateUri($request, $path), 'get', array(), $request->cookies->all(), array(), $request->server->all());
75+
$newRequest = Request::create($this->generateUri($request, $path), 'get', array(), $request->cookies->all(), array(), $request->server->all());
7676
if ($request->hasSession()) {
7777
$newRequest->setSession($request->getSession());
7878
}

0 commit comments

Comments
 (0)