Skip to content

Commit 88dba68

Browse files
committed
Change open resource exception to RuntimeException
1 parent d0ba6dd commit 88dba68

File tree

1 file changed

+21
-2
lines changed

1 file changed

+21
-2
lines changed

Stream.php

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public function __construct(mixed $stream = null, string $permission = "r+")
5757
} else {
5858
$this->stream = $stream;
5959
$this->permission = $permission;
60-
$this->resource = fopen($this->stream, $this->permission);
60+
$this->resource = $this->fopen($this->stream, $this->permission);
6161
$this->meta = $this->getMetadata();
6262
}
6363
}
@@ -297,7 +297,26 @@ public function withContext(array $opts): StreamInterface
297297
{
298298
$inst = clone $this;
299299
$context = stream_context_create($opts);
300-
$inst->resource = fopen($this->stream, $this->permission, false, $context);
300+
$inst->resource = $this->fopen($this->stream, $this->permission, false, $context);
301301
return $inst;
302302
}
303+
304+
/**
305+
* Open a resource correct with the right resource
306+
* @param ...$fArgs
307+
* @return false|resource
308+
* @throws \RuntimeException on error.
309+
*/
310+
private function fopen(...$fArgs)
311+
{
312+
set_error_handler(function ($errorNo, $errorStr) {
313+
throw new RuntimeException('Failed to open stream: ' . $errorStr, $errorNo);
314+
});
315+
try {
316+
$this->resource = fopen(...$fArgs);
317+
} finally {
318+
restore_error_handler();
319+
}
320+
return $this->resource;
321+
}
303322
}

0 commit comments

Comments
 (0)