merged branch msonnabaum/request-get-encodings (PR #8185)

This PR was merged into the master branch.

Discussion
----------

[HttpFoundation] Added Request::getEncodings() method

| Q             | A
| ------------- | ---
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets |
| License       | MIT
| Doc PR        |

We needed access to the Accept-Encoding information in Drupal, and I was surprised to see that there wasn't a method on the request object to access this.

This PR adds that method.

Commits
-------

28a8443 [HttpFoundation] Added Request::getEncodings() method
This commit is contained in:
Fabien Potencier 2013-06-13 09:24:40 +02:00
commit 7eb4fdee64
2 changed files with 33 additions and 0 deletions

View File

@ -218,6 +218,7 @@ class Request
$this->content = $content;
$this->languages = null;
$this->charsets = null;
$this->encodings = null;
$this->acceptableContentTypes = null;
$this->pathInfo = null;
$this->requestUri = null;
@ -386,6 +387,7 @@ class Request
}
$dup->languages = null;
$dup->charsets = null;
$dup->encodings = null;
$dup->acceptableContentTypes = null;
$dup->pathInfo = null;
$dup->requestUri = null;
@ -1444,6 +1446,21 @@ class Request
return $this->charsets = array_keys(AcceptHeader::fromString($this->headers->get('Accept-Charset'))->all());
}
/**
* Gets a list of encodings acceptable by the client browser.
*
* @return array List of encodings in preferable order
*
* @api
*/
public function getEncodings()
{
if (null !== $this->encodings) {
return $this->encodings;
}
return $this->encodings = array_keys(AcceptHeader::fromString($this->headers->get('Accept-Encoding'))->all());
}
/**
* Gets a list of content types acceptable by the client browser
*

View File

@ -1122,6 +1122,22 @@ class RequestTest extends \PHPUnit_Framework_TestCase
$this->assertEquals(array('ISO-8859-1', 'utf-8', '*'), $request->getCharsets());
}
public function testGetEncodings()
{
$request = new Request();
$this->assertEquals(array(), $request->getEncodings());
$request->headers->set('Accept-Encoding', 'gzip,deflate,sdch');
$this->assertEquals(array(), $request->getEncodings()); // testing caching
$request = new Request();
$request->headers->set('Accept-Encoding', 'gzip,deflate,sdch');
$this->assertEquals(array('gzip', 'deflate', 'sdch'), $request->getEncodings());
$request = new Request();
$request->headers->set('Accept-Encoding', 'gzip;q=0.4,deflate;q=0.9,compress;q=0.7');
$this->assertEquals(array('deflate', 'compress', 'gzip'), $request->getEncodings());
}
public function testGetAcceptableContentTypes()
{
$request = new Request();