8000 merged branch msonnabaum/request-get-encodings (PR #8185) · symfony/symfony@7eb4fde · GitHub
[go: up one dir, main page]

Skip to content {"props":{"docsUrl":"https://docs.github.com/get-started/accessibility/keyboard-shortcuts"}}

Commit 7eb4fde

Browse files
committed
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
2 parents 35bdf82 + 28a8443 commit 7eb4fde

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

src/Symfony/Component/HttpFoundation/Request.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,7 @@ public function initialize(array $query = array(), array $request = array(), arr
218218
$this->content = $content;
219219
$this->languages = null;
220220
$this->charsets = null;
221+
$this->encodings = null;
221222
$this->acceptableContentTypes = null;
222223
$this->pathInfo = null;
223224
$this->requestUri = null;
@@ -386,6 +387,7 @@ public function duplicate(array $query = null, array $request = null, array $att
386387
}
387388
$dup->languages = null;
388389
$dup->charsets = null;
390+
$dup->encodings = null;
389391
$dup->acceptableContentTypes = null;
390392
$dup->pathInfo = null;
391393
$dup->requestUri = null;
@@ -1444,6 +1446,21 @@ public function getCharsets()
14441446
return $this->charsets = array_keys(AcceptHeader::fromString($this->headers->get('Accept-Charset'))->all());
14451447
}
14461448

1449+
/**
1450+
* Gets a list of encodings acceptable by the client browser.
1451+
*
1452+
* @return array List of encodings in preferable order
1453+
*
1454+
* @api
1455+
*/
1456+
public function getEncodings()
1457+
{
1458+
if (null !== $this->encodings) {
1459+
return $this->encodings;
1460+
}
1461+
return $this->encodings = array_keys(AcceptHeader::fromString($this->headers->get('Accept-Encoding'))->all());
1462+
}
1463+
14471464
/**
14481465
* Gets a list of content types acceptable by the client browser
14491466
*

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1122,6 +1122,22 @@ public function testGetCharsets()
11221122
$this->assertEquals(array('ISO-8859-1', 'utf-8', '*'), $request->getCharsets());
11231123
}
11241124

1125+
public function testGetEncodings()
1126+
{
1127+
$request = new Request();
1128+
$this->assertEquals(array(), $request->getEncodings());
1129+
$request->headers->set('Accept-Encoding', 'gzip,deflate,sdch');
1130+
$this->assertEquals(array(), $request->getEncodings()); // testing caching
1131+
1132+
$request = new Request();
1133+
$request->headers->set('Accept-Encoding', 'gzip,deflate,sdch');
1134+
$this->assertEquals(array('gzip', 'deflate', 'sdch'), $request->getEncodings());
1135+
1136+
$request = new Request();
1137+
$request->headers->set('Accept-Encoding', 'gzip;q=0.4,deflate;q=0.9,compress;q=0.7');
1138+
$this->assertEquals(array('deflate', 'compress', 'gzip'), $request->getEncodings());
1139+
}
1140+
11251141
public function testGetAcceptableContentTypes()
11261142
{
11271143
$request = new Request();

0 commit comments

Comments
 (0)
0