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