LazyOpenStream.php 880 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. <?php
  2. namespace GuzzleHttp\Psr7;
  3. use Psr\Http\Message\StreamInterface;
  4. /**
  5. * Lazily reads or writes to a file that is opened only after an IO operation
  6. * take place on the stream.
  7. */
  8. class LazyOpenStream implements StreamInterface
  9. {
  10. use StreamDecoratorTrait;
  11. /** @var string File to open */
  12. private $filename;
  13. /** @var string $mode */
  14. private $mode;
  15. /**
  16. * @param string $filename File to lazily open
  17. * @param string $mode fopen mode to use when opening the stream
  18. */
  19. public function __construct($filename, $mode)
  20. {
  21. $this->filename = $filename;
  22. $this->mode = $mode;
  23. }
  24. /**
  25. * Creates the underlying stream lazily when required.
  26. *
  27. * @return StreamInterface
  28. */
  29. protected function createStream()
  30. {
  31. return stream_for(try_fopen($this->filename, $this->mode));
  32. }
  33. }