Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
11 / 11 |
|
100.00% |
3 / 3 |
CRAP | |
100.00% |
1 / 1 |
| StreamFactory | |
100.00% |
11 / 11 |
|
100.00% |
3 / 3 |
4 | |
100.00% |
1 / 1 |
| createStream | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| createStreamFromFile | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
2 | |||
| createStreamFromResource | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | declare(strict_types=1); |
| 3 | /** |
| 4 | * Pop PHP Framework (https://www.popphp.org/) |
| 5 | * |
| 6 | * @link https://github.com/popphp/popphp-framework |
| 7 | * @author Nick Sagona, III <dev@noladev.com> |
| 8 | * @copyright Copyright (c) 2009-2027 NOLA Interactive, LLC. |
| 9 | * @license https://www.popphp.org/license New BSD License |
| 10 | */ |
| 11 | |
| 12 | /** |
| 13 | * @namespace |
| 14 | */ |
| 15 | namespace Pop\Http\Factory; |
| 16 | |
| 17 | use Psr\Http\Message\StreamFactoryInterface; |
| 18 | use Psr\Http\Message\StreamInterface; |
| 19 | use Pop\Http\Body; |
| 20 | |
| 21 | /** |
| 22 | * PSR-17 stream factory class |
| 23 | * |
| 24 | * @category Pop |
| 25 | * @package Pop\Http |
| 26 | * @author Nick Sagona, III <dev@noladev.com> |
| 27 | * @copyright Copyright (c) 2009-2027 NOLA Interactive, LLC. |
| 28 | * @license https://www.popphp.org/license New BSD License |
| 29 | * @version 6.0.0 |
| 30 | */ |
| 31 | class StreamFactory implements StreamFactoryInterface |
| 32 | { |
| 33 | |
| 34 | /** |
| 35 | * Create a new stream from a string |
| 36 | * |
| 37 | * @param string $content |
| 38 | * @return StreamInterface |
| 39 | */ |
| 40 | public function createStream(string $content = ''): StreamInterface |
| 41 | { |
| 42 | return new Body($content); |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * Create a new stream from a file path |
| 47 | * |
| 48 | * Opens the file directly with the given mode (bypassing Body::setContentFromFile(), |
| 49 | * which always opens read-only) so the stream honors the mode the caller requested, |
| 50 | * and throws \RuntimeException on failure per the PSR-17 contract. |
| 51 | * |
| 52 | * @param string $filename |
| 53 | * @param string $mode |
| 54 | * @throws \RuntimeException |
| 55 | * @return StreamInterface |
| 56 | */ |
| 57 | public function createStreamFromFile(string $filename, string $mode = 'r'): StreamInterface |
| 58 | { |
| 59 | $resource = @fopen($filename, $mode); |
| 60 | if ($resource === false) { |
| 61 | throw new \RuntimeException("Error: Unable to open the file '" . $filename . "' with mode '" . $mode . "'."); |
| 62 | } |
| 63 | |
| 64 | $body = new Body(); |
| 65 | $body->setContentFromStream($resource); |
| 66 | $body->setAsFile(true); |
| 67 | |
| 68 | return $body; |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Create a new stream from an existing resource |
| 73 | * |
| 74 | * @param mixed $resource |
| 75 | * @return StreamInterface |
| 76 | */ |
| 77 | public function createStreamFromResource($resource): StreamInterface |
| 78 | { |
| 79 | $body = new Body(); |
| 80 | $body->setContentFromStream($resource); |
| 81 | return $body; |
| 82 | } |
| 83 | |
| 84 | } |