@@ -1515,18 +1515,16 @@ public function createStream(string $content = ''): StreamInterface
1515
1515
1516
1516
public function createStreamFromFile (string $ filename , string $ mode = 'r ' ): StreamInterface
1517
1517
{
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 ' );
1522
1520
}
1523
1521
1524
- if (false === $ resource ) {
1522
+ if (false === $ resource = @ \fopen ( $ filename , $ mode ) ) {
1525
1523
if ('' === $ mode || false === \in_array ($ mode [0 ], ['r ' , 'w ' , 'a ' , 'x ' , 'c ' ], true )) {
1526
1524
throw new \InvalidArgumentException (\sprintf ('The mode "%s" is invalid. ' , $ mode ));
1527
1525
}
1528
1526
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 ' ] ?? '' ));
1530
1528
}
1531
1529
1532
1530
return Stream::create ($ resource );
@@ -2087,6 +2085,9 @@ public function getUploadedFiles(): array
2087
2085
return $ this ->uploadedFiles ;
2088
2086
}
2089
2087
2088
+ /**
2089
+ * @return static
2090
+ */
2090
2091
public function withUploadedFiles (array $ uploadedFiles )
2091
2092
{
2092
2093
$ new = clone $ this ;
@@ -2100,6 +2101,9 @@ public function getCookieParams(): array
2100
2101
return $ this ->cookieParams ;
2101
2102
}
2102
2103
2104
+ /**
2105
+ * @return static
2106
+ */
2103
2107
public function withCookieParams (array $ cookies )
2104
2108
{
2105
2109
$ new = clone $ this ;
@@ -2113,6 +2117,9 @@ public function getQueryParams(): array
2113
2117
return $ this ->queryParams ;
2114
2118
}
2115
2119
2120
+ /**
2121
+ * @return static
2122
+ */
2116
2123
public function withQueryParams (array $ query )
2117
2124
{
2118
2125
$ new = clone $ this ;
@@ -2121,11 +2128,17 @@ public function withQueryParams(array $query)
2121
2128
return $ new ;
2122
2129
}
2123
2130
2131
+ /**
2132
+ * @return array|object|null
2133
+ */
2124
2134
public function getParsedBody ()
2125
2135
{
2126
2136
return $ this ->parsedBody ;
2127
2137
}
2128
2138
2139
+ /**
2140
+ * @return static
2141
+ */
2129
2142
public function withParsedBody ($ data )
2130
2143
{
2131
2144
if (!\is_array ($ data ) && !\is_object ($ data ) && null !== $ data ) {
@@ -2143,6 +2156,9 @@ public function getAttributes(): array
2143
2156
return $ this ->attributes ;
2144
2157
}
2145
2158
2159
+ /**
2160
+ * @return mixed
2161
+ */
2146
2162
public function getAttribute ($ attribute , $ default = null )
2147
2163
{
2148
2164
if (false === \array_key_exists ($ attribute , $ this ->attributes )) {
@@ -2358,16 +2374,20 @@ public function getSize() /*:?int*/
2358
2374
2359
2375
public function tell (): int
2360
2376
{
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 ' ] ?? '' ));
2363
2383
}
2364
2384
2365
2385
return $ result ;
2366
2386
}
2367
2387
2368
2388
public function eof (): bool
2369
2389
{
2370
- return !$ this ->stream || \feof ($ this ->stream );
2390
+ return !isset ( $ this ->stream ) || \feof ($ this ->stream );
2371
2391
}
2372
2392
2373
2393
public function isSeekable (): bool
@@ -2377,6 +2397,10 @@ public function isSeekable(): bool
2377
2397
2378
2398
public function seek ($ offset , $ whence = \SEEK_SET ) /*:void*/
2379
2399
{
2400
+ if (!isset ($ this ->stream )) {
2401
+ throw new \RuntimeException ('Stream is detached ' );
2402
+ }
2403
+
2380
2404
if (!$ this ->seekable ) {
2381
2405
throw new \RuntimeException ('Stream is not seekable ' );
2382
2406
}
@@ -2398,15 +2422,19 @@ public function isWritable(): bool
2398
2422
2399
2423
public function write ($ string ): int
2400
2424
{
2425
+ if (!isset ($ this ->stream )) {
2426
+ throw new \RuntimeException ('Stream is detached ' );
2427
+ }
2428
+
2401
2429
if (!$ this ->writable ) {
2402
2430
throw new \RuntimeException ('Cannot write to a non-writable stream ' );
2403
2431
}
2404
2432
2405
2433
// We can't know the size after writing anything
2406
2434
$ this ->size = null ;
2407
2435
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 ' ] ?? '' ) );
2410
2438
}
2411
2439
2412
2440
return $ result ;
@@ -2419,12 +2447,16 @@ public function isReadable(): bool
2419
2447
2420
2448
public function read ($ length ): string
2421
2449
{
2450
+ if (!isset ($ this ->stream )) {
2451
+ throw new \RuntimeException ('Stream is detached ' );
2452
+ }
2453
+
2422
2454
if (!$ this ->readable ) {
2423
2455
throw new \RuntimeException ('Cannot read from non-readable stream ' );
2424
2456
}
2425
2457
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 ' ] ?? '' ) );
2428
2460
}
2429
2461
2430
2462
return $ result ;
@@ -2433,16 +2465,19 @@ public function read($length): string
2433
2465
public function getContents (): string
2434
2466
{
2435
2467
if (!isset ($ this ->stream )) {
2436
- throw new \RuntimeException ('Unable to read stream contents ' );
2468
+ throw new \RuntimeException ('Stream is detached ' );
2437
2469
}
2438
2470
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 ' ] ?? '' ) );
2441
2473
}
2442
2474
2443
2475
return $ contents ;
2444
2476
}
2445
2477
2478
+ /**
2479
+ * @return mixed
2480
+ */
2446
2481
public function getMetadata ($ key = null )
2447
2482
{
2448
2483
if (!isset ($ this ->stream )) {
@@ -2539,7 +2574,7 @@ public function __construct($streamOrFile, $size, $errorStatus, $clientFilename
2539
2574
2540
2575
if (\UPLOAD_ERR_OK === $ this ->error ) {
2541
2576
// Depending on the value set file or stream variable.
2542
- if (\is_string ($ streamOrFile )) {
2577
+ if (\is_string ($ streamOrFile ) && '' !== $ streamOrFile ) {
2543
2578
$ this ->file = $ streamOrFile ;
2544
2579
} elseif (\is_resource ($ streamOrFile )) {
2545
2580
$ this ->stream = Stream::create ($ streamOrFile );
@@ -2573,11 +2608,11 @@ public function getStream(): StreamInterface
2573
2608
return $ this ->stream ;
2574
2609
}
2575
2610
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 ' ] ?? '' ));
2580
2613
}
2614
+
2615
+ return Stream::create ($ resource );
2581
2616
}
2582
2617
2583
2618
public function moveTo ($ targetPath ) /*:void*/
@@ -2589,20 +2624,23 @@ public function moveTo($targetPath) /*:void*/
2589
2624
}
2590
2625
2591
2626
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
+ }
2593
2632
} else {
2594
2633
$ stream = $ this ->getStream ();
2595
2634
if ($ stream ->isSeekable ()) {
2596
2635
$ stream ->rewind ();
2597
2636
}
2598
2637
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 ' ] ?? '' ));
2604
2640
}
2605
2641
2642
+ $ dest = Stream::create ($ resource );
2643
+
2606
2644
while (!$ stream ->eof ()) {
2607
2645
if (!$ dest ->write ($ stream ->read (1048576 ))) {
2608
2646
break ;
@@ -2611,10 +2649,6 @@ public function moveTo($targetPath) /*:void*/
2611
2649
2612
2650
$ this ->moved = true ;
2613
2651
}
2614
-
2615
- if (false === $ this ->moved ) {
2616
- throw new \RuntimeException (\sprintf ('Uploaded file could not be moved to "%s" ' , $ targetPath ));
2617
- }
2618
2652
}
2619
2653
2620
2654
public function getSize (): int
0 commit comments