PluginProbe
Media Cloud Sync / 1.3.11
Media Cloud Sync v1.3.11
1.4.1 1.4.0 1.3.12 1.3.11 1.3.10 trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 1.2.0 1.2.10 1.2.11 1.2.12 1.2.13 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 1.2.9 All 35 releases
media-cloud-sync / includes / sdk / s3 / GuzzleHttp / Psr7 / StreamWrapper.php

StreamWrapper.php in Media Cloud Sync 1.3.11, at includes/sdk/s3/GuzzleHttp/Psr7/StreamWrapper.php

146 lines 4.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 declare (strict_types=1);
4 namespace Dudlewebs\WPMCS\s3\GuzzleHttp\Psr7;
5
6 use Dudlewebs\WPMCS\s3\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(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(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 }
146