| [01a620f] | 1 | <?php
|
|---|
| 2 | namespace GuzzleHttp\Psr7;
|
|---|
| 3 |
|
|---|
| 4 | use Psr\Http\Message\StreamInterface;
|
|---|
| 5 |
|
|---|
| 6 | /**
|
|---|
| 7 | * Converts Guzzle streams into PHP stream resources.
|
|---|
| 8 | */
|
|---|
| 9 | class StreamWrapper
|
|---|
| 10 | {
|
|---|
| 11 | /** @var resource */
|
|---|
| 12 | public $context;
|
|---|
| 13 |
|
|---|
| 14 | /** @var StreamInterface */
|
|---|
| 15 | private $stream;
|
|---|
| 16 |
|
|---|
| 17 | /** @var string r, r+, or w */
|
|---|
| 18 | private $mode;
|
|---|
| 19 |
|
|---|
| 20 | /**
|
|---|
| 21 | * Returns a resource representing the stream.
|
|---|
| 22 | *
|
|---|
| 23 | * @param StreamInterface $stream The stream to get a resource for
|
|---|
| 24 | *
|
|---|
| 25 | * @return resource
|
|---|
| 26 | * @throws \InvalidArgumentException if stream is not readable or writable
|
|---|
| 27 | */
|
|---|
| 28 | public static function getResource(StreamInterface $stream)
|
|---|
| 29 | {
|
|---|
| 30 | self::register();
|
|---|
| 31 |
|
|---|
| 32 | if ($stream->isReadable()) {
|
|---|
| 33 | $mode = $stream->isWritable() ? 'r+' : 'r';
|
|---|
| 34 | } elseif ($stream->isWritable()) {
|
|---|
| 35 | $mode = 'w';
|
|---|
| 36 | } else {
|
|---|
| 37 | throw new \InvalidArgumentException('The stream must be readable, '
|
|---|
| 38 | . 'writable, or both.');
|
|---|
| 39 | }
|
|---|
| 40 |
|
|---|
| 41 | return fopen('guzzle://stream', $mode, null, stream_context_create([
|
|---|
| 42 | 'guzzle' => ['stream' => $stream]
|
|---|
| 43 | ]));
|
|---|
| 44 | }
|
|---|
| 45 |
|
|---|
| 46 | /**
|
|---|
| 47 | * Registers the stream wrapper if needed
|
|---|
| 48 | */
|
|---|
| 49 | public static function register()
|
|---|
| 50 | {
|
|---|
| 51 | if (!in_array('guzzle', stream_get_wrappers())) {
|
|---|
| 52 | stream_wrapper_register('guzzle', __CLASS__);
|
|---|
| 53 | }
|
|---|
| 54 | }
|
|---|
| 55 |
|
|---|
| 56 | public function stream_open($path, $mode, $options, &$opened_path)
|
|---|
| 57 | {
|
|---|
| 58 | $options = stream_context_get_options($this->context);
|
|---|
| 59 |
|
|---|
| 60 | if (!isset($options['guzzle']['stream'])) {
|
|---|
| 61 | return false;
|
|---|
| 62 | }
|
|---|
| 63 |
|
|---|
| 64 | $this->mode = $mode;
|
|---|
| 65 | $this->stream = $options['guzzle']['stream'];
|
|---|
| 66 |
|
|---|
| 67 | return true;
|
|---|
| 68 | }
|
|---|
| 69 |
|
|---|
| 70 | public function stream_read($count)
|
|---|
| 71 | {
|
|---|
| 72 | return $this->stream->read($count);
|
|---|
| 73 | }
|
|---|
| 74 |
|
|---|
| 75 | public function stream_write($data)
|
|---|
| 76 | {
|
|---|
| 77 | return (int) $this->stream->write($data);
|
|---|
| 78 | }
|
|---|
| 79 |
|
|---|
| 80 | public function stream_tell()
|
|---|
| 81 | {
|
|---|
| 82 | return $this->stream->tell();
|
|---|
| 83 | }
|
|---|
| 84 |
|
|---|
| 85 | public function stream_eof()
|
|---|
| 86 | {
|
|---|
| 87 | return $this->stream->eof();
|
|---|
| 88 | }
|
|---|
| 89 |
|
|---|
| 90 | public function stream_seek($offset, $whence)
|
|---|
| 91 | {
|
|---|
| 92 | $this->stream->seek($offset, $whence);
|
|---|
| 93 |
|
|---|
| 94 | return true;
|
|---|
| 95 | }
|
|---|
| 96 |
|
|---|
| 97 | public function stream_stat()
|
|---|
| 98 | {
|
|---|
| 99 | static $modeMap = [
|
|---|
| 100 | 'r' => 33060,
|
|---|
| 101 | 'r+' => 33206,
|
|---|
| 102 | 'w' => 33188
|
|---|
| 103 | ];
|
|---|
| 104 |
|
|---|
| 105 | return [
|
|---|
| 106 | 'dev' => 0,
|
|---|
| 107 | 'ino' => 0,
|
|---|
| 108 | 'mode' => $modeMap[$this->mode],
|
|---|
| 109 | 'nlink' => 0,
|
|---|
| 110 | 'uid' => 0,
|
|---|
| 111 | 'gid' => 0,
|
|---|
| 112 | 'rdev' => 0,
|
|---|
| 113 | 'size' => $this->stream->getSize() ?: 0,
|
|---|
| 114 | 'atime' => 0,
|
|---|
| 115 | 'mtime' => 0,
|
|---|
| 116 | 'ctime' => 0,
|
|---|
| 117 | 'blksize' => 0,
|
|---|
| 118 | 'blocks' => 0
|
|---|
| 119 | ];
|
|---|
| 120 | }
|
|---|
| 121 | }
|
|---|