Skip to content

Commit 933a5d2

Browse files
committed
fix for #842
1 parent 63362eb commit 933a5d2

File tree

2 files changed

+48
-24
lines changed

2 files changed

+48
-24
lines changed

api.include.php

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

15161516
public function createStreamFromFile(string $filename, string $mode = 'r'): StreamInterface
15171517
{
1518-
$resource = @\fopen($filename, $mode);
1518+
try {
1519+
$resource = @\fopen($filename, $mode);
1520+
} catch (\Throwable $e) {
1521+
throw new \RuntimeException(\sprintf('The file "%s" cannot be opened.', $filename));
1522+
}
1523+
15191524
if (false === $resource) {
1520-
if ('' === $mode || false === \in_array($mode[0], ['r', 'w', 'a', 'x', 'c'])) {
1521-
throw new \InvalidArgumentException('The mode ' . $mode . ' is invalid.');
1525+
if ('' === $mode || false === \in_array($mode[0], ['r', 'w', 'a', 'x', 'c'], true)) {
1526+
throw new \InvalidArgumentException(\sprintf('The mode "%s" is invalid.', $mode));
15221527
}
15231528

1524-
throw new \RuntimeException('The file ' . $filename . ' cannot be opened.');
1529+
throw new \RuntimeException(\sprintf('The file "%s" cannot be opened.', $filename));
15251530
}
15261531

15271532
return Stream::create($resource);
@@ -2377,7 +2382,7 @@ public function seek($offset, $whence = \SEEK_SET) /*:void*/
23772382
}
23782383

23792384
if (-1 === \fseek($this->stream, $offset, $whence)) {
2380-
throw new \RuntimeException('Unable to seek to stream position ' . $offset . ' with whence ' . \var_export($whence, true));
2385+
throw new \RuntimeException('Unable to seek to stream position "' . $offset . '" with whence ' . \var_export($whence, true));
23812386
}
23822387
}
23832388

@@ -2568,9 +2573,11 @@ public function getStream(): StreamInterface
25682573
return $this->stream;
25692574
}
25702575

2571-
$resource = \fopen($this->file, 'r');
2572-
2573-
return Stream::create($resource);
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));
2580+
}
25742581
}
25752582

25762583
public function moveTo($targetPath) /*:void*/
@@ -2589,8 +2596,13 @@ public function moveTo($targetPath) /*:void*/
25892596
$stream->rewind();
25902597
}
25912598

2592-
// Copy the contents of a stream into another stream until end-of-file.
2593-
$dest = Stream::create(\fopen($targetPath, 'w'));
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));
2604+
}
2605+
25942606
while (!$stream->eof()) {
25952607
if (!$dest->write($stream->read(1048576))) {
25962608
break;
@@ -2601,7 +2613,7 @@ public function moveTo($targetPath) /*:void*/
26012613
}
26022614

26032615
if (false === $this->moved) {
2604-
throw new \RuntimeException(\sprintf('Uploaded file could not be moved to %s', $targetPath));
2616+
throw new \RuntimeException(\sprintf('Uploaded file could not be moved to "%s"', $targetPath));
26052617
}
26062618
}
26072619

@@ -2676,7 +2688,7 @@ public function __construct(string $uri = '')
26762688
{
26772689
if ('' !== $uri) {
26782690
if (false === $parts = \parse_url($uri)) {
2679-
throw new \InvalidArgumentException("Unable to parse URI: $uri");
2691+
throw new \InvalidArgumentException(\sprintf('Unable to parse URI: "%s"', $uri));
26802692
}
26812693

26822694
// Apply parse_url parts to a URI.

api.php

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

15161516
public function createStreamFromFile(string $filename, string $mode = 'r'): StreamInterface
15171517
{
1518-
$resource = @\fopen($filename, $mode);
1518+
try {
1519+
$resource = @\fopen($filename, $mode);
1520+
} catch (\Throwable $e) {
1521+
throw new \RuntimeException(\sprintf('The file "%s" cannot be opened.', $filename));
1522+
}
1523+
15191524
if (false === $resource) {
1520-
if ('' === $mode || false === \in_array($mode[0], ['r', 'w', 'a', 'x', 'c'])) {
1521-
throw new \InvalidArgumentException('The mode ' . $mode . ' is invalid.');
1525+
if ('' === $mode || false === \in_array($mode[0], ['r', 'w', 'a', 'x', 'c'], true)) {
1526+
throw new \InvalidArgumentException(\sprintf('The mode "%s" is invalid.', $mode));
15221527
}
15231528

1524-
throw new \RuntimeException('The file ' . $filename . ' cannot be opened.');
1529+
throw new \RuntimeException(\sprintf('The file "%s" cannot be opened.', $filename));
15251530
}
15261531

15271532
return Stream::create($resource);
@@ -2377,7 +2382,7 @@ public function seek($offset, $whence = \SEEK_SET) /*:void*/
23772382
}
23782383

23792384
if (-1 === \fseek($this->stream, $offset, $whence)) {
2380-
throw new \RuntimeException('Unable to seek to stream position ' . $offset . ' with whence ' . \var_export($whence, true));
2385+
throw new \RuntimeException('Unable to seek to stream position "' . $offset . '" with whence ' . \var_export($whence, true));
23812386
}
23822387
}
23832388

@@ -2568,9 +2573,11 @@ public function getStream(): StreamInterface
25682573
return $this->stream;
25692574
}
25702575

2571-
$resource = \fopen($this->file, 'r');
2572-
2573-
return Stream::create($resource);
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));
2580+
}
25742581
}
25752582

25762583
public function moveTo($targetPath) /*:void*/
@@ -2589,8 +2596,13 @@ public function moveTo($targetPath) /*:void*/
25892596
$stream->rewind();
25902597
}
25912598

2592-
// Copy the contents of a stream into another stream until end-of-file.
2593-
$dest = Stream::create(\fopen($targetPath, 'w'));
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));
2604+
}
2605+
25942606
while (!$stream->eof()) {
25952607
if (!$dest->write($stream->read(1048576))) {
25962608
break;
@@ -2601,7 +2613,7 @@ public function moveTo($targetPath) /*:void*/
26012613
}
26022614

26032615
if (false === $this->moved) {
2604-
throw new \RuntimeException(\sprintf('Uploaded file could not be moved to %s', $targetPath));
2616+
throw new \RuntimeException(\sprintf('Uploaded file could not be moved to "%s"', $targetPath));
26052617
}
26062618
}
26072619

@@ -2676,7 +2688,7 @@ public function __construct(string $uri = '')
26762688
{
26772689
if ('' !== $uri) {
26782690
if (false === $parts = \parse_url($uri)) {
2679-
throw new \InvalidArgumentException("Unable to parse URI: $uri");
2691+
throw new \InvalidArgumentException(\sprintf('Unable to parse URI: "%s"', $uri));
26802692
}
26812693

26822694
// Apply parse_url parts to a URI.

0 commit comments

Comments
 (0)