DiactorosStreamFactory.php
6 months ago
GuzzleStreamFactory.php
6 months ago
SlimStreamFactory.php
6 months ago
SlimStreamFactory.php
38 lines
| 1 | <?php |
| 2 | |
| 3 | namespace AmeliaHttp\Message\StreamFactory; |
| 4 | |
| 5 | use AmeliaHttp\Message\StreamFactory; |
| 6 | use AmeliaVendor\Psr\Http\Message\StreamInterface; |
| 7 | use Slim\Http\Stream; |
| 8 | |
| 9 | /** |
| 10 | * Creates Slim 3 streams. |
| 11 | * |
| 12 | * @author Mika Tuupola <tuupola@appelsiini.net> |
| 13 | */ |
| 14 | final class SlimStreamFactory implements StreamFactory |
| 15 | { |
| 16 | /** |
| 17 | * {@inheritdoc} |
| 18 | */ |
| 19 | public function createStream($body = null) |
| 20 | { |
| 21 | if ($body instanceof StreamInterface) { |
| 22 | return $body; |
| 23 | } |
| 24 | |
| 25 | if (is_resource($body)) { |
| 26 | return new Stream($body); |
| 27 | } |
| 28 | |
| 29 | $resource = fopen('php://memory', 'r+'); |
| 30 | $stream = new Stream($resource); |
| 31 | if (null !== $body && '' !== $body) { |
| 32 | $stream->write((string) $body); |
| 33 | } |
| 34 | |
| 35 | return $stream; |
| 36 | } |
| 37 | } |
| 38 |