| @@ -1,6 +1,7 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | +declare (strict_types=1); | |
| 3 | 4 | namespace Dudlewebs\WPMCS\s3\GuzzleHttp\Psr7; |
| 4 | 5 | |
| 5 | 6 | use Dudlewebs\WPMCS\s3\Psr\Http\Message\StreamInterface; |
| 6 | 7 | /** |
| @@ -5,11 +6,11 @@ | ||
| 5 | 6 | use Dudlewebs\WPMCS\s3\Psr\Http\Message\StreamInterface; |
| 6 | 7 | /** |
| 7 | 8 | * Converts Guzzle streams into PHP stream resources. |
| 8 | 9 | * |
| 9 | - * @final | |
| 10 | + * @see https://www.php.net/streamwrapper | |
| 10 | 11 | */ |
| 11 | -class StreamWrapper | |
| 12 | +final class StreamWrapper | |
| 12 | 13 | { |
| 13 | 14 | /** @var resource */ |
| 14 | 15 | public $context; |
| 15 | 16 | /** @var StreamInterface */ |
| @@ -34,15 +35,13 @@ | ||
| 34 | 35 | $mode = 'w'; |
| 35 | 36 | } else { |
| 36 | 37 | throw new \InvalidArgumentException('The stream must be readable, ' . 'writable, or both.'); |
| 37 | 38 | } |
| 38 | - return \fopen('guzzle://stream', $mode, null, self::createStreamContext($stream)); | |
| 39 | + return \fopen('guzzle://stream', $mode, \false, self::createStreamContext($stream)); | |
| 39 | 40 | } |
| 40 | 41 | /** |
| 41 | 42 | * Creates a stream context that can be used to open a stream as a php stream resource. |
| 42 | 43 | * |
| 43 | - * @param StreamInterface $stream | |
| 44 | - * | |
| 45 | 44 | * @return resource |
| 46 | 45 | */ |
| 47 | 46 | public static function createStreamContext(StreamInterface $stream) |
| 48 | 47 | { |
| @@ -50,15 +49,15 @@ | ||
| 50 | 49 | } |
| 51 | 50 | /** |
| 52 | 51 | * Registers the stream wrapper if needed |
| 53 | 52 | */ |
| 54 | - public static function register() | |
| 53 | + public static function register() : void | |
| 55 | 54 | { |
| 56 | 55 | if (!\in_array('guzzle', \stream_get_wrappers())) { |
| 57 | 56 | \stream_wrapper_register('guzzle', __CLASS__); |
| 58 | 57 | } |
| 59 | 58 | } |
| 60 | - public function stream_open($path, $mode, $options, &$opened_path) | |
| 59 | + public function stream_open(string $path, string $mode, int $options, ?string &$opened_path = null) : bool | |
| 61 | 60 | { |
| 62 | 61 | $options = \stream_context_get_options($this->context); |
| 63 | 62 | if (!isset($options['guzzle']['stream'])) { |
| 64 | 63 | return \false; |
| @@ -66,40 +65,81 @@ | ||
| 66 | 65 | $this->mode = $mode; |
| 67 | 66 | $this->stream = $options['guzzle']['stream']; |
| 68 | 67 | return \true; |
| 69 | 68 | } |
| 70 | - public function stream_read($count) | |
| 69 | + public function stream_read(int $count) : string | |
| 71 | 70 | { |
| 72 | 71 | return $this->stream->read($count); |
| 73 | 72 | } |
| 74 | - public function stream_write($data) | |
| 73 | + public function stream_write(string $data) : int | |
| 75 | 74 | { |
| 76 | - return (int) $this->stream->write($data); | |
| 75 | + return $this->stream->write($data); | |
| 77 | 76 | } |
| 78 | - public function stream_tell() | |
| 77 | + public function stream_tell() : int | |
| 79 | 78 | { |
| 80 | 79 | return $this->stream->tell(); |
| 81 | 80 | } |
| 82 | - public function stream_eof() | |
| 81 | + public function stream_eof() : bool | |
| 83 | 82 | { |
| 84 | 83 | return $this->stream->eof(); |
| 85 | 84 | } |
| 86 | - public function stream_seek($offset, $whence) | |
| 85 | + public function stream_seek(int $offset, int $whence) : bool | |
| 87 | 86 | { |
| 88 | 87 | $this->stream->seek($offset, $whence); |
| 89 | 88 | return \true; |
| 90 | 89 | } |
| 91 | - public function stream_cast($cast_as) | |
| 90 | + /** | |
| 91 | + * @return resource|false | |
| 92 | + */ | |
| 93 | + public function stream_cast(int $cast_as) | |
| 92 | 94 | { |
| 93 | 95 | $stream = clone $this->stream; |
| 94 | - return $stream->detach(); | |
| 96 | + $resource = $stream->detach(); | |
| 97 | + return $resource ?? \false; | |
| 95 | 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 | + */ | |
| 96 | 116 | public function stream_stat() |
| 97 | 117 | { |
| 118 | + if ($this->stream->getSize() === null) { | |
| 119 | + return \false; | |
| 120 | + } | |
| 98 | 121 | static $modeMap = ['r' => 33060, 'rb' => 33060, 'r+' => 33206, 'w' => 33188, 'wb' => 33188]; |
| 99 | 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]; |
| 100 | 123 | } |
| 101 | - public function url_stat($path, $flags) | |
| 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 | |
| 102 | 142 | { |
| 103 | 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]; |
| 104 | 144 | } |
| 105 | 145 | } |