vendor/guzzlehttp/psr7/src/StreamWrapper.php

218 lines · 4872 bytes

  1<?php
  2
  3declare(strict_types=1);
  4
  5namespace GuzzleHttp\Psr7;
  6
  7use Psr\Http\Message\StreamInterface;
  8
  9/**
 10 * Converts Guzzle streams into PHP stream resources.
 11 *
 12 * @see https://www.php.net/streamwrapper
 13 */
 14final class StreamWrapper
 15{
 16    /** @var resource */
 17    public $context;
 18
 19    /** @var StreamInterface */
 20    private $stream;
 21
 22    /** @var string r, r+, or w */
 23    private $mode;
 24
 25    /**
 26     * Returns a resource representing the stream.
 27     *
 28     * @param StreamInterface $stream The stream to get a resource for
 29     *
 30     * @return resource
 31     *
 32     * @throws \InvalidArgumentException if stream is not readable or writable
 33     */
 34    public static function getResource(StreamInterface $stream)
 35    {
 36        self::register();
 37
 38        if ($stream->isReadable()) {
 39            $mode = $stream->isWritable() ? 'r+' : 'r';
 40        } elseif ($stream->isWritable()) {
 41            $mode = 'w';
 42        } else {
 43            throw new \InvalidArgumentException('The stream must be readable, '
 44                .'writable, or both.');
 45        }
 46
 47        $resource = @fopen('guzzle://stream', $mode, false, self::createStreamContext($stream));
 48
 49        if ($resource === false) {
 50            throw new \RuntimeException('Unable to create stream resource');
 51        }
 52
 53        return $resource;
 54    }
 55
 56    /**
 57     * Creates a stream context that can be used to open a stream as a php stream resource.
 58     *
 59     * @return resource
 60     */
 61    public static function createStreamContext(StreamInterface $stream)
 62    {
 63        return stream_context_create([
 64            'guzzle' => ['stream' => $stream],
 65        ]);
 66    }
 67
 68    /**
 69     * Registers the stream wrapper if needed
 70     */
 71    public static function register(): void
 72    {
 73        if (!in_array('guzzle', stream_get_wrappers())) {
 74            stream_wrapper_register('guzzle', __CLASS__);
 75        }
 76    }
 77
 78    public function stream_open(string $path, string $mode, int $options, ?string &$opened_path = null): bool
 79    {
 80        $options = stream_context_get_options($this->context);
 81
 82        if (!isset($options['guzzle']['stream'])) {
 83            return false;
 84        }
 85
 86        $this->mode = $mode;
 87        $this->stream = $options['guzzle']['stream'];
 88
 89        return true;
 90    }
 91
 92    public function stream_read(int $count): string
 93    {
 94        return $this->stream->read($count);
 95    }
 96
 97    public function stream_write(string $data): int
 98    {
 99        return $this->stream->write($data);
100    }
101
102    public function stream_flush(): bool
103    {
104        return true;
105    }
106
107    public function stream_tell(): int
108    {
109        return $this->stream->tell();
110    }
111
112    public function stream_eof(): bool
113    {
114        return $this->stream->eof();
115    }
116
117    public function stream_seek(int $offset, int $whence): bool
118    {
119        $this->stream->seek($offset, $whence);
120
121        return true;
122    }
123
124    /**
125     * @return resource|false
126     */
127    public function stream_cast(int $cast_as)
128    {
129        $stream = clone $this->stream;
130        $resource = $stream->detach();
131
132        return $resource ?? false;
133    }
134
135    /**
136     * @return array{
137     *   dev: int,
138     *   ino: int,
139     *   mode: int,
140     *   nlink: int,
141     *   uid: int,
142     *   gid: int,
143     *   rdev: int,
144     *   size: int,
145     *   atime: int,
146     *   mtime: int,
147     *   ctime: int,
148     *   blksize: int,
149     *   blocks: int
150     * }|false
151     */
152    public function stream_stat()
153    {
154        if ($this->stream->getSize() === null) {
155            return false;
156        }
157
158        static $modeMap = [
159            'r' => 33060,
160            'rb' => 33060,
161            'r+' => 33206,
162            'w' => 33188,
163            'wb' => 33188,
164        ];
165
166        return [
167            'dev' => 0,
168            'ino' => 0,
169            'mode' => $modeMap[$this->mode],
170            'nlink' => 0,
171            'uid' => 0,
172            'gid' => 0,
173            'rdev' => 0,
174            'size' => $this->stream->getSize() ?: 0,
175            'atime' => 0,
176            'mtime' => 0,
177            'ctime' => 0,
178            'blksize' => 0,
179            'blocks' => 0,
180        ];
181    }
182
183    /**
184     * @return array{
185     *   dev: int,
186     *   ino: int,
187     *   mode: int,
188     *   nlink: int,
189     *   uid: int,
190     *   gid: int,
191     *   rdev: int,
192     *   size: int,
193     *   atime: int,
194     *   mtime: int,
195     *   ctime: int,
196     *   blksize: int,
197     *   blocks: int
198     * }
199     */
200    public function url_stat(string $path, int $flags): array
201    {
202        return [
203            'dev' => 0,
204            'ino' => 0,
205            'mode' => 0,
206            'nlink' => 0,
207            'uid' => 0,
208            'gid' => 0,
209            'rdev' => 0,
210            'size' => 0,
211            'atime' => 0,
212            'mtime' => 0,
213            'ctime' => 0,
214            'blksize' => 0,
215            'blocks' => 0,
216        ];
217    }
218}