Skip to content

Commit 9d12cec

Browse files
committed
fix for #951
1 parent e07f7e7 commit 9d12cec

File tree

4 files changed

+138
-70
lines changed

4 files changed

+138
-70
lines changed

api.include.php

Lines changed: 65 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1515,18 +1515,16 @@ public function createStream(string $content = ''): StreamInterface
15151515

15161516
public function createStreamFromFile(string $filename, string $mode = 'r'): StreamInterface
15171517
{
1518-
try {
1519-
$resource = @\fopen($filename, $mode);
1520-
} catch (\Throwable $e) {
1521-
throw new \RuntimeException(\sprintf('The file "%s" cannot be opened.', $filename));
1518+
if ('' === $filename) {
1519+
throw new \RuntimeException('Path cannot be empty');
15221520
}
15231521

1524-
if (false === $resource) {
1522+
if (false === $resource = @\fopen($filename, $mode)) {
15251523
if ('' === $mode || false === \in_array($mode[0], ['r', 'w', 'a', 'x', 'c'], true)) {
15261524
throw new \InvalidArgumentException(\sprintf('The mode "%s" is invalid.', $mode));
15271525
}
15281526

1529-
throw new \RuntimeException(\sprintf('The file "%s" cannot be opened.', $filename));
1527+
throw new \RuntimeException(\sprintf('The file "%s" cannot be opened: %s', $filename, \error_get_last()['message'] ?? ''));
15301528
}
15311529

15321530
return Stream::create($resource);
@@ -2087,6 +2085,9 @@ public function getUploadedFiles(): array
20872085
return $this->uploadedFiles;
20882086
}
20892087

2088+
/**
2089+
* @return static
2090+
*/
20902091
public function withUploadedFiles(array $uploadedFiles)
20912092
{
20922093
$new = clone $this;
@@ -2100,6 +2101,9 @@ public function getCookieParams(): array
21002101
return $this->cookieParams;
21012102
}
21022103

2104+
/**
2105+
* @return static
2106+
*/
21032107
public function withCookieParams(array $cookies)
21042108
{
21052109
$new = clone $this;
@@ -2113,6 +2117,9 @@ public function getQueryParams(): array
21132117
return $this->queryParams;
21142118
}
21152119

2120+
/**
2121+
* @return static
2122+
*/
21162123
public function withQueryParams(array $query)
21172124
{
21182125
$new = clone $this;
@@ -2121,11 +2128,17 @@ public function withQueryParams(array $query)
21212128
return $new;
21222129
}
21232130

2131+
/**
2132+
* @return array|object|null
2133+
*/
21242134
public function getParsedBody()
21252135
{
21262136
return $this->parsedBody;
21272137
}
21282138

2139+
/**
2140+
* @return static
2141+
*/
21292142
public function withParsedBody($data)
21302143
{
21312144
if (!\is_array($data) && !\is_object($data) && null !== $data) {
@@ -2143,6 +2156,9 @@ public function getAttributes(): array
21432156
return $this->attributes;
21442157
}
21452158

2159+
/**
2160+
* @return mixed
2161+
*/
21462162
public function getAttribute($attribute, $default = null)
21472163
{
21482164
if (false === \array_key_exists($attribute, $this->attributes)) {
@@ -2358,16 +2374,20 @@ public function getSize() /*:?int*/
23582374

23592375
public function tell(): int
23602376
{
2361-
if (false === $result = \ftell($this->stream)) {
2362-
throw new \RuntimeException('Unable to determine stream position');
2377+
if (!isset($this->stream)) {
2378+
throw new \RuntimeException('Stream is detached');
2379+
}
2380+
2381+
if (false === $result = @\ftell($this->stream)) {
2382+
throw new \RuntimeException('Unable to determine stream position: ' . (\error_get_last()['message'] ?? ''));
23632383
}
23642384

23652385
return $result;
23662386
}
23672387

23682388
public function eof(): bool
23692389
{
2370-
return !$this->stream || \feof($this->stream);
2390+
return !isset($this->stream) || \feof($this->stream);
23712391
}
23722392

23732393
public function isSeekable(): bool
@@ -2377,6 +2397,10 @@ public function isSeekable(): bool
23772397

23782398
public function seek($offset, $whence = \SEEK_SET) /*:void*/
23792399
{
2400+
if (!isset($this->stream)) {
2401+
throw new \RuntimeException('Stream is detached');
2402+
}
2403+
23802404
if (!$this->seekable) {
23812405
throw new \RuntimeException('Stream is not seekable');
23822406
}
@@ -2398,15 +2422,19 @@ public function isWritable(): bool
23982422

23992423
public function write($string): int
24002424
{
2425+
if (!isset($this->stream)) {
2426+
throw new \RuntimeException('Stream is detached');
2427+
}
2428+
24012429
if (!$this->writable) {
24022430
throw new \RuntimeException('Cannot write to a non-writable stream');
24032431
}
24042432

24052433
// We can't know the size after writing anything
24062434
$this->size = null;
24072435

2408-
if (false === $result = \fwrite($this->stream, $string)) {
2409-
throw new \RuntimeException('Unable to write to stream');
2436+
if (false === $result = @\fwrite($this->stream, $string)) {
2437+
throw new \RuntimeException('Unable to write to stream: ' . (\error_get_last()['message'] ?? ''));
24102438
}
24112439

24122440
return $result;
@@ -2419,12 +2447,16 @@ public function isReadable(): bool
24192447

24202448
public function read($length): string
24212449
{
2450+
if (!isset($this->stream)) {
2451+
throw new \RuntimeException('Stream is detached');
2452+
}
2453+
24222454
if (!$this->readable) {
24232455
throw new \RuntimeException('Cannot read from non-readable stream');
24242456
}
24252457

2426-
if (false === $result = \fread($this->stream, $length)) {
2427-
throw new \RuntimeException('Unable to read from stream');
2458+
if (false === $result = @\fread($this->stream, $length)) {
2459+
throw new \RuntimeException('Unable to read from stream: ' . (\error_get_last()['message'] ?? ''));
24282460
}
24292461

24302462
return $result;
@@ -2433,16 +2465,19 @@ public function read($length): string
24332465
public function getContents(): string
24342466
{
24352467
if (!isset($this->stream)) {
2436-
throw new \RuntimeException('Unable to read stream contents');
2468+
throw new \RuntimeException('Stream is detached');
24372469
}
24382470

2439-
if (false === $contents = \stream_get_contents($this->stream)) {
2440-
throw new \RuntimeException('Unable to read stream contents');
2471+
if (false === $contents = @\stream_get_contents($this->stream)) {
2472+
throw new \RuntimeException('Unable to read stream contents: ' . (\error_get_last()['message'] ?? ''));
24412473
}
24422474

24432475
return $contents;
24442476
}
24452477

2478+
/**
2479+
* @return mixed
2480+
*/
24462481
public function getMetadata($key = null)
24472482
{
24482483
if (!isset($this->stream)) {
@@ -2539,7 +2574,7 @@ public function __construct($streamOrFile, $size, $errorStatus, $clientFilename
25392574

25402575
if (\UPLOAD_ERR_OK === $this->error) {
25412576
// Depending on the value set file or stream variable.
2542-
if (\is_string($streamOrFile)) {
2577+
if (\is_string($streamOrFile) && '' !== $streamOrFile) {
25432578
$this->file = $streamOrFile;
25442579
} elseif (\is_resource($streamOrFile)) {
25452580
$this->stream = Stream::create($streamOrFile);
@@ -2573,11 +2608,11 @@ public function getStream(): StreamInterface
25732608
return $this->stream;
25742609
}
25752610

2576-
try {
2577-
return Stream::create(\fopen($this->file, 'r'));
2578-
} catch (\Throwable $e) {
2579-
throw new \RuntimeException(\sprintf('The file "%s" cannot be opened.', $this->file));
2611+
if (false === $resource = @\fopen($this->file, 'r')) {
2612+
throw new \RuntimeException(\sprintf('The file "%s" cannot be opened: %s', $this->file, \error_get_last()['message'] ?? ''));
25802613
}
2614+
2615+
return Stream::create($resource);
25812616
}
25822617

25832618
public function moveTo($targetPath) /*:void*/
@@ -2589,20 +2624,23 @@ public function moveTo($targetPath) /*:void*/
25892624
}
25902625

25912626
if (null !== $this->file) {
2592-
$this->moved = 'cli' === \PHP_SAPI ? \rename($this->file, $targetPath) : \move_uploaded_file($this->file, $targetPath);
2627+
$this->moved = 'cli' === \PHP_SAPI ? @\rename($this->file, $targetPath) : @\move_uploaded_file($this->file, $targetPath);
2628+
2629+
if (false === $this->moved) {
2630+
throw new \RuntimeException(\sprintf('Uploaded file could not be moved to "%s": %s', $targetPath, \error_get_last()['message'] ?? ''));
2631+
}
25932632
} else {
25942633
$stream = $this->getStream();
25952634
if ($stream->isSeekable()) {
25962635
$stream->rewind();
25972636
}
25982637

2599-
try {
2600-
// Copy the contents of a stream into another stream until end-of-file.
2601-
$dest = Stream::create(\fopen($targetPath, 'w'));
2602-
} catch (\Throwable $e) {
2603-
throw new \RuntimeException(\sprintf('The file "%s" cannot be opened.', $targetPath));
2638+
if (false === $resource = @\fopen($targetPath, 'w')) {
2639+
throw new \RuntimeException(\sprintf('The file "%s" cannot be opened: %s', $targetPath, \error_get_last()['message'] ?? ''));
26042640
}
26052641

2642+
$dest = Stream::create($resource);
2643+
26062644
while (!$stream->eof()) {
26072645
if (!$dest->write($stream->read(1048576))) {
26082646
break;
@@ -2611,10 +2649,6 @@ public function moveTo($targetPath) /*:void*/
26112649

26122650
$this->moved = true;
26132651
}
2614-
2615-
if (false === $this->moved) {
2616-
throw new \RuntimeException(\sprintf('Uploaded file could not be moved to "%s"', $targetPath));
2617-
}
26182652
}
26192653

26202654
public function getSize(): int

0 commit comments

Comments
 (0)