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
This commit is contained in:
commit
2cd6e002c7
@ -187,6 +187,8 @@ class Request
|
|||||||
*/
|
*/
|
||||||
protected static $formats;
|
protected static $formats;
|
||||||
|
|
||||||
|
protected static $requestFactory;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructor.
|
* Constructor.
|
||||||
*
|
*
|
||||||
@ -252,7 +254,7 @@ class Request
|
|||||||
*/
|
*/
|
||||||
public static function createFromGlobals()
|
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')
|
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'))
|
&& in_array(strtoupper($request->server->get('REQUEST_METHOD', 'GET')), array('PUT', 'DELETE', 'PATCH'))
|
||||||
@ -370,7 +372,21 @@ class Request
|
|||||||
$server['REQUEST_URI'] = $components['path'].('' !== $queryString ? '?'.$queryString : '');
|
$server['REQUEST_URI'] = $components['path'].('' !== $queryString ? '?'.$queryString : '');
|
||||||
$server['QUERY_STRING'] = $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;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -1807,4 +1823,19 @@ class Request
|
|||||||
|
|
||||||
return false;
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1507,7 +1507,7 @@ class RequestTest extends \PHPUnit_Framework_TestCase
|
|||||||
$this->assertEquals($expectedRequestUri, $request->getRequestUri(), '->getRequestUri() is correct');
|
$this->assertEquals($expectedRequestUri, $request->getRequestUri(), '->getRequestUri() is correct');
|
||||||
|
|
||||||
$subRequestUri = '/bar/foo';
|
$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');
|
$this->assertEquals($subRequestUri, $subRequest->getRequestUri(), '->getRequestUri() is correct in sub request');
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1626,6 +1626,15 @@ class RequestTest extends \PHPUnit_Framework_TestCase
|
|||||||
// reset request for following tests
|
// reset request for following tests
|
||||||
Request::setTrustedHosts(array());
|
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
|
class RequestContentProxy extends Request
|
||||||
@ -1635,3 +1644,11 @@ class RequestContentProxy extends Request
|
|||||||
return http_build_query(array('_method' => 'PUT', 'content' => 'mycontent'));
|
return http_build_query(array('_method' => 'PUT', 'content' => 'mycontent'));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
class NewRequest extends Request
|
||||||
|
{
|
||||||
|
public function getFoo()
|
||||||
|
{
|
||||||
|
return 'foo';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -132,7 +132,7 @@ class InlineFragmentRenderer extends RoutableFragmentRenderer
|
|||||||
|
|
||||||
$server['REMOTE_ADDR'] = '127.0.0.1';
|
$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')) {
|
if ($request->headers->has('Surrogate-Capability')) {
|
||||||
$subRequest->headers->set('Surrogate-Capability', $request->headers->get('Surrogate-Capability'));
|
$subRequest->headers->set('Surrogate-Capability', $request->headers->get('Surrogate-Capability'));
|
||||||
}
|
}
|
||||||
|
@ -268,7 +268,7 @@ class HttpCache implements HttpKernelInterface, TerminableInterface
|
|||||||
// As per the RFC, invalidate Location and Content-Location URLs if present
|
// As per the RFC, invalidate Location and Content-Location URLs if present
|
||||||
foreach (array('Location', 'Content-Location') as $header) {
|
foreach (array('Location', 'Content-Location') as $header) {
|
||||||
if ($uri = $response->headers->get($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);
|
$this->store->invalidate($subRequest);
|
||||||
}
|
}
|
||||||
|
@ -72,7 +72,7 @@ class HttpUtils
|
|||||||
*/
|
*/
|
||||||
public function createRequest(Request $request, $path)
|
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()) {
|
if ($request->hasSession()) {
|
||||||
$newRequest->setSession($request->getSession());
|
$newRequest->setSession($request->getSession());
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user