Skip to content

Commit bb1490d

Browse files
committed
refactor(http): address feedbacks
1 parent 11c2581 commit bb1490d

3 files changed

Lines changed: 29 additions & 1 deletion

File tree

system/HTTP/IncomingRequest.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,11 @@ class IncomingRequest extends Request
123123
*/
124124
protected $userAgent;
125125

126+
/**
127+
* Typed input data selector.
128+
*/
129+
protected ?RequestInput $input = null;
130+
126131
/**
127132
* Constructor
128133
*
@@ -169,6 +174,11 @@ public function __construct($config, ?URI $uri = null, $body = 'php://input', ?U
169174
$this->detectLocale($config);
170175
}
171176

177+
public function __clone()
178+
{
179+
$this->input = null;
180+
}
181+
172182
private function getPostMaxSize(): int
173183
{
174184
$postMaxSize = ini_get('post_max_size');
@@ -574,7 +584,7 @@ public function getGet($index = null, $filter = null, $flags = null)
574584
*/
575585
public function input(): RequestInput
576586
{
577-
return new RequestInput($this, service('inputdatafactory'));
587+
return $this->input ??= new RequestInput($this, service('inputdatafactory'));
578588
}
579589

580590
/**

tests/system/HTTP/RequestInputTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,23 @@ public function testInputReturnsRequestInput(): void
5656
$this->assertInstanceOf(RequestInput::class, $input);
5757
}
5858

59+
public function testInputReturnsSameRequestInputInstance(): void
60+
{
61+
$request = $this->createRequest();
62+
63+
$this->assertSame($request->input(), $request->input());
64+
}
65+
66+
public function testClonedRequestGetsNewRequestInputInstance(): void
67+
{
68+
$request = $this->createRequest();
69+
$input = $request->input();
70+
71+
$clonedRequest = $request->withMethod(Method::POST);
72+
73+
$this->assertNotSame($input, $clonedRequest->input());
74+
}
75+
5976
public function testGetReadsGetData(): void
6077
{
6178
service('superglobals')->setGet('page', '3');

user_guide_src/source/incoming/incomingrequest.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,7 @@ Filtering a POST variable would look like this:
251251

252252
All of the methods mentioned above support the filter type passed in as the second parameter, with the
253253
exception of ``getJSON()`` and ``getRawInput()``.
254+
The typed input helpers returned by ``input()`` do not accept filter parameters.
254255

255256
Retrieving Headers
256257
******************

0 commit comments

Comments
 (0)