Skip to content

Commit a4a3629

Browse files
ZegnatNyholm
andauthored
Wrap fopen for PHP 8 (#174)
* Wrap fopen to not leak PHP 8 throwing ValueError * Dont expand the public API Co-authored-by: Nyholm <[email protected]>
1 parent d937173 commit a4a3629

File tree

4 files changed

+23
-10
lines changed

4 files changed

+23
-10
lines changed

CHANGELOG.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@
22

33
All notable changes to this project will be documented in this file, in reverse chronological order by release.
44

5-
## Unreleased
5+
## 1.4.1
66

77
### Fixed
88

99
- `Stream::create` with a string needs to rewind the created memory stream.
10+
- `Psr17Factory::createStreamFromFile`, `UploadedFile::moveTo`, and
11+
`UploadedFile::getStream` no longer throw `ValueError` in PHP 8.
1012

1113
## 1.4.0
1214

@@ -118,4 +120,3 @@ The `final` keyword was replaced by `@final` annotation.
118120
## 0.2.3
119121

120122
No changelog before this release
121-

composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
"require-dev": {
2424
"phpunit/phpunit": "^7.5 || 8.5 || 9.4",
2525
"php-http/psr7-integration-tests": "^1.0",
26-
"http-interop/http-factory-tests": "^0.8",
26+
"http-interop/http-factory-tests": "^0.9",
2727
"symfony/error-handler": "^4.4"
2828
},
2929
"provide": {

src/Factory/Psr17Factory.php

+7-2
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,14 @@ public function createStream(string $content = ''): StreamInterface
3737

3838
public function createStreamFromFile(string $filename, string $mode = 'r'): StreamInterface
3939
{
40-
$resource = @\fopen($filename, $mode);
40+
try {
41+
$resource = @\fopen($filename, $mode);
42+
} catch (\Throwable $e) {
43+
throw new \RuntimeException('The file ' . $filename . ' cannot be opened.');
44+
}
45+
4146
if (false === $resource) {
42-
if ('' === $mode || false === \in_array($mode[0], ['r', 'w', 'a', 'x', 'c'])) {
47+
if ('' === $mode || false === \in_array($mode[0], ['r', 'w', 'a', 'x', 'c'], true)) {
4348
throw new \InvalidArgumentException('The mode ' . $mode . ' is invalid.');
4449
}
4550

src/UploadedFile.php

+12-5
Original file line numberDiff line numberDiff line change
@@ -114,9 +114,11 @@ public function getStream(): StreamInterface
114114
return $this->stream;
115115
}
116116

117-
$resource = \fopen($this->file, 'r');
118-
119-
return Stream::create($resource);
117+
try {
118+
return Stream::create(\fopen($this->file, 'r'));
119+
} catch (\Throwable $e) {
120+
throw new \RuntimeException('The file ' . $this->file . ' cannot be opened.');
121+
}
120122
}
121123

122124
public function moveTo($targetPath): void
@@ -135,8 +137,13 @@ public function moveTo($targetPath): void
135137
$stream->rewind();
136138
}
137139

138-
// Copy the contents of a stream into another stream until end-of-file.
139-
$dest = Stream::create(\fopen($targetPath, 'w'));
140+
try {
141+
// Copy the contents of a stream into another stream until end-of-file.
142+
$dest = Stream::create(\fopen($targetPath, 'w'));
143+
} catch (\Throwable $e) {
144+
throw new \RuntimeException('The file ' . $targetPath . ' cannot be opened.');
145+
}
146+
140147
while (!$stream->eof()) {
141148
if (!$dest->write($stream->read(1048576))) {
142149
break;

0 commit comments

Comments
 (0)