Skip to content

Commit 00bdb70

Browse files
committed
[HttpFoundation] deprecate using $first in get and added key in all
1 parent 0d8f5fe commit 00bdb70

File tree

9 files changed

+50
-30
lines changed

9 files changed

+50
-30
lines changed

src/Symfony/Component/HttpFoundation/HeaderBag.php

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,18 @@ public function __toString()
5858
/**
5959
* Returns the headers.
6060
*
61+
* @param string|null $key The name of the headers to return or null to get them all
62+
*
6163
* @return array An array of headers
6264
*/
63-
public function all()
65+
public function all(/*string $key = null*/)
6466
{
67+
if (1 <= \func_num_args() && null !== $key = func_get_arg(0)) {
68+
$key = str_replace('_', '-', strtolower($key));
69+
70+
return $this->headers[$key] ?? [];
71+
}
72+
6573
return $this->headers;
6674
}
6775

@@ -103,28 +111,21 @@ public function add(array $headers)
103111
*
104112
* @param string $key The header name
105113
* @param string|null $default The default value
106-
* @param bool $first Whether to return the first value or all header values
107114
*
108-
* @return string|string[]|null The first header value or default value if $first is true, an array of values otherwise
115+
* @return string|null The first header value or default value if $first is true, an array of values otherwise
109116
*/
110-
public function get($key, $default = null, $first = true)
117+
public function get($key, $default = null)
111118
{
112-
$key = str_replace('_', '-', strtolower($key));
113-
$headers = $this->all();
119+
$headers = $this->all((string) $key);
120+
if (2 < \func_num_args()) {
121+
@trigger_error(sprintf('Passing a third argument to "%s()" is deprecated since Symfony 4.4, use method "all()" instead', __METHOD__), E_USER_DEPRECATED);
114122

115-
if (!\array_key_exists($key, $headers)) {
116-
if (null === $default) {
117-
return $first ? null : [];
123+
if (!func_get_arg(2)) {
124+
return $headers;
118125
}
119-
120-
return $first ? $default : [$default];
121-
}
122-
123-
if ($first) {
124-
return \count($headers[$key]) ? $headers[$key][0] : $default;
125126
}
126127

127-
return $headers[$key];
128+
return $headers[0] ?? $default;
128129
}
129130

130131
/**
@@ -181,7 +182,7 @@ public function has($key)
181182
*/
182183
public function contains($key, $value)
183184
{
184-
return \in_array($value, $this->get($key, null, false));
185+
return \in_array($value, $this->all((string) $key));
185186
}
186187

187188
/**

src/Symfony/Component/HttpFoundation/Response.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1024,7 +1024,7 @@ public function hasVary(): bool
10241024
*/
10251025
public function getVary(): array
10261026
{
1027-
if (!$vary = $this->headers->get('Vary', null, false)) {
1027+
if (!$vary = $this->headers->all('Vary')) {
10281028
return [];
10291029
}
10301030

src/Symfony/Component/HttpFoundation/ResponseHeaderBag.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,14 +87,22 @@ public function replace(array $headers = [])
8787

8888
/**
8989
* {@inheritdoc}
90+
*
91+
* @param string|null $key The name of the headers to return or null to get them all
9092
*/
91-
public function all()
93+
public function all(/*string $key = null*/)
9294
{
9395
$headers = parent::all();
9496
foreach ($this->getCookies() as $cookie) {
9597
$headers['set-cookie'][] = (string) $cookie;
9698
}
9799

100+
if (1 <= \func_num_args() && null !== $key = func_get_arg(0)) {
101+
$key = str_replace('_', '-', strtolower($key));
102+
103+
return $headers[$key] ?? [];
104+
}
105+
98106
return $headers;
99107
}
100108

src/Symfony/Component/HttpFoundation/Test/Constraint/ResponseHeaderSame.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public function toString(): string
4040
*/
4141
protected function matches($response): bool
4242
{
43-
return $this->expectedValue === $response->headers->get($this->headerName, null, true);
43+
return $this->expectedValue === $response->headers->get($this->headerName, null);
4444
}
4545

4646
/**

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

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,24 +88,35 @@ public function testGet()
8888
$bag = new HeaderBag(['foo' => 'bar', 'fuzz' => 'bizz']);
8989
$this->assertEquals('bar', $bag->get('foo'), '->get return current value');
9090
$this->assertEquals('bar', $bag->get('FoO'), '->get key in case insensitive');
91-
$this->assertEquals(['bar'], $bag->get('foo', 'nope', false), '->get return the value as array');
91+
$this->assertEquals(['bar'], $bag->all('foo'), '->get return the value as array');
9292

9393
// defaults
9494
$this->assertNull($bag->get('none'), '->get unknown values returns null');
9595
$this->assertEquals('default', $bag->get('none', 'default'), '->get unknown values returns default');
96-
$this->assertEquals(['default'], $bag->get('none', 'default', false), '->get unknown values returns default as array');
96+
$this->assertEquals([], $bag->all('none'), '->get unknown values returns an empty array');
9797

9898
$bag->set('foo', 'bor', false);
9999
$this->assertEquals('bar', $bag->get('foo'), '->get return first value');
100-
$this->assertEquals(['bar', 'bor'], $bag->get('foo', 'nope', false), '->get return all values as array');
100+
$this->assertEquals(['bar', 'bor'], $bag->all('foo'), '->get return all values as array');
101+
}
102+
103+
/**
104+
* @group legacy
105+
* @expectedDeprecation Passing a third argument to "Symfony\Component\HttpFoundation\HeaderBag::get" is deprecated since Symfony 4.4, use method "all()" instead
106+
*/
107+
public function testGetIsEqualToNewMethod()
108+
{
109+
$bag = new HeaderBag(['foo' => 'bar', 'fuzz' => 'bizz']);
110+
$this->assertEquals($bag->get('none', 'default'), $bag->get('none', 'default'), '->get unknown values returns default as array');
111+
$this->assertEquals($bag->all('none'), $bag->get('none', [], false), '->get unknown values returns default as array');
101112
}
102113

103114
public function testSetAssociativeArray()
104115
{
105116
$bag = new HeaderBag();
106117
$bag->set('foo', ['bad-assoc-index' => 'value']);
107118
$this->assertSame('value', $bag->get('foo'));
108-
$this->assertEquals(['value'], $bag->get('foo', 'nope', false), 'assoc indices of multi-valued headers are ignored');
119+
$this->assertEquals(['value'], $bag->all('foo'), 'assoc indices of multi-valued headers are ignored');
109120
}
110121

111122
public function testContains()

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,13 +89,13 @@ public function testCacheControlHeader()
8989

9090
$bag = new ResponseHeaderBag();
9191
$bag->set('Cache-Control', ['public', 'must-revalidate']);
92-
$this->assertCount(1, $bag->get('Cache-Control', null, false));
92+
$this->assertCount(1, $bag->all('Cache-Control'));
9393
$this->assertEquals('must-revalidate, public', $bag->get('Cache-Control'));
9494

9595
$bag = new ResponseHeaderBag();
9696
$bag->set('Cache-Control', 'public');
9797
$bag->set('Cache-Control', 'must-revalidate', false);
98-
$this->assertCount(1, $bag->get('Cache-Control', null, false));
98+
$this->assertCount(1, $bag->all('Cache-Control'));
9999
$this->assertEquals('must-revalidate, public', $bag->get('Cache-Control'));
100100
}
101101

@@ -166,7 +166,7 @@ public function testCookiesWithSameNames()
166166
'foo=bar; path=/path/bar; domain=foo.bar; httponly; samesite=lax',
167167
'foo=bar; path=/path/bar; domain=bar.foo; httponly; samesite=lax',
168168
'foo=bar; path=/; httponly; samesite=lax',
169-
], $bag->get('set-cookie', null, false));
169+
], $bag->all('set-cookie'));
170170

171171
$this->assertSetCookieHeader('foo=bar; path=/path/foo; domain=foo.bar; httponly; samesite=lax', $bag);
172172
$this->assertSetCookieHeader('foo=bar; path=/path/bar; domain=foo.bar; httponly; samesite=lax', $bag);

src/Symfony/Component/HttpKernel/Tests/EventListener/TestSessionListenerTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ public function testSessionWithNewSessionIdAndNewCookieDoesNotSendAnotherCookie(
123123

124124
$response = $this->filterResponse(new Request(), HttpKernelInterface::MASTER_REQUEST, $response);
125125

126-
$this->assertSame($expected, $response->headers->get('Set-Cookie', null, false));
126+
$this->assertSame($expected, $response->headers->all()['set-cookie']);
127127
}
128128

129129
public function anotherCookieProvider()

src/Symfony/Component/WebLink/Tests/EventListener/AddLinkHeaderListenerTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public function testOnKernelResponse()
4747
'</foo>; rel="preload"',
4848
];
4949

50-
$this->assertEquals($expected, $response->headers->get('Link', null, false));
50+
$this->assertEquals($expected, $response->headers->all()['link']);
5151
}
5252

5353
public function testSubscribedEvents()

src/Symfony/Component/WebLink/composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
"symfony/http-kernel": ""
2525
},
2626
"require-dev": {
27-
"symfony/http-foundation": "^3.4|^4.0|^5.0",
27+
"symfony/http-foundation": "^4.4|^5.0",
2828
"symfony/http-kernel": "^4.3|^5.0"
2929
},
3030
"conflict": {

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy