Skip to content

Commit

Permalink
Merge pull request #16 from tommey/feature/replace-requestbody-with-s…
Browse files Browse the repository at this point in the history
…tream

Changed RequestBody to Stream in convertRequest to avoid unnecessary input stream copy
  • Loading branch information
tommey authored May 7, 2021
2 parents 593f3dd + b561de8 commit 1bdf203
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 14 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/static-analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
uses: actions/checkout@v2

- name: PHPStan
uses: docker://oskarstark/phpstan-ga:0.12.41
uses: docker://oskarstark/phpstan-ga:0.12.85
env:
REQUIRE_DEV: true
with:
Expand All @@ -27,6 +27,6 @@ jobs:
uses: actions/checkout@v2

- name: PHP-CS-Fixer
uses: docker://oskarstark/php-cs-fixer-ga:2.16.4
uses: docker://oskarstark/php-cs-fixer-ga:2.19.0
with:
args: --dry-run --diff-format udiff
1 change: 1 addition & 0 deletions .php_cs.dist
Original file line number Diff line number Diff line change
Expand Up @@ -62,5 +62,6 @@ return PhpCsFixer\Config::create()
PhpCsFixer\Finder::create()
->in(__DIR__.'/src')
->in(__DIR__.'/test')
->exclude('support/_generated')
->name('*.php')
);
4 changes: 4 additions & 0 deletions CHANGELOG-1.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## [1.0.1] - 2021-05-07
### Changed
- Changed RequestBody to Stream in convertRequest to avoid unnecessary input stream copy.

## [1.0.0] - 2020-10-13
### Added
- Added Slim v3 support.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ coverage:
static: static-phpstan static-cs-check

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

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

static-cs-check:
$(MAKE) static-cs-fix CS_PARAMS="--dry-run"
23 changes: 13 additions & 10 deletions src/Lib/Connector/Slim.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
use Slim\Http\Environment;
use Slim\Http\Headers;
use Slim\Http\Request;
use Slim\Http\RequestBody;
use Slim\Http\Response;
use Slim\Http\Stream;
use Slim\Http\UploadedFile;
Expand All @@ -37,15 +36,9 @@ public function setApp(App $app): void
*/
protected function doRequest($request): BrowserKitResponse
{
$slimRequest = $this->convertRequest($request);

$stream = fopen('php://temp', 'wb+');
if ($stream === false) {
throw new RuntimeException('Could not open `php://temp` stream.');
}

$slimRequest = $this->convertRequest($request);
$headers = new Headers(['Content-Type' => 'text/html; charset=UTF-8']);
$body = new Stream($stream);
$body = $this->createStream();
$slimResponse = new Response(200, $headers, $body);
$slimResponse = $this->app->process($slimRequest, $slimResponse);

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

$requestContent = $request->getContent();
if ($requestContent !== null) {
$body = new RequestBody();
$body = $this->createStream();
$body->write($requestContent);

$slimRequest = $slimRequest->withBody($body);
Expand Down Expand Up @@ -128,4 +121,14 @@ private function createUploadedFile(array $file): UploadedFile
$file['error']
);
}

private function createStream(): Stream
{
$stream = fopen('php://temp', 'wb+');
if ($stream === false) {
throw new RuntimeException('Could not open `php://temp` stream.');
}

return new Stream($stream);
}
}

0 comments on commit 1bdf203

Please sign in to comment.