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