Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wrap fopen for PHP 8 #174

Merged
merged 2 commits into from
May 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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