Skip to content

Commit 1bdf203

Browse files
authored
Merge pull request #16 from tommey/feature/replace-requestbody-with-stream
Changed RequestBody to Stream in convertRequest to avoid unnecessary input stream copy
2 parents 593f3dd + b561de8 commit 1bdf203

File tree

5 files changed

+22
-14
lines changed

5 files changed

+22
-14
lines changed

.github/workflows/static-analysis.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ jobs:
1212
uses: actions/checkout@v2
1313

1414
- name: PHPStan
15-
uses: docker://oskarstark/phpstan-ga:0.12.41
15+
uses: docker://oskarstark/phpstan-ga:0.12.85
1616
env:
1717
REQUIRE_DEV: true
1818
with:
@@ -27,6 +27,6 @@ jobs:
2727
uses: actions/checkout@v2
2828

2929
- name: PHP-CS-Fixer
30-
uses: docker://oskarstark/php-cs-fixer-ga:2.16.4
30+
uses: docker://oskarstark/php-cs-fixer-ga:2.19.0
3131
with:
3232
args: --dry-run --diff-format udiff

.php_cs.dist

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,5 +62,6 @@ return PhpCsFixer\Config::create()
6262
PhpCsFixer\Finder::create()
6363
->in(__DIR__.'/src')
6464
->in(__DIR__.'/test')
65+
->exclude('support/_generated')
6566
->name('*.php')
6667
);

CHANGELOG-1.0.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
55
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
66

7+
## [1.0.1] - 2021-05-07
8+
### Changed
9+
- Changed RequestBody to Stream in convertRequest to avoid unnecessary input stream copy.
10+
711
## [1.0.0] - 2020-10-13
812
### Added
913
- Added Slim v3 support.

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@ coverage:
2121
static: static-phpstan static-cs-check
2222

2323
static-phpstan:
24-
docker run --rm -it -e REQUIRE_DEV=true -v ${PWD}:/app -w /app oskarstark/phpstan-ga:0.12.41 analyze $(PHPSTAN_PARAMS)
24+
docker run --rm -it -e REQUIRE_DEV=true -v ${PWD}:/app -w /app oskarstark/phpstan-ga:0.12.85 analyze $(PHPSTAN_PARAMS)
2525

2626
static-cs-fix:
27-
docker run --rm -it -v ${PWD}:/app -w /app oskarstark/php-cs-fixer-ga:2.16.4 --diff-format udiff $(CS_PARAMS)
27+
docker run --rm -it -v ${PWD}:/app -w /app oskarstark/php-cs-fixer-ga:2.19.0 --diff-format udiff $(CS_PARAMS)
2828

2929
static-cs-check:
3030
$(MAKE) static-cs-fix CS_PARAMS="--dry-run"

src/Lib/Connector/Slim.php

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
use Slim\Http\Environment;
1212
use Slim\Http\Headers;
1313
use Slim\Http\Request;
14-
use Slim\Http\RequestBody;
1514
use Slim\Http\Response;
1615
use Slim\Http\Stream;
1716
use Slim\Http\UploadedFile;
@@ -37,15 +36,9 @@ public function setApp(App $app): void
3736
*/
3837
protected function doRequest($request): BrowserKitResponse
3938
{
40-
$slimRequest = $this->convertRequest($request);
41-
42-
$stream = fopen('php://temp', 'wb+');
43-
if ($stream === false) {
44-
throw new RuntimeException('Could not open `php://temp` stream.');
45-
}
46-
39+
$slimRequest = $this->convertRequest($request);
4740
$headers = new Headers(['Content-Type' => 'text/html; charset=UTF-8']);
48-
$body = new Stream($stream);
41+
$body = $this->createStream();
4942
$slimResponse = new Response(200, $headers, $body);
5043
$slimResponse = $this->app->process($slimRequest, $slimResponse);
5144

@@ -77,7 +70,7 @@ private function convertRequest(BrowserKitRequest $request): Request
7770

7871
$requestContent = $request->getContent();
7972
if ($requestContent !== null) {
80-
$body = new RequestBody();
73+
$body = $this->createStream();
8174
$body->write($requestContent);
8275

8376
$slimRequest = $slimRequest->withBody($body);
@@ -128,4 +121,14 @@ private function createUploadedFile(array $file): UploadedFile
128121
$file['error']
129122
);
130123
}
124+
125+
private function createStream(): Stream
126+
{
127+
$stream = fopen('php://temp', 'wb+');
128+
if ($stream === false) {
129+
throw new RuntimeException('Could not open `php://temp` stream.');
130+
}
131+
132+
return new Stream($stream);
133+
}
131134
}

0 commit comments

Comments
 (0)