Skip to content

Commit 1c3c193

Browse files
authored
Steaming large files (#312)
* feature: PUT style BodyStream closes #57 * tweak: remove superglobal usage * test: remove unused test
1 parent d15b0d3 commit 1c3c193

File tree

3 files changed

+43
-1
lines changed

3 files changed

+43
-1
lines changed

phpcs.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
<rule ref="Generic.Files.OneObjectStructurePerFile" />
2626
<rule ref="Generic.Files.OneTraitPerFile" />
2727
<rule ref="Generic.Formatting.DisallowMultipleStatements" />
28-
<rule ref="Generic.Formatting.NoSpaceAfterCast" />
2928
<rule ref="Generic.Functions.FunctionCallArgumentSpacing" />
3029
<rule ref="Generic.Functions.OpeningFunctionBraceKernighanRitchie" />
3130
<rule ref="Generic.Metrics.CyclomaticComplexity" />

src/Input.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Psr\Http\Message\StreamInterface;
1212
use Gt\Input\Trigger\Trigger;
1313
use Gt\Input\InputData\InputData;
14+
use Gt\Input\InputData\Datum\StreamNotAvailableException;
1415
use Gt\Input\InputData\Datum\InputDatum;
1516
use Gt\Input\InputData\KeyValueArrayAccess;
1617
use Gt\Input\InputData\KeyValueCountable;
@@ -38,6 +39,7 @@ class Input implements ArrayAccess, Countable, Iterator {
3839
const DATA_COMBINED = "combined";
3940

4041
protected BodyStream $bodyStream;
42+
protected string $requestMethod;
4143
protected QueryStringInputData $queryStringParameters;
4244
protected BodyInputData $bodyParameters;
4345

@@ -52,8 +54,10 @@ public function __construct(
5254
array $post = [],
5355
array $files = [],
5456
string $bodyPath = "php://input",
57+
?string $requestMethod = "GET",
5558
) {
5659
$this->bodyStream = new BodyStream($bodyPath);
60+
$this->requestMethod = strtoupper($requestMethod);
5761

5862
$this->queryStringParameters = new QueryStringInputData($get);
5963
$this->bodyParameters = new BodyInputData($post);
@@ -73,6 +77,19 @@ public function getStream():StreamInterface {
7377
return $this->bodyStream;
7478
}
7579

80+
/**
81+
* Returns a streamable PUT file upload body.
82+
*/
83+
public function getPutFileStream():BodyStream {
84+
if($this->requestMethod !== "PUT") {
85+
throw new StreamNotAvailableException(
86+
"PUT file stream is only available for PUT requests."
87+
);
88+
}
89+
90+
return $this->bodyStream;
91+
}
92+
7693
public function add(string $key, InputDatum $datum, string $method):void {
7794
switch($method) {
7895
case self::DATA_QUERYSTRING:

test/phpunit/InputTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Gt\Input\Input;
77
use Gt\Input\InputData\Datum\FileUpload;
88
use Gt\Input\InputData\Datum\InputDatum;
9+
use Gt\Input\InputData\Datum\StreamNotAvailableException;
910
use Gt\Input\InputData\InputData;
1011
use Gt\Input\InvalidInputMethodException;
1112
use Gt\Input\MissingInputParameterException;
@@ -62,6 +63,31 @@ public function testBodyStreamContents():void {
6263
self::assertEquals($testMessage, (string)$body);
6364
}
6465

66+
public function testGetPutFileStream_putRequest():void {
67+
$testMessage = "This is a PUT file test message";
68+
$tmpPath = implode(DIRECTORY_SEPARATOR, [
69+
sys_get_temp_dir(),
70+
"phpgt",
71+
"input",
72+
"test",
73+
uniqid(),
74+
]);
75+
mkdir(dirname($tmpPath), 0775, true);
76+
touch($tmpPath);
77+
$fh = fopen($tmpPath, "r+");
78+
fwrite($fh, $testMessage);
79+
80+
$input = new Input([], [], [], $tmpPath, "PUT");
81+
$body = $input->getPutFileStream();
82+
self::assertEquals($testMessage, (string)$body);
83+
}
84+
85+
public function testGetPutFileStream_notPutRequest():void {
86+
self::expectException(StreamNotAvailableException::class);
87+
$input = new Input([], [], [], "php://input", "POST");
88+
$input->getPutFileStream();
89+
}
90+
6591
/** @dataProvider dataRandomGetPost */
6692
public function testGetQueryString(array $get, array $post):void {
6793
$input = new Input($get, $post);

0 commit comments

Comments
 (0)