SlimStreamFactory.php 764 B

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