Skip to content

Commit

Permalink
Wrap fopen for PHP 8 (#174)
Browse files Browse the repository at this point in the history
* Wrap fopen to not leak PHP 8 throwing ValueError

* Dont expand the public API

Co-authored-by: Nyholm <[email protected]>
  • Loading branch information
Zegnat and Nyholm committed May 10, 2021
1 parent d937173 commit a4a3629
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 10 deletions.
5 changes: 3 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -118,4 +120,3 @@ The `final` keyword was replaced by `@final` annotation.
## 0.2.3

No changelog before this release

2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
9 changes: 7 additions & 2 deletions src/Factory/Psr17Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.');
}

Expand Down
17 changes: 12 additions & 5 deletions src/UploadedFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
Expand Down

0 comments on commit a4a3629

Please sign in to comment.