Skip to content

Commit 25769b2

Browse files
[HttpFoundation] use InputBag for Request::$request only if data is coming from a form
1 parent 86c79ce commit 25769b2

File tree

6 files changed

+53
-36
lines changed

6 files changed

+53
-36
lines changed

src/Symfony/Component/HttpFoundation/InputBag.php

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -36,29 +36,18 @@ public function get(string $key, $default = null)
3636
$value = parent::get($key, $this);
3737

3838
if (null !== $value && $this !== $value && !is_scalar($value) && !(\is_object($value) && method_exists($value, '__toString'))) {
39-
trigger_deprecation('symfony/http-foundation', '5.1', 'Retrieving a non-string value from "%s()" is deprecated, and will throw a "%s" exception in Symfony 6.0, use "%s::all()" instead.', __METHOD__, BadRequestException::class, __CLASS__);
39+
trigger_deprecation('symfony/http-foundation', '5.1', 'Retrieving a non-string value from "%s()" is deprecated, and will throw a "%s" exception in Symfony 6.0, use "%s::all($key)" instead.', __METHOD__, BadRequestException::class, __CLASS__);
4040
}
4141

4242
return $this === $value ? $default : $value;
4343
}
4444

4545
/**
46-
* Returns the inputs.
47-
*
48-
* @param string|null $key The name of the input to return or null to get them all
46+
* {@inheritdoc}
4947
*/
5048
public function all(string $key = null): array
5149
{
52-
if (null === $key) {
53-
return $this->parameters;
54-
}
55-
56-
$value = $this->parameters[$key] ?? [];
57-
if (!\is_array($value)) {
58-
throw new BadRequestException(sprintf('Unexpected value for "%s" input, expecting "array", got "%s".', $key, get_debug_type($value)));
59-
}
60-
61-
return $value;
50+
return parent::all($key);
6251
}
6352

6453
/**

src/Symfony/Component/HttpFoundation/ParameterBag.php

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
namespace Symfony\Component\HttpFoundation;
1313

14+
use Symfony\Component\HttpFoundation\Exception\BadRequestException;
15+
1416
/**
1517
* ParameterBag is a container for key/value pairs.
1618
*
@@ -31,11 +33,23 @@ public function __construct(array $parameters = [])
3133
/**
3234
* Returns the parameters.
3335
*
36+
* @param string|null $key The name of the parameter to return or null to get them all
37+
*
3438
* @return array An array of parameters
3539
*/
36-
public function all()
40+
public function all(/*string $key = null*/)
3741
{
38-
return $this->parameters;
42+
$key = \func_num_args() > 0 ? func_get_arg(0) : null;
43+
44+
if (null === $key) {
45+
return $this->parameters;
46+
}
47+
48+
if (!\is_array($value = $this->parameters[$key] ?? [])) {
49+
throw new BadRequestException(sprintf('Unexpected value for parameter "%s": expecting "array", got "%s".', $key, get_debug_type($value)));
50+
}
51+
52+
return $value;
3953
}
4054

4155
/**

src/Symfony/Component/HttpFoundation/Request.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ class Request
8585
/**
8686
* Request body parameters ($_POST).
8787
*
88-
* @var InputBag
88+
* @var InputBag|ParameterBag
8989
*/
9090
public $request;
9191

@@ -268,7 +268,7 @@ public function __construct(array $query = [], array $request = [], array $attri
268268
*/
269269
public function initialize(array $query = [], array $request = [], array $attributes = [], array $cookies = [], array $files = [], array $server = [], $content = null)
270270
{
271-
$this->request = new InputBag($request);
271+
$this->request = new ParameterBag($request);
272272
$this->query = new InputBag($query);
273273
$this->attributes = new ParameterBag($attributes);
274274
$this->cookies = new InputBag($cookies);
@@ -298,7 +298,9 @@ public static function createFromGlobals()
298298
{
299299
$request = self::createRequestFromFactory($_GET, $_POST, [], $_COOKIE, $_FILES, $_SERVER);
300300

301-
if (0 === strpos($request->headers->get('CONTENT_TYPE'), 'application/x-www-form-urlencoded')
301+
if ($_POST) {
302+
$request->request = new InputBag($_POST);
303+
} elseif (0 === strpos($request->headers->get('CONTENT_TYPE'), 'application/x-www-form-urlencoded')
302304
&& \in_array(strtoupper($request->server->get('REQUEST_METHOD', 'GET')), ['PUT', 'DELETE', 'PATCH'])
303305
) {
304306
parse_str($request->getContent(), $data);
@@ -447,7 +449,7 @@ public function duplicate(array $query = null, array $request = null, array $att
447449
$dup->query = new InputBag($query);
448450
}
449451
if (null !== $request) {
450-
$dup->request = new InputBag($request);
452+
$dup->request = new ParameterBag($request);
451453
}
452454
if (null !== $attributes) {
453455
$dup->attributes = new ParameterBag($attributes);

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

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -36,21 +36,6 @@ public function testGetDoesNotUseDeepByDefault()
3636
$this->assertNull($bag->get('foo[bar]'));
3737
}
3838

39-
public function testAllWithInputKey()
40-
{
41-
$bag = new InputBag(['foo' => ['bar', 'baz'], 'null' => null]);
42-
43-
$this->assertEquals(['bar', 'baz'], $bag->all('foo'), '->all() gets the value of a parameter');
44-
$this->assertEquals([], $bag->all('unknown'), '->all() returns an empty array if a parameter is not defined');
45-
}
46-
47-
public function testAllThrowsForNonArrayValues()
48-
{
49-
$this->expectException(BadRequestException::class);
50-
$bag = new InputBag(['foo' => 'bar', 'null' => null]);
51-
$bag->all('foo');
52-
}
53-
5439
public function testFilterArray()
5540
{
5641
$bag = new InputBag([
@@ -77,7 +62,7 @@ public function testSetWithNonStringishOrArrayIsDeprecated()
7762
public function testGettingANonStringValueIsDeprecated()
7863
{
7964
$bag = new InputBag(['foo' => ['a', 'b']]);
80-
$this->expectDeprecation('Since symfony/http-foundation 5.1: Retrieving a non-string value from "Symfony\Component\HttpFoundation\InputBag::get()" is deprecated, and will throw a "Symfony\Component\HttpFoundation\Exception\BadRequestException" exception in Symfony 6.0, use "Symfony\Component\HttpFoundation\InputBag::all()" instead.');
65+
$this->expectDeprecation('Since symfony/http-foundation 5.1: Retrieving a non-string value from "Symfony\Component\HttpFoundation\InputBag::get()" is deprecated, and will throw a "Symfony\Component\HttpFoundation\Exception\BadRequestException" exception in Symfony 6.0, use "Symfony\Component\HttpFoundation\InputBag::all($key)" instead.');
8166
$bag->get('foo');
8267
}
8368

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\HttpFoundation\Tests;
1313

1414
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\HttpFoundation\Exception\BadRequestException;
1516
use Symfony\Component\HttpFoundation\ParameterBag;
1617

1718
class ParameterBagTest extends TestCase
@@ -27,6 +28,21 @@ public function testAll()
2728
$this->assertEquals(['foo' => 'bar'], $bag->all(), '->all() gets all the input');
2829
}
2930

31+
public function testAllWithInputKey()
32+
{
33+
$bag = new ParameterBag(['foo' => ['bar', 'baz'], 'null' => null]);
34+
35+
$this->assertEquals(['bar', 'baz'], $bag->all('foo'), '->all() gets the value of a parameter');
36+
$this->assertEquals([], $bag->all('unknown'), '->all() returns an empty array if a parameter is not defined');
37+
}
38+
39+
public function testAllThrowsForNonArrayValues()
40+
{
41+
$this->expectException(BadRequestException::class);
42+
$bag = new ParameterBag(['foo' => 'bar', 'null' => null]);
43+
$bag->all('foo');
44+
}
45+
3046
public function testKeys()
3147
{
3248
$bag = new ParameterBag(['foo' => 'bar']);

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313

1414
use PHPUnit\Framework\TestCase;
1515
use Symfony\Component\HttpFoundation\Exception\SuspiciousOperationException;
16+
use Symfony\Component\HttpFoundation\InputBag;
17+
use Symfony\Component\HttpFoundation\ParameterBag;
1618
use Symfony\Component\HttpFoundation\Request;
1719
use Symfony\Component\HttpFoundation\Session\Session;
1820
use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage;
@@ -1255,6 +1257,11 @@ public function testCreateFromGlobals($method)
12551257
{
12561258
$normalizedMethod = strtoupper($method);
12571259

1260+
$_POST = [];
1261+
$request = Request::createFromGlobals();
1262+
$this->assertNotInstanceOf(InputBag::class, $request->request);
1263+
$this->assertInstanceOf(ParameterBag::class, $request->request);
1264+
12581265
$_GET['foo1'] = 'bar1';
12591266
$_POST['foo2'] = 'bar2';
12601267
$_COOKIE['foo3'] = 'bar3';
@@ -1267,6 +1274,8 @@ public function testCreateFromGlobals($method)
12671274
$this->assertEquals('bar3', $request->cookies->get('foo3'), '::fromGlobals() uses values from $_COOKIE');
12681275
$this->assertEquals(['bar4'], $request->files->get('foo4'), '::fromGlobals() uses values from $_FILES');
12691276
$this->assertEquals('bar5', $request->server->get('foo5'), '::fromGlobals() uses values from $_SERVER');
1277+
$this->assertInstanceOf(InputBag::class, $request->request);
1278+
$this->assertInstanceOf(ParameterBag::class, $request->request);
12701279

12711280
unset($_GET['foo1'], $_POST['foo2'], $_COOKIE['foo3'], $_FILES['foo4'], $_SERVER['foo5']);
12721281

@@ -1275,6 +1284,8 @@ public function testCreateFromGlobals($method)
12751284
$request = RequestContentProxy::createFromGlobals();
12761285
$this->assertEquals($normalizedMethod, $request->getMethod());
12771286
$this->assertEquals('mycontent', $request->request->get('content'));
1287+
$this->assertInstanceOf(InputBag::class, $request->request);
1288+
$this->assertInstanceOf(ParameterBag::class, $request->request);
12781289

12791290
unset($_SERVER['REQUEST_METHOD'], $_SERVER['CONTENT_TYPE']);
12801291

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