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