| @@ 14-39 (lines=26) @@ | ||
| 11 | * |
|
| 12 | * @author Михаил Красильников <[email protected]> |
|
| 13 | */ |
|
| 14 | final class DiactorosStreamFactory implements StreamFactory |
|
| 15 | { |
|
| 16 | /** |
|
| 17 | * {@inheritdoc} |
|
| 18 | */ |
|
| 19 | public function createStream($body = null) |
|
| 20 | { |
|
| 21 | if (!$body instanceof StreamInterface) { |
|
| 22 | if (is_resource($body)) { |
|
| 23 | $body = new Stream($body); |
|
| 24 | } else { |
|
| 25 | $stream = new Stream('php://memory', 'rw'); |
|
| 26 | ||
| 27 | if (null !== $body) { |
|
| 28 | $stream->write((string) $body); |
|
| 29 | } |
|
| 30 | ||
| 31 | $body = $stream; |
|
| 32 | } |
|
| 33 | } |
|
| 34 | ||
| 35 | $body->rewind(); |
|
| 36 | ||
| 37 | return $body; |
|
| 38 | } |
|
| 39 | } |
|
| 40 | ||
| @@ 14-40 (lines=27) @@ | ||
| 11 | * |
|
| 12 | * @author Mika Tuupola <[email protected]> |
|
| 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 | $stream = new Stream($body); |
|
| 27 | } else { |
|
| 28 | $resource = fopen('php://memory', 'r+'); |
|
| 29 | $stream = new Stream($resource); |
|
| 30 | ||
| 31 | if (null !== $body) { |
|
| 32 | $stream->write((string) $body); |
|
| 33 | } |
|
| 34 | } |
|
| 35 | ||
| 36 | $stream->rewind(); |
|
| 37 | ||
| 38 | return $stream; |
|
| 39 | } |
|
| 40 | } |
|
| 41 | ||