← All changes
|
vendor_prefixed/guzzlehttp/psr7/src/StreamWrapper.php
+0
-145
27.7
→
trunk
View file →
| @@ -1,145 +1,0 @@ | ||
| 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 | - return \fopen('guzzle://stream', $mode, \false, self::createStreamContext($stream)); | |
| 40 | - } | |
| 41 | - /** | |
| 42 | - * Creates a stream context that can be used to open a stream as a php stream resource. | |
| 43 | - * | |
| 44 | - * @return resource | |
| 45 | - */ | |
| 46 | - public static function createStreamContext(\YoastSEO_Vendor\Psr\Http\Message\StreamInterface $stream) | |
| 47 | - { | |
| 48 | - return \stream_context_create(['guzzle' => ['stream' => $stream]]); | |
| 49 | - } | |
| 50 | - /** | |
| 51 | - * Registers the stream wrapper if needed | |
| 52 | - */ | |
| 53 | - public static function register() : void | |
| 54 | - { | |
| 55 | - if (!\in_array('guzzle', \stream_get_wrappers())) { | |
| 56 | - \stream_wrapper_register('guzzle', __CLASS__); | |
| 57 | - } | |
| 58 | - } | |
| 59 | - public function stream_open(string $path, string $mode, int $options, ?string &$opened_path = null) : bool | |
| 60 | - { | |
| 61 | - $options = \stream_context_get_options($this->context); | |
| 62 | - if (!isset($options['guzzle']['stream'])) { | |
| 63 | - return \false; | |
| 64 | - } | |
| 65 | - $this->mode = $mode; | |
| 66 | - $this->stream = $options['guzzle']['stream']; | |
| 67 | - return \true; | |
| 68 | - } | |
| 69 | - public function stream_read(int $count) : string | |
| 70 | - { | |
| 71 | - return $this->stream->read($count); | |
| 72 | - } | |
| 73 | - public function stream_write(string $data) : int | |
| 74 | - { | |
| 75 | - return $this->stream->write($data); | |
| 76 | - } | |
| 77 | - public function stream_tell() : int | |
| 78 | - { | |
| 79 | - return $this->stream->tell(); | |
| 80 | - } | |
| 81 | - public function stream_eof() : bool | |
| 82 | - { | |
| 83 | - return $this->stream->eof(); | |
| 84 | - } | |
| 85 | - public function stream_seek(int $offset, int $whence) : bool | |
| 86 | - { | |
| 87 | - $this->stream->seek($offset, $whence); | |
| 88 | - return \true; | |
| 89 | - } | |
| 90 | - /** | |
| 91 | - * @return resource|false | |
| 92 | - */ | |
| 93 | - public function stream_cast(int $cast_as) | |
| 94 | - { | |
| 95 | - $stream = clone $this->stream; | |
| 96 | - $resource = $stream->detach(); | |
| 97 | - return $resource ?? \false; | |
| 98 | - } | |
| 99 | - /** | |
| 100 | - * @return array{ | |
| 101 | - * dev: int, | |
| 102 | - * ino: int, | |
| 103 | - * mode: int, | |
| 104 | - * nlink: int, | |
| 105 | - * uid: int, | |
| 106 | - * gid: int, | |
| 107 | - * rdev: int, | |
| 108 | - * size: int, | |
| 109 | - * atime: int, | |
| 110 | - * mtime: int, | |
| 111 | - * ctime: int, | |
| 112 | - * blksize: int, | |
| 113 | - * blocks: int | |
| 114 | - * }|false | |
| 115 | - */ | |
| 116 | - public function stream_stat() | |
| 117 | - { | |
| 118 | - if ($this->stream->getSize() === null) { | |
| 119 | - return \false; | |
| 120 | - } | |
| 121 | - static $modeMap = ['r' => 33060, 'rb' => 33060, 'r+' => 33206, 'w' => 33188, 'wb' => 33188]; | |
| 122 | - 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]; | |
| 123 | - } | |
| 124 | - /** | |
| 125 | - * @return array{ | |
| 126 | - * dev: int, | |
| 127 | - * ino: int, | |
| 128 | - * mode: int, | |
| 129 | - * nlink: int, | |
| 130 | - * uid: int, | |
| 131 | - * gid: int, | |
| 132 | - * rdev: int, | |
| 133 | - * size: int, | |
| 134 | - * atime: int, | |
| 135 | - * mtime: int, | |
| 136 | - * ctime: int, | |
| 137 | - * blksize: int, | |
| 138 | - * blocks: int | |
| 139 | - * } | |
| 140 | - */ | |
| 141 | - public function url_stat(string $path, int $flags) : array | |
| 142 | - { | |
| 143 | - 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]; | |
| 144 | - } | |
| 145 | -} | |