LazyOpenStream.php 899 B

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