Skip to content

Commit a186435

Browse files
committed
fix for #944
1 parent c004a3c commit a186435

File tree

3 files changed

+67
-102
lines changed

3 files changed

+67
-102
lines changed

api.include.php

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

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

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

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

15301532
return Stream::create($resource);
@@ -2141,9 +2143,6 @@ public function getAttributes(): array
21412143
return $this->attributes;
21422144
}
21432145

2144-
/**
2145-
* @return mixed
2146-
*/
21472146
public function getAttribute($attribute, $default = null)
21482147
{
21492148
if (false === \array_key_exists($attribute, $this->attributes)) {
@@ -2359,20 +2358,16 @@ public function getSize() /*:?int*/
23592358

23602359
public function tell(): int
23612360
{
2362-
if (!isset($this->stream)) {
2363-
throw new \RuntimeException('Stream is detached');
2364-
}
2365-
2366-
if (false === $result = @\ftell($this->stream)) {
2367-
throw new \RuntimeException('Unable to determine stream position: ' . (\error_get_last()['message'] ?? ''));
2361+
if (false === $result = \ftell($this->stream)) {
2362+
throw new \RuntimeException('Unable to determine stream position');
23682363
}
23692364

23702365
return $result;
23712366
}
23722367

23732368
public function eof(): bool
23742369
{
2375-
return !isset($this->stream) || \feof($this->stream);
2370+
return !$this->stream || \feof($this->stream);
23762371
}
23772372

23782373
public function isSeekable(): bool
@@ -2382,10 +2377,6 @@ public function isSeekable(): bool
23822377

23832378
public function seek($offset, $whence = \SEEK_SET) /*:void*/
23842379
{
2385-
if (!isset($this->stream)) {
2386-
throw new \RuntimeException('Stream is detached');
2387-
}
2388-
23892380
if (!$this->seekable) {
23902381
throw new \RuntimeException('Stream is not seekable');
23912382
}
@@ -2407,19 +2398,15 @@ public function isWritable(): bool
24072398

24082399
public function write($string): int
24092400
{
2410-
if (!isset($this->stream)) {
2411-
throw new \RuntimeException('Stream is detached');
2412-
}
2413-
24142401
if (!$this->writable) {
24152402
throw new \RuntimeException('Cannot write to a non-writable stream');
24162403
}
24172404

24182405
// We can't know the size after writing anything
24192406
$this->size = null;
24202407

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

24252412
return $result;
@@ -2432,16 +2419,12 @@ public function isReadable(): bool
24322419

24332420
public function read($length): string
24342421
{
2435-
if (!isset($this->stream)) {
2436-
throw new \RuntimeException('Stream is detached');
2437-
}
2438-
24392422
if (!$this->readable) {
24402423
throw new \RuntimeException('Cannot read from non-readable stream');
24412424
}
24422425

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

24472430
return $result;
@@ -2450,19 +2433,16 @@ public function read($length): string
24502433
public function getContents(): string
24512434
{
24522435
if (!isset($this->stream)) {
2453-
throw new \RuntimeException('Stream is detached');
2436+
throw new \RuntimeException('Unable to read stream contents');
24542437
}
24552438

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

24602443
return $contents;
24612444
}
24622445

2463-
/**
2464-
* @return mixed
2465-
*/
24662446
public function getMetadata($key = null)
24672447
{
24682448
if (!isset($this->stream)) {
@@ -2559,7 +2539,7 @@ public function __construct($streamOrFile, $size, $errorStatus, $clientFilename
25592539

25602540
if (\UPLOAD_ERR_OK === $this->error) {
25612541
// Depending on the value set file or stream variable.
2562-
if (\is_string($streamOrFile) && '' !== $streamOrFile) {
2542+
if (\is_string($streamOrFile)) {
25632543
$this->file = $streamOrFile;
25642544
} elseif (\is_resource($streamOrFile)) {
25652545
$this->stream = Stream::create($streamOrFile);
@@ -2593,11 +2573,11 @@ public function getStream(): StreamInterface
25932573
return $this->stream;
25942574
}
25952575

2596-
if (false === $resource = @\fopen($this->file, 'r')) {
2597-
throw new \RuntimeException(\sprintf('The file "%s" cannot be opened: %s', $this->file, \error_get_last()['message'] ?? ''));
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));
25982580
}
2599-
2600-
return Stream::create($resource);
26012581
}
26022582

26032583
public function moveTo($targetPath) /*:void*/
@@ -2609,23 +2589,20 @@ public function moveTo($targetPath) /*:void*/
26092589
}
26102590

26112591
if (null !== $this->file) {
2612-
$this->moved = 'cli' === \PHP_SAPI ? @\rename($this->file, $targetPath) : @\move_uploaded_file($this->file, $targetPath);
2613-
2614-
if (false === $this->moved) {
2615-
throw new \RuntimeException(\sprintf('Uploaded file could not be moved to "%s": %s', $targetPath, \error_get_last()['message'] ?? ''));
2616-
}
2592+
$this->moved = 'cli' === \PHP_SAPI ? \rename($this->file, $targetPath) : \move_uploaded_file($this->file, $targetPath);
26172593
} else {
26182594
$stream = $this->getStream();
26192595
if ($stream->isSeekable()) {
26202596
$stream->rewind();
26212597
}
26222598

2623-
if (false === $resource = @\fopen($targetPath, 'w')) {
2624-
throw new \RuntimeException(\sprintf('The file "%s" cannot be opened: %s', $targetPath, \error_get_last()['message'] ?? ''));
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));
26252604
}
26262605

2627-
$dest = Stream::create($resource);
2628-
26292606
while (!$stream->eof()) {
26302607
if (!$dest->write($stream->read(1048576))) {
26312608
break;
@@ -2634,6 +2611,10 @@ public function moveTo($targetPath) /*:void*/
26342611

26352612
$this->moved = true;
26362613
}
2614+
2615+
if (false === $this->moved) {
2616+
throw new \RuntimeException(\sprintf('Uploaded file could not be moved to "%s"', $targetPath));
2617+
}
26372618
}
26382619

26392620
public function getSize(): int
@@ -9598,6 +9579,7 @@ class OpenApiBuilder
95989579
private $openapi;
95999580
private $records;
96009581
private $columns;
9582+
private $status;
96019583
private $builders;
96029584

96039585
public function __construct(ReflectionService $reflection, array $base, array $controllers, array $builders)
@@ -11822,7 +11804,7 @@ class Config
1182211804
'command' => '',
1182311805
'tables' => 'all',
1182411806
'mapping' => '',
11825-
'middlewares' => 'cors,errors',
11807+
'middlewares' => 'cors',
1182611808
'controllers' => 'records,geojson,openapi,status',
1182711809
'customControllers' => '',
1182811810
'customOpenApiBuilders' => '',

0 commit comments

Comments
 (0)