From e3b7e2fc7d16a443324a77d85e98c72df4d8d21b Mon Sep 17 00:00:00 2001 From: Nicolas Grekas Date: Sun, 23 Apr 2023 10:17:54 +0200 Subject: [PATCH] Detach stream before serializing PSR-7 response --- .github/workflows/static.yml | 2 +- .github/workflows/tests.yml | 21 +++++++-------------- composer.json | 4 ++-- src/CachePlugin.php | 13 ++++++++----- 4 files changed, 18 insertions(+), 22 deletions(-) diff --git a/.github/workflows/static.yml b/.github/workflows/static.yml index 4e821aa..5762374 100644 --- a/.github/workflows/static.yml +++ b/.github/workflows/static.yml @@ -13,7 +13,7 @@ jobs: steps: - name: Checkout code - uses: actions/checkout@v2 + uses: actions/checkout@v3 - name: PHPStan uses: docker://oskarstark/phpstan-ga diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 1bbd75a..7ed7dc5 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -12,11 +12,11 @@ jobs: runs-on: ubuntu-latest strategy: matrix: - php: ['7.1', '7.2', '7.3', '7.4', '8.0'] + php: ['7.1', '7.2', '7.3', '7.4', '8.0', '8.1', '8.2'] steps: - name: Checkout code - uses: actions/checkout@v2 + uses: actions/checkout@v3 - name: Setup PHP uses: shivammathur/setup-php@v2 @@ -25,15 +25,8 @@ jobs: tools: composer:v2 coverage: none - - name: Install PHP 7 dependencies + - name: Install dependencies run: composer update --prefer-dist --no-interaction --no-progress - if: "matrix.php != '8.0'" - - - name: Install PHP 8 dependencies - run: | - composer require "phpdocumentor/reflection-docblock:^5.2@dev" --no-interaction --no-update - composer update --prefer-dist --prefer-stable --no-interaction --no-progress --ignore-platform-req=php - if: "matrix.php == '8.0'" - name: Execute tests run: composer test @@ -43,11 +36,11 @@ jobs: runs-on: ubuntu-latest strategy: matrix: - php: ['7.1', '7.2', '7.3', '7.4'] + php: ['7.1', '7.2', '7.3', '7.4', '8.0','8.1', '8.2'] steps: - name: Checkout code - uses: actions/checkout@v2 + uses: actions/checkout@v3 - name: Setup PHP uses: shivammathur/setup-php@v2 @@ -70,7 +63,7 @@ jobs: steps: - name: Checkout code - uses: actions/checkout@v2 + uses: actions/checkout@v3 - name: Setup PHP uses: shivammathur/setup-php@v2 @@ -81,7 +74,7 @@ jobs: - name: Install dependencies run: | - composer require "friends-of-phpspec/phpspec-code-coverage:^4.3.2" --no-interaction --no-update + composer require "friends-of-phpspec/phpspec-code-coverage" --no-interaction --no-update composer update --prefer-dist --no-interaction --no-progress - name: Execute tests diff --git a/composer.json b/composer.json index 08dc4bf..601d262 100644 --- a/composer.json +++ b/composer.json @@ -18,7 +18,7 @@ "symfony/options-resolver": "^2.6 || ^3.0 || ^4.0 || ^5.0 || ^6.0" }, "require-dev": { - "phpspec/phpspec": "^5.1 || ^6.0" + "phpspec/phpspec": "^5.1 || ^6.0 || ^7.0" }, "autoload": { "psr-4": { @@ -36,7 +36,7 @@ }, "extra": { "branch-alias": { - "dev-master": "1.6-dev" + "dev-master": "1.7-dev" } } } diff --git a/src/CachePlugin.php b/src/CachePlugin.php index 77de67c..4c2a3e9 100644 --- a/src/CachePlugin.php +++ b/src/CachePlugin.php @@ -195,11 +195,7 @@ protected function doHandleRequest(RequestInterface $request, callable $next, ca if ($this->isCacheable($response) && $this->isCacheableRequest($request)) { $bodyStream = $response->getBody(); $body = $bodyStream->__toString(); - if ($bodyStream->isSeekable()) { - $bodyStream->rewind(); - } else { - $response = $response->withBody($this->streamFactory->createStream($body)); - } + $bodyStream->detach(); $maxAge = $this->getMaxAge($response); $cacheItem @@ -212,6 +208,13 @@ protected function doHandleRequest(RequestInterface $request, callable $next, ca 'etag' => $response->getHeader('ETag'), ]); $this->pool->save($cacheItem); + + $bodyStream = $this->streamFactory->createStream($body); + if ($bodyStream->isSeekable()) { + $bodyStream->rewind(); + } + + $response = $response->withBody($bodyStream); } return $this->handleCacheListeners($request, $response, false, $cacheItem);