From a4a362944244ed20a6bbbecacc882abc8045585a Mon Sep 17 00:00:00 2001 From: Martijn van der Ven Date: Mon, 10 May 2021 22:13:32 +0200 Subject: [PATCH] Wrap fopen for PHP 8 (#174) * Wrap fopen to not leak PHP 8 throwing ValueError * Dont expand the public API Co-authored-by: Nyholm --- CHANGELOG.md | 5 +++-- composer.json | 2 +- src/Factory/Psr17Factory.php | 9 +++++++-- src/UploadedFile.php | 17 ++++++++++++----- 4 files changed, 23 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2a761d7..7a626f2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,11 +2,13 @@ All notable changes to this project will be documented in this file, in reverse chronological order by release. -## Unreleased +## 1.4.1 ### Fixed - `Stream::create` with a string needs to rewind the created memory stream. +- `Psr17Factory::createStreamFromFile`, `UploadedFile::moveTo`, and + `UploadedFile::getStream` no longer throw `ValueError` in PHP 8. ## 1.4.0 @@ -118,4 +120,3 @@ The `final` keyword was replaced by `@final` annotation. ## 0.2.3 No changelog before this release - diff --git a/composer.json b/composer.json index 0741e74..fffdec0 100644 --- a/composer.json +++ b/composer.json @@ -23,7 +23,7 @@ "require-dev": { "phpunit/phpunit": "^7.5 || 8.5 || 9.4", "php-http/psr7-integration-tests": "^1.0", - "http-interop/http-factory-tests": "^0.8", + "http-interop/http-factory-tests": "^0.9", "symfony/error-handler": "^4.4" }, "provide": { diff --git a/src/Factory/Psr17Factory.php b/src/Factory/Psr17Factory.php index 0da5acd..0726b68 100644 --- a/src/Factory/Psr17Factory.php +++ b/src/Factory/Psr17Factory.php @@ -37,9 +37,14 @@ public function createStream(string $content = ''): StreamInterface public function createStreamFromFile(string $filename, string $mode = 'r'): StreamInterface { - $resource = @\fopen($filename, $mode); + try { + $resource = @\fopen($filename, $mode); + } catch (\Throwable $e) { + throw new \RuntimeException('The file ' . $filename . ' cannot be opened.'); + } + if (false === $resource) { - if ('' === $mode || false === \in_array($mode[0], ['r', 'w', 'a', 'x', 'c'])) { + if ('' === $mode || false === \in_array($mode[0], ['r', 'w', 'a', 'x', 'c'], true)) { throw new \InvalidArgumentException('The mode ' . $mode . ' is invalid.'); } diff --git a/src/UploadedFile.php b/src/UploadedFile.php index 415bf87..3c0d098 100644 --- a/src/UploadedFile.php +++ b/src/UploadedFile.php @@ -114,9 +114,11 @@ public function getStream(): StreamInterface return $this->stream; } - $resource = \fopen($this->file, 'r'); - - return Stream::create($resource); + try { + return Stream::create(\fopen($this->file, 'r')); + } catch (\Throwable $e) { + throw new \RuntimeException('The file ' . $this->file . ' cannot be opened.'); + } } public function moveTo($targetPath): void @@ -135,8 +137,13 @@ public function moveTo($targetPath): void $stream->rewind(); } - // Copy the contents of a stream into another stream until end-of-file. - $dest = Stream::create(\fopen($targetPath, 'w')); + try { + // Copy the contents of a stream into another stream until end-of-file. + $dest = Stream::create(\fopen($targetPath, 'w')); + } catch (\Throwable $e) { + throw new \RuntimeException('The file ' . $targetPath . ' cannot be opened.'); + } + while (!$stream->eof()) { if (!$dest->write($stream->read(1048576))) { break;