File indexing completed on 2024-12-29 05:27:23

0001 <?php
0002 namespace GuzzleHttp\Psr7;
0003 
0004 use Psr\Http\Message\StreamInterface;
0005 
0006 /**
0007  * Lazily reads or writes to a file that is opened only after an IO operation
0008  * take place on the stream.
0009  */
0010 class LazyOpenStream implements StreamInterface
0011 {
0012     use StreamDecoratorTrait;
0013 
0014     /** @var string File to open */
0015     private $filename;
0016 
0017     /** @var string $mode */
0018     private $mode;
0019 
0020     /**
0021      * @param string $filename File to lazily open
0022      * @param string $mode     fopen mode to use when opening the stream
0023      */
0024     public function __construct($filename, $mode)
0025     {
0026         $this->filename = $filename;
0027         $this->mode = $mode;
0028     }
0029 
0030     /**
0031      * Creates the underlying stream lazily when required.
0032      *
0033      * @return StreamInterface
0034      */
0035     protected function createStream()
0036     {
0037         return stream_for(try_fopen($this->filename, $this->mode));
0038     }
0039 }