| 1 |
<?php |
| 2 |
|
| 3 |
declare (strict_types=1); |
| 4 |
namespace YoastSEO_Vendor\GuzzleHttp\Psr7; |
| 5 |
|
| 6 |
use YoastSEO_Vendor\Psr\Http\Message\StreamInterface; |
| 7 |
/** |
| 8 |
* Converts Guzzle streams into PHP stream resources. |
| 9 |
* |
| 10 |
* @see https://www.php.net/streamwrapper |
| 11 |
*/ |
| 12 |
final class StreamWrapper |
| 13 |
{ |
| 14 |
/** @var resource */ |
| 15 |
public $context; |
| 16 |
/** @var StreamInterface */ |
| 17 |
private $stream; |
| 18 |
/** @var string r, r+, or w */ |
| 19 |
private $mode; |
| 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 |
* |
| 27 |
* @throws \InvalidArgumentException if stream is not readable or writable |
| 28 |
*/ |
| 29 |
public static function getResource(\YoastSEO_Vendor\Psr\Http\Message\StreamInterface $stream) |
| 30 |
{ |
| 31 |
self::register(); |
| 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, ' . 'writable, or both.'); |
| 38 |
} |
| 39 |
$resource = @\fopen('guzzle://stream', $mode, \false, self::createStreamContext($stream)); |
| 40 |
if ($resource === \false) { |
| 41 |
throw new \RuntimeException('Unable to create stream resource'); |
| 42 |
} |
| 43 |
return $resource; |
| 44 |
} |
| 45 |
/** |
| 46 |
* Creates a stream context that can be used to open a stream as a php stream resource. |
| 47 |
* |
| 48 |
* @return resource |
| 49 |
*/ |
| 50 |
public static function createStreamContext(\YoastSEO_Vendor\Psr\Http\Message\StreamInterface $stream) |
| 51 |
{ |
| 52 |
return \stream_context_create(['guzzle' => ['stream' => $stream]]); |
| 53 |
} |
| 54 |
/** |
| 55 |
* Registers the stream wrapper if needed |
| 56 |
*/ |
| 57 |
public static function register() : void |
| 58 |
{ |
| 59 |
if (!\in_array('guzzle', \stream_get_wrappers())) { |
| 60 |
\stream_wrapper_register('guzzle', __CLASS__); |
| 61 |
} |
| 62 |
} |
| 63 |
public function stream_open(string $path, string $mode, int $options, ?string &$opened_path = null) : bool |
| 64 |
{ |
| 65 |
$options = \stream_context_get_options($this->context); |
| 66 |
if (!isset($options['guzzle']['stream'])) { |
| 67 |
return \false; |
| 68 |
} |
| 69 |
$this->mode = $mode; |
| 70 |
$this->stream = $options['guzzle']['stream']; |
| 71 |
return \true; |
| 72 |
} |
| 73 |
public function stream_read(int $count) : string |
| 74 |
{ |
| 75 |
return $this->stream->read($count); |
| 76 |
} |
| 77 |
public function stream_write(string $data) : int |
| 78 |
{ |
| 79 |
return $this->stream->write($data); |
| 80 |
} |
| 81 |
public function stream_tell() : int |
| 82 |
{ |
| 83 |
return $this->stream->tell(); |
| 84 |
} |
| 85 |
public function stream_eof() : bool |
| 86 |
{ |
| 87 |
return $this->stream->eof(); |
| 88 |
} |
| 89 |
public function stream_seek(int $offset, int $whence) : bool |
| 90 |
{ |
| 91 |
$this->stream->seek($offset, $whence); |
| 92 |
return \true; |
| 93 |
} |
| 94 |
/** |
| 95 |
* @return resource|false |
| 96 |
*/ |
| 97 |
public function stream_cast(int $cast_as) |
| 98 |
{ |
| 99 |
$stream = clone $this->stream; |
| 100 |
$resource = $stream->detach(); |
| 101 |
return $resource ?? \false; |
| 102 |
} |
| 103 |
/** |
| 104 |
* @return array{ |
| 105 |
* dev: int, |
| 106 |
* ino: int, |
| 107 |
* mode: int, |
| 108 |
* nlink: int, |
| 109 |
* uid: int, |
| 110 |
* gid: int, |
| 111 |
* rdev: int, |
| 112 |
* size: int, |
| 113 |
* atime: int, |
| 114 |
* mtime: int, |
| 115 |
* ctime: int, |
| 116 |
* blksize: int, |
| 117 |
* blocks: int |
| 118 |
* }|false |
| 119 |
*/ |
| 120 |
public function stream_stat() |
| 121 |
{ |
| 122 |
if ($this->stream->getSize() === null) { |
| 123 |
return \false; |
| 124 |
} |
| 125 |
static $modeMap = ['r' => 33060, 'rb' => 33060, 'r+' => 33206, 'w' => 33188, 'wb' => 33188]; |
| 126 |
return ['dev' => 0, 'ino' => 0, 'mode' => $modeMap[$this->mode], 'nlink' => 0, 'uid' => 0, 'gid' => 0, 'rdev' => 0, 'size' => $this->stream->getSize() ?: 0, 'atime' => 0, 'mtime' => 0, 'ctime' => 0, 'blksize' => 0, 'blocks' => 0]; |
| 127 |
} |
| 128 |
/** |
| 129 |
* @return array{ |
| 130 |
* dev: int, |
| 131 |
* ino: int, |
| 132 |
* mode: int, |
| 133 |
* nlink: int, |
| 134 |
* uid: int, |
| 135 |
* gid: int, |
| 136 |
* rdev: int, |
| 137 |
* size: int, |
| 138 |
* atime: int, |
| 139 |
* mtime: int, |
| 140 |
* ctime: int, |
| 141 |
* blksize: int, |
| 142 |
* blocks: int |
| 143 |
* } |
| 144 |
*/ |
| 145 |
public function url_stat(string $path, int $flags) : array |
| 146 |
{ |
| 147 |
return ['dev' => 0, 'ino' => 0, 'mode' => 0, 'nlink' => 0, 'uid' => 0, 'gid' => 0, 'rdev' => 0, 'size' => 0, 'atime' => 0, 'mtime' => 0, 'ctime' => 0, 'blksize' => 0, 'blocks' => 0]; |
| 148 |
} |
| 149 |
} |
| 150 |
|