LazyOpenStream.php 926 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  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. #[\AllowDynamicProperties]
  10. final class LazyOpenStream implements StreamInterface
  11. {
  12. use StreamDecoratorTrait;
  13. /** @var string */
  14. private $filename;
  15. /** @var string */
  16. private $mode;
  17. /**
  18. * @param string $filename File to lazily open
  19. * @param string $mode fopen mode to use when opening the stream
  20. */
  21. public function __construct(string $filename, string $mode)
  22. {
  23. $this->filename = $filename;
  24. $this->mode = $mode;
  25. }
  26. /**
  27. * Creates the underlying stream lazily when required.
  28. */
  29. protected function createStream(): StreamInterface
  30. {
  31. return Utils::streamFor(Utils::tryFopen($this->filename, $this->mode));
  32. }
  33. }