@@ -19,19 +19,19 @@ |
||
19 | 19 | */ |
20 | 20 | final class InflateStream implements StreamInterface |
21 | 21 | { |
22 | - use StreamDecoratorTrait; |
|
22 | + use StreamDecoratorTrait; |
|
23 | 23 | |
24 | - /** @var StreamInterface */ |
|
25 | - private $stream; |
|
24 | + /** @var StreamInterface */ |
|
25 | + private $stream; |
|
26 | 26 | |
27 | - public function __construct(StreamInterface $stream) |
|
28 | - { |
|
29 | - $resource = StreamWrapper::getResource($stream); |
|
30 | - // Specify window=15+32, so zlib will use header detection to both gzip (with header) and zlib data |
|
31 | - // See https://www.zlib.net/manual.html#Advanced definition of inflateInit2 |
|
32 | - // "Add 32 to windowBits to enable zlib and gzip decoding with automatic header detection" |
|
33 | - // Default window size is 15. |
|
34 | - stream_filter_append($resource, 'zlib.inflate', STREAM_FILTER_READ, ['window' => 15 + 32]); |
|
35 | - $this->stream = $stream->isSeekable() ? new Stream($resource) : new NoSeekStream(new Stream($resource)); |
|
36 | - } |
|
27 | + public function __construct(StreamInterface $stream) |
|
28 | + { |
|
29 | + $resource = StreamWrapper::getResource($stream); |
|
30 | + // Specify window=15+32, so zlib will use header detection to both gzip (with header) and zlib data |
|
31 | + // See https://www.zlib.net/manual.html#Advanced definition of inflateInit2 |
|
32 | + // "Add 32 to windowBits to enable zlib and gzip decoding with automatic header detection" |
|
33 | + // Default window size is 15. |
|
34 | + stream_filter_append($resource, 'zlib.inflate', STREAM_FILTER_READ, ['window' => 15 + 32]); |
|
35 | + $this->stream = $stream->isSeekable() ? new Stream($resource) : new NoSeekStream(new Stream($resource)); |
|
36 | + } |
|
37 | 37 | } |
@@ -17,8 +17,7 @@ |
||
17 | 17 | * @see https://datatracker.ietf.org/doc/html/rfc1952 |
18 | 18 | * @see https://www.php.net/manual/en/filters.compression.php |
19 | 19 | */ |
20 | -final class InflateStream implements StreamInterface |
|
21 | -{ |
|
20 | +final class InflateStream implements StreamInterface { |
|
22 | 21 | use StreamDecoratorTrait; |
23 | 22 | |
24 | 23 | /** @var StreamInterface */ |
@@ -11,273 +11,273 @@ |
||
11 | 11 | */ |
12 | 12 | class Stream implements StreamInterface |
13 | 13 | { |
14 | - /** |
|
15 | - * @see https://www.php.net/manual/en/function.fopen.php |
|
16 | - * @see https://www.php.net/manual/en/function.gzopen.php |
|
17 | - */ |
|
18 | - private const READABLE_MODES = '/r|a\+|ab\+|w\+|wb\+|x\+|xb\+|c\+|cb\+/'; |
|
19 | - private const WRITABLE_MODES = '/a|w|r\+|rb\+|rw|x|c/'; |
|
20 | - |
|
21 | - /** @var resource */ |
|
22 | - private $stream; |
|
23 | - /** @var int|null */ |
|
24 | - private $size; |
|
25 | - /** @var bool */ |
|
26 | - private $seekable; |
|
27 | - /** @var bool */ |
|
28 | - private $readable; |
|
29 | - /** @var bool */ |
|
30 | - private $writable; |
|
31 | - /** @var string|null */ |
|
32 | - private $uri; |
|
33 | - /** @var mixed[] */ |
|
34 | - private $customMetadata; |
|
35 | - |
|
36 | - /** |
|
37 | - * This constructor accepts an associative array of options. |
|
38 | - * |
|
39 | - * - size: (int) If a read stream would otherwise have an indeterminate |
|
40 | - * size, but the size is known due to foreknowledge, then you can |
|
41 | - * provide that size, in bytes. |
|
42 | - * - metadata: (array) Any additional metadata to return when the metadata |
|
43 | - * of the stream is accessed. |
|
44 | - * |
|
45 | - * @param resource $stream Stream resource to wrap. |
|
46 | - * @param array{size?: int, metadata?: array} $options Associative array of options. |
|
47 | - * |
|
48 | - * @throws \InvalidArgumentException if the stream is not a stream resource |
|
49 | - */ |
|
50 | - public function __construct($stream, array $options = []) |
|
51 | - { |
|
52 | - if (!is_resource($stream)) { |
|
53 | - throw new \InvalidArgumentException('Stream must be a resource'); |
|
54 | - } |
|
55 | - |
|
56 | - if (isset($options['size'])) { |
|
57 | - $this->size = $options['size']; |
|
58 | - } |
|
59 | - |
|
60 | - $this->customMetadata = $options['metadata'] ?? []; |
|
61 | - $this->stream = $stream; |
|
62 | - $meta = stream_get_meta_data($this->stream); |
|
63 | - $this->seekable = $meta['seekable']; |
|
64 | - $this->readable = (bool) preg_match(self::READABLE_MODES, $meta['mode']); |
|
65 | - $this->writable = (bool) preg_match(self::WRITABLE_MODES, $meta['mode']); |
|
66 | - $this->uri = $this->getMetadata('uri'); |
|
67 | - } |
|
68 | - |
|
69 | - /** |
|
70 | - * Closes the stream when the destructed |
|
71 | - */ |
|
72 | - public function __destruct() |
|
73 | - { |
|
74 | - $this->close(); |
|
75 | - } |
|
76 | - |
|
77 | - public function __toString(): string |
|
78 | - { |
|
79 | - try { |
|
80 | - if ($this->isSeekable()) { |
|
81 | - $this->seek(0); |
|
82 | - } |
|
83 | - |
|
84 | - return $this->getContents(); |
|
85 | - } catch (\Throwable $e) { |
|
86 | - if (\PHP_VERSION_ID >= 70400) { |
|
87 | - throw $e; |
|
88 | - } |
|
89 | - trigger_error(sprintf('%s::__toString exception: %s', self::class, (string) $e), E_USER_ERROR); |
|
90 | - |
|
91 | - return ''; |
|
92 | - } |
|
93 | - } |
|
94 | - |
|
95 | - public function getContents(): string |
|
96 | - { |
|
97 | - if (!isset($this->stream)) { |
|
98 | - throw new \RuntimeException('Stream is detached'); |
|
99 | - } |
|
100 | - |
|
101 | - if (!$this->readable) { |
|
102 | - throw new \RuntimeException('Cannot read from non-readable stream'); |
|
103 | - } |
|
104 | - |
|
105 | - return Utils::tryGetContents($this->stream); |
|
106 | - } |
|
107 | - |
|
108 | - public function close(): void |
|
109 | - { |
|
110 | - if (isset($this->stream)) { |
|
111 | - if (is_resource($this->stream)) { |
|
112 | - fclose($this->stream); |
|
113 | - } |
|
114 | - $this->detach(); |
|
115 | - } |
|
116 | - } |
|
117 | - |
|
118 | - public function detach() |
|
119 | - { |
|
120 | - if (!isset($this->stream)) { |
|
121 | - return null; |
|
122 | - } |
|
123 | - |
|
124 | - $result = $this->stream; |
|
125 | - unset($this->stream); |
|
126 | - $this->size = $this->uri = null; |
|
127 | - $this->readable = $this->writable = $this->seekable = false; |
|
128 | - |
|
129 | - return $result; |
|
130 | - } |
|
131 | - |
|
132 | - public function getSize(): ?int |
|
133 | - { |
|
134 | - if ($this->size !== null) { |
|
135 | - return $this->size; |
|
136 | - } |
|
137 | - |
|
138 | - if (!isset($this->stream)) { |
|
139 | - return null; |
|
140 | - } |
|
141 | - |
|
142 | - // Clear the stat cache if the stream has a URI |
|
143 | - if ($this->uri) { |
|
144 | - clearstatcache(true, $this->uri); |
|
145 | - } |
|
146 | - |
|
147 | - $stats = fstat($this->stream); |
|
148 | - if (is_array($stats) && isset($stats['size'])) { |
|
149 | - $this->size = $stats['size']; |
|
150 | - |
|
151 | - return $this->size; |
|
152 | - } |
|
153 | - |
|
154 | - return null; |
|
155 | - } |
|
156 | - |
|
157 | - public function isReadable(): bool |
|
158 | - { |
|
159 | - return $this->readable; |
|
160 | - } |
|
161 | - |
|
162 | - public function isWritable(): bool |
|
163 | - { |
|
164 | - return $this->writable; |
|
165 | - } |
|
166 | - |
|
167 | - public function isSeekable(): bool |
|
168 | - { |
|
169 | - return $this->seekable; |
|
170 | - } |
|
171 | - |
|
172 | - public function eof(): bool |
|
173 | - { |
|
174 | - if (!isset($this->stream)) { |
|
175 | - throw new \RuntimeException('Stream is detached'); |
|
176 | - } |
|
177 | - |
|
178 | - return feof($this->stream); |
|
179 | - } |
|
180 | - |
|
181 | - public function tell(): int |
|
182 | - { |
|
183 | - if (!isset($this->stream)) { |
|
184 | - throw new \RuntimeException('Stream is detached'); |
|
185 | - } |
|
186 | - |
|
187 | - $result = ftell($this->stream); |
|
188 | - |
|
189 | - if ($result === false) { |
|
190 | - throw new \RuntimeException('Unable to determine stream position'); |
|
191 | - } |
|
192 | - |
|
193 | - return $result; |
|
194 | - } |
|
195 | - |
|
196 | - public function rewind(): void |
|
197 | - { |
|
198 | - $this->seek(0); |
|
199 | - } |
|
200 | - |
|
201 | - public function seek($offset, $whence = SEEK_SET): void |
|
202 | - { |
|
203 | - $whence = (int) $whence; |
|
204 | - |
|
205 | - if (!isset($this->stream)) { |
|
206 | - throw new \RuntimeException('Stream is detached'); |
|
207 | - } |
|
208 | - if (!$this->seekable) { |
|
209 | - throw new \RuntimeException('Stream is not seekable'); |
|
210 | - } |
|
211 | - if (fseek($this->stream, $offset, $whence) === -1) { |
|
212 | - throw new \RuntimeException('Unable to seek to stream position ' |
|
213 | - .$offset.' with whence '.var_export($whence, true)); |
|
214 | - } |
|
215 | - } |
|
216 | - |
|
217 | - public function read($length): string |
|
218 | - { |
|
219 | - if (!isset($this->stream)) { |
|
220 | - throw new \RuntimeException('Stream is detached'); |
|
221 | - } |
|
222 | - if (!$this->readable) { |
|
223 | - throw new \RuntimeException('Cannot read from non-readable stream'); |
|
224 | - } |
|
225 | - if ($length < 0) { |
|
226 | - throw new \RuntimeException('Length parameter cannot be negative'); |
|
227 | - } |
|
228 | - |
|
229 | - if (0 === $length) { |
|
230 | - return ''; |
|
231 | - } |
|
232 | - |
|
233 | - try { |
|
234 | - $string = fread($this->stream, $length); |
|
235 | - } catch (\Exception $e) { |
|
236 | - throw new \RuntimeException('Unable to read from stream', 0, $e); |
|
237 | - } |
|
238 | - |
|
239 | - if (false === $string) { |
|
240 | - throw new \RuntimeException('Unable to read from stream'); |
|
241 | - } |
|
242 | - |
|
243 | - return $string; |
|
244 | - } |
|
245 | - |
|
246 | - public function write($string): int |
|
247 | - { |
|
248 | - if (!isset($this->stream)) { |
|
249 | - throw new \RuntimeException('Stream is detached'); |
|
250 | - } |
|
251 | - if (!$this->writable) { |
|
252 | - throw new \RuntimeException('Cannot write to a non-writable stream'); |
|
253 | - } |
|
254 | - |
|
255 | - // We can't know the size after writing anything |
|
256 | - $this->size = null; |
|
257 | - $result = fwrite($this->stream, $string); |
|
258 | - |
|
259 | - if ($result === false) { |
|
260 | - throw new \RuntimeException('Unable to write to stream'); |
|
261 | - } |
|
262 | - |
|
263 | - return $result; |
|
264 | - } |
|
265 | - |
|
266 | - /** |
|
267 | - * @return mixed |
|
268 | - */ |
|
269 | - public function getMetadata($key = null) |
|
270 | - { |
|
271 | - if (!isset($this->stream)) { |
|
272 | - return $key ? null : []; |
|
273 | - } elseif (!$key) { |
|
274 | - return $this->customMetadata + stream_get_meta_data($this->stream); |
|
275 | - } elseif (isset($this->customMetadata[$key])) { |
|
276 | - return $this->customMetadata[$key]; |
|
277 | - } |
|
278 | - |
|
279 | - $meta = stream_get_meta_data($this->stream); |
|
280 | - |
|
281 | - return $meta[$key] ?? null; |
|
282 | - } |
|
14 | + /** |
|
15 | + * @see https://www.php.net/manual/en/function.fopen.php |
|
16 | + * @see https://www.php.net/manual/en/function.gzopen.php |
|
17 | + */ |
|
18 | + private const READABLE_MODES = '/r|a\+|ab\+|w\+|wb\+|x\+|xb\+|c\+|cb\+/'; |
|
19 | + private const WRITABLE_MODES = '/a|w|r\+|rb\+|rw|x|c/'; |
|
20 | + |
|
21 | + /** @var resource */ |
|
22 | + private $stream; |
|
23 | + /** @var int|null */ |
|
24 | + private $size; |
|
25 | + /** @var bool */ |
|
26 | + private $seekable; |
|
27 | + /** @var bool */ |
|
28 | + private $readable; |
|
29 | + /** @var bool */ |
|
30 | + private $writable; |
|
31 | + /** @var string|null */ |
|
32 | + private $uri; |
|
33 | + /** @var mixed[] */ |
|
34 | + private $customMetadata; |
|
35 | + |
|
36 | + /** |
|
37 | + * This constructor accepts an associative array of options. |
|
38 | + * |
|
39 | + * - size: (int) If a read stream would otherwise have an indeterminate |
|
40 | + * size, but the size is known due to foreknowledge, then you can |
|
41 | + * provide that size, in bytes. |
|
42 | + * - metadata: (array) Any additional metadata to return when the metadata |
|
43 | + * of the stream is accessed. |
|
44 | + * |
|
45 | + * @param resource $stream Stream resource to wrap. |
|
46 | + * @param array{size?: int, metadata?: array} $options Associative array of options. |
|
47 | + * |
|
48 | + * @throws \InvalidArgumentException if the stream is not a stream resource |
|
49 | + */ |
|
50 | + public function __construct($stream, array $options = []) |
|
51 | + { |
|
52 | + if (!is_resource($stream)) { |
|
53 | + throw new \InvalidArgumentException('Stream must be a resource'); |
|
54 | + } |
|
55 | + |
|
56 | + if (isset($options['size'])) { |
|
57 | + $this->size = $options['size']; |
|
58 | + } |
|
59 | + |
|
60 | + $this->customMetadata = $options['metadata'] ?? []; |
|
61 | + $this->stream = $stream; |
|
62 | + $meta = stream_get_meta_data($this->stream); |
|
63 | + $this->seekable = $meta['seekable']; |
|
64 | + $this->readable = (bool) preg_match(self::READABLE_MODES, $meta['mode']); |
|
65 | + $this->writable = (bool) preg_match(self::WRITABLE_MODES, $meta['mode']); |
|
66 | + $this->uri = $this->getMetadata('uri'); |
|
67 | + } |
|
68 | + |
|
69 | + /** |
|
70 | + * Closes the stream when the destructed |
|
71 | + */ |
|
72 | + public function __destruct() |
|
73 | + { |
|
74 | + $this->close(); |
|
75 | + } |
|
76 | + |
|
77 | + public function __toString(): string |
|
78 | + { |
|
79 | + try { |
|
80 | + if ($this->isSeekable()) { |
|
81 | + $this->seek(0); |
|
82 | + } |
|
83 | + |
|
84 | + return $this->getContents(); |
|
85 | + } catch (\Throwable $e) { |
|
86 | + if (\PHP_VERSION_ID >= 70400) { |
|
87 | + throw $e; |
|
88 | + } |
|
89 | + trigger_error(sprintf('%s::__toString exception: %s', self::class, (string) $e), E_USER_ERROR); |
|
90 | + |
|
91 | + return ''; |
|
92 | + } |
|
93 | + } |
|
94 | + |
|
95 | + public function getContents(): string |
|
96 | + { |
|
97 | + if (!isset($this->stream)) { |
|
98 | + throw new \RuntimeException('Stream is detached'); |
|
99 | + } |
|
100 | + |
|
101 | + if (!$this->readable) { |
|
102 | + throw new \RuntimeException('Cannot read from non-readable stream'); |
|
103 | + } |
|
104 | + |
|
105 | + return Utils::tryGetContents($this->stream); |
|
106 | + } |
|
107 | + |
|
108 | + public function close(): void |
|
109 | + { |
|
110 | + if (isset($this->stream)) { |
|
111 | + if (is_resource($this->stream)) { |
|
112 | + fclose($this->stream); |
|
113 | + } |
|
114 | + $this->detach(); |
|
115 | + } |
|
116 | + } |
|
117 | + |
|
118 | + public function detach() |
|
119 | + { |
|
120 | + if (!isset($this->stream)) { |
|
121 | + return null; |
|
122 | + } |
|
123 | + |
|
124 | + $result = $this->stream; |
|
125 | + unset($this->stream); |
|
126 | + $this->size = $this->uri = null; |
|
127 | + $this->readable = $this->writable = $this->seekable = false; |
|
128 | + |
|
129 | + return $result; |
|
130 | + } |
|
131 | + |
|
132 | + public function getSize(): ?int |
|
133 | + { |
|
134 | + if ($this->size !== null) { |
|
135 | + return $this->size; |
|
136 | + } |
|
137 | + |
|
138 | + if (!isset($this->stream)) { |
|
139 | + return null; |
|
140 | + } |
|
141 | + |
|
142 | + // Clear the stat cache if the stream has a URI |
|
143 | + if ($this->uri) { |
|
144 | + clearstatcache(true, $this->uri); |
|
145 | + } |
|
146 | + |
|
147 | + $stats = fstat($this->stream); |
|
148 | + if (is_array($stats) && isset($stats['size'])) { |
|
149 | + $this->size = $stats['size']; |
|
150 | + |
|
151 | + return $this->size; |
|
152 | + } |
|
153 | + |
|
154 | + return null; |
|
155 | + } |
|
156 | + |
|
157 | + public function isReadable(): bool |
|
158 | + { |
|
159 | + return $this->readable; |
|
160 | + } |
|
161 | + |
|
162 | + public function isWritable(): bool |
|
163 | + { |
|
164 | + return $this->writable; |
|
165 | + } |
|
166 | + |
|
167 | + public function isSeekable(): bool |
|
168 | + { |
|
169 | + return $this->seekable; |
|
170 | + } |
|
171 | + |
|
172 | + public function eof(): bool |
|
173 | + { |
|
174 | + if (!isset($this->stream)) { |
|
175 | + throw new \RuntimeException('Stream is detached'); |
|
176 | + } |
|
177 | + |
|
178 | + return feof($this->stream); |
|
179 | + } |
|
180 | + |
|
181 | + public function tell(): int |
|
182 | + { |
|
183 | + if (!isset($this->stream)) { |
|
184 | + throw new \RuntimeException('Stream is detached'); |
|
185 | + } |
|
186 | + |
|
187 | + $result = ftell($this->stream); |
|
188 | + |
|
189 | + if ($result === false) { |
|
190 | + throw new \RuntimeException('Unable to determine stream position'); |
|
191 | + } |
|
192 | + |
|
193 | + return $result; |
|
194 | + } |
|
195 | + |
|
196 | + public function rewind(): void |
|
197 | + { |
|
198 | + $this->seek(0); |
|
199 | + } |
|
200 | + |
|
201 | + public function seek($offset, $whence = SEEK_SET): void |
|
202 | + { |
|
203 | + $whence = (int) $whence; |
|
204 | + |
|
205 | + if (!isset($this->stream)) { |
|
206 | + throw new \RuntimeException('Stream is detached'); |
|
207 | + } |
|
208 | + if (!$this->seekable) { |
|
209 | + throw new \RuntimeException('Stream is not seekable'); |
|
210 | + } |
|
211 | + if (fseek($this->stream, $offset, $whence) === -1) { |
|
212 | + throw new \RuntimeException('Unable to seek to stream position ' |
|
213 | + .$offset.' with whence '.var_export($whence, true)); |
|
214 | + } |
|
215 | + } |
|
216 | + |
|
217 | + public function read($length): string |
|
218 | + { |
|
219 | + if (!isset($this->stream)) { |
|
220 | + throw new \RuntimeException('Stream is detached'); |
|
221 | + } |
|
222 | + if (!$this->readable) { |
|
223 | + throw new \RuntimeException('Cannot read from non-readable stream'); |
|
224 | + } |
|
225 | + if ($length < 0) { |
|
226 | + throw new \RuntimeException('Length parameter cannot be negative'); |
|
227 | + } |
|
228 | + |
|
229 | + if (0 === $length) { |
|
230 | + return ''; |
|
231 | + } |
|
232 | + |
|
233 | + try { |
|
234 | + $string = fread($this->stream, $length); |
|
235 | + } catch (\Exception $e) { |
|
236 | + throw new \RuntimeException('Unable to read from stream', 0, $e); |
|
237 | + } |
|
238 | + |
|
239 | + if (false === $string) { |
|
240 | + throw new \RuntimeException('Unable to read from stream'); |
|
241 | + } |
|
242 | + |
|
243 | + return $string; |
|
244 | + } |
|
245 | + |
|
246 | + public function write($string): int |
|
247 | + { |
|
248 | + if (!isset($this->stream)) { |
|
249 | + throw new \RuntimeException('Stream is detached'); |
|
250 | + } |
|
251 | + if (!$this->writable) { |
|
252 | + throw new \RuntimeException('Cannot write to a non-writable stream'); |
|
253 | + } |
|
254 | + |
|
255 | + // We can't know the size after writing anything |
|
256 | + $this->size = null; |
|
257 | + $result = fwrite($this->stream, $string); |
|
258 | + |
|
259 | + if ($result === false) { |
|
260 | + throw new \RuntimeException('Unable to write to stream'); |
|
261 | + } |
|
262 | + |
|
263 | + return $result; |
|
264 | + } |
|
265 | + |
|
266 | + /** |
|
267 | + * @return mixed |
|
268 | + */ |
|
269 | + public function getMetadata($key = null) |
|
270 | + { |
|
271 | + if (!isset($this->stream)) { |
|
272 | + return $key ? null : []; |
|
273 | + } elseif (!$key) { |
|
274 | + return $this->customMetadata + stream_get_meta_data($this->stream); |
|
275 | + } elseif (isset($this->customMetadata[$key])) { |
|
276 | + return $this->customMetadata[$key]; |
|
277 | + } |
|
278 | + |
|
279 | + $meta = stream_get_meta_data($this->stream); |
|
280 | + |
|
281 | + return $meta[$key] ?? null; |
|
282 | + } |
|
283 | 283 | } |
@@ -61,8 +61,8 @@ discard block |
||
61 | 61 | $this->stream = $stream; |
62 | 62 | $meta = stream_get_meta_data($this->stream); |
63 | 63 | $this->seekable = $meta['seekable']; |
64 | - $this->readable = (bool) preg_match(self::READABLE_MODES, $meta['mode']); |
|
65 | - $this->writable = (bool) preg_match(self::WRITABLE_MODES, $meta['mode']); |
|
64 | + $this->readable = (bool)preg_match(self::READABLE_MODES, $meta['mode']); |
|
65 | + $this->writable = (bool)preg_match(self::WRITABLE_MODES, $meta['mode']); |
|
66 | 66 | $this->uri = $this->getMetadata('uri'); |
67 | 67 | } |
68 | 68 | |
@@ -86,7 +86,7 @@ discard block |
||
86 | 86 | if (\PHP_VERSION_ID >= 70400) { |
87 | 87 | throw $e; |
88 | 88 | } |
89 | - trigger_error(sprintf('%s::__toString exception: %s', self::class, (string) $e), E_USER_ERROR); |
|
89 | + trigger_error(sprintf('%s::__toString exception: %s', self::class, (string)$e), E_USER_ERROR); |
|
90 | 90 | |
91 | 91 | return ''; |
92 | 92 | } |
@@ -200,7 +200,7 @@ discard block |
||
200 | 200 | |
201 | 201 | public function seek($offset, $whence = SEEK_SET): void |
202 | 202 | { |
203 | - $whence = (int) $whence; |
|
203 | + $whence = (int)$whence; |
|
204 | 204 | |
205 | 205 | if (!isset($this->stream)) { |
206 | 206 | throw new \RuntimeException('Stream is detached'); |
@@ -9,8 +9,7 @@ |
||
9 | 9 | /** |
10 | 10 | * PHP stream implementation. |
11 | 11 | */ |
12 | -class Stream implements StreamInterface |
|
13 | -{ |
|
12 | +class Stream implements StreamInterface { |
|
14 | 13 | /** |
15 | 14 | * @see https://www.php.net/manual/en/function.fopen.php |
16 | 15 | * @see https://www.php.net/manual/en/function.gzopen.php |
@@ -10,237 +10,237 @@ |
||
10 | 10 | |
11 | 11 | final class Message |
12 | 12 | { |
13 | - /** |
|
14 | - * Returns the string representation of an HTTP message. |
|
15 | - * |
|
16 | - * @param MessageInterface $message Message to convert to a string. |
|
17 | - */ |
|
18 | - public static function toString(MessageInterface $message): string |
|
19 | - { |
|
20 | - if ($message instanceof RequestInterface) { |
|
21 | - $msg = trim($message->getMethod().' ' |
|
22 | - .$message->getRequestTarget()) |
|
23 | - .' HTTP/'.$message->getProtocolVersion(); |
|
24 | - if (!$message->hasHeader('host')) { |
|
25 | - $msg .= "\r\nHost: ".$message->getUri()->getHost(); |
|
26 | - } |
|
27 | - } elseif ($message instanceof ResponseInterface) { |
|
28 | - $msg = 'HTTP/'.$message->getProtocolVersion().' ' |
|
29 | - .$message->getStatusCode().' ' |
|
30 | - .$message->getReasonPhrase(); |
|
31 | - } else { |
|
32 | - throw new \InvalidArgumentException('Unknown message type'); |
|
33 | - } |
|
34 | - |
|
35 | - foreach ($message->getHeaders() as $name => $values) { |
|
36 | - if (is_string($name) && strtolower($name) === 'set-cookie') { |
|
37 | - foreach ($values as $value) { |
|
38 | - $msg .= "\r\n{$name}: ".$value; |
|
39 | - } |
|
40 | - } else { |
|
41 | - $msg .= "\r\n{$name}: ".implode(', ', $values); |
|
42 | - } |
|
43 | - } |
|
44 | - |
|
45 | - return "{$msg}\r\n\r\n".$message->getBody(); |
|
46 | - } |
|
47 | - |
|
48 | - /** |
|
49 | - * Get a short summary of the message body. |
|
50 | - * |
|
51 | - * Will return `null` if the response is not printable. |
|
52 | - * |
|
53 | - * @param MessageInterface $message The message to get the body summary |
|
54 | - * @param int $truncateAt The maximum allowed size of the summary |
|
55 | - */ |
|
56 | - public static function bodySummary(MessageInterface $message, int $truncateAt = 120): ?string |
|
57 | - { |
|
58 | - $body = $message->getBody(); |
|
59 | - |
|
60 | - if (!$body->isSeekable() || !$body->isReadable()) { |
|
61 | - return null; |
|
62 | - } |
|
63 | - |
|
64 | - $size = $body->getSize(); |
|
65 | - |
|
66 | - if ($size === 0) { |
|
67 | - return null; |
|
68 | - } |
|
69 | - |
|
70 | - $body->rewind(); |
|
71 | - $summary = $body->read($truncateAt); |
|
72 | - $body->rewind(); |
|
73 | - |
|
74 | - if ($size > $truncateAt) { |
|
75 | - $summary .= ' (truncated...)'; |
|
76 | - } |
|
77 | - |
|
78 | - // Matches any printable character, including unicode characters: |
|
79 | - // letters, marks, numbers, punctuation, spacing, and separators. |
|
80 | - if (preg_match('/[^\pL\pM\pN\pP\pS\pZ\n\r\t]/u', $summary) !== 0) { |
|
81 | - return null; |
|
82 | - } |
|
83 | - |
|
84 | - return $summary; |
|
85 | - } |
|
86 | - |
|
87 | - /** |
|
88 | - * Attempts to rewind a message body and throws an exception on failure. |
|
89 | - * |
|
90 | - * The body of the message will only be rewound if a call to `tell()` |
|
91 | - * returns a value other than `0`. |
|
92 | - * |
|
93 | - * @param MessageInterface $message Message to rewind |
|
94 | - * |
|
95 | - * @throws \RuntimeException |
|
96 | - */ |
|
97 | - public static function rewindBody(MessageInterface $message): void |
|
98 | - { |
|
99 | - $body = $message->getBody(); |
|
100 | - |
|
101 | - if ($body->tell()) { |
|
102 | - $body->rewind(); |
|
103 | - } |
|
104 | - } |
|
105 | - |
|
106 | - /** |
|
107 | - * Parses an HTTP message into an associative array. |
|
108 | - * |
|
109 | - * The array contains the "start-line" key containing the start line of |
|
110 | - * the message, "headers" key containing an associative array of header |
|
111 | - * array values, and a "body" key containing the body of the message. |
|
112 | - * |
|
113 | - * @param string $message HTTP request or response to parse. |
|
114 | - */ |
|
115 | - public static function parseMessage(string $message): array |
|
116 | - { |
|
117 | - if (!$message) { |
|
118 | - throw new \InvalidArgumentException('Invalid message'); |
|
119 | - } |
|
120 | - |
|
121 | - $message = ltrim($message, "\r\n"); |
|
122 | - |
|
123 | - $messageParts = preg_split("/\r?\n\r?\n/", $message, 2); |
|
124 | - |
|
125 | - if ($messageParts === false || count($messageParts) !== 2) { |
|
126 | - throw new \InvalidArgumentException('Invalid message: Missing header delimiter'); |
|
127 | - } |
|
128 | - |
|
129 | - [$rawHeaders, $body] = $messageParts; |
|
130 | - $rawHeaders .= "\r\n"; // Put back the delimiter we split previously |
|
131 | - $headerParts = preg_split("/\r?\n/", $rawHeaders, 2); |
|
132 | - |
|
133 | - if ($headerParts === false || count($headerParts) !== 2) { |
|
134 | - throw new \InvalidArgumentException('Invalid message: Missing status line'); |
|
135 | - } |
|
136 | - |
|
137 | - [$startLine, $rawHeaders] = $headerParts; |
|
138 | - |
|
139 | - if (preg_match("/(?:^HTTP\/|^[A-Z]+ \S+ HTTP\/)(\d+(?:\.\d+)?)/i", $startLine, $matches) && $matches[1] === '1.0') { |
|
140 | - // Header folding is deprecated for HTTP/1.1, but allowed in HTTP/1.0 |
|
141 | - $rawHeaders = preg_replace(Rfc7230::HEADER_FOLD_REGEX, ' ', $rawHeaders); |
|
142 | - } |
|
143 | - |
|
144 | - /** @var array[] $headerLines */ |
|
145 | - $count = preg_match_all(Rfc7230::HEADER_REGEX, $rawHeaders, $headerLines, PREG_SET_ORDER); |
|
146 | - |
|
147 | - // If these aren't the same, then one line didn't match and there's an invalid header. |
|
148 | - if ($count !== substr_count($rawHeaders, "\n")) { |
|
149 | - // Folding is deprecated, see https://datatracker.ietf.org/doc/html/rfc7230#section-3.2.4 |
|
150 | - if (preg_match(Rfc7230::HEADER_FOLD_REGEX, $rawHeaders)) { |
|
151 | - throw new \InvalidArgumentException('Invalid header syntax: Obsolete line folding'); |
|
152 | - } |
|
153 | - |
|
154 | - throw new \InvalidArgumentException('Invalid header syntax'); |
|
155 | - } |
|
156 | - |
|
157 | - $headers = []; |
|
158 | - |
|
159 | - foreach ($headerLines as $headerLine) { |
|
160 | - $headers[$headerLine[1]][] = $headerLine[2]; |
|
161 | - } |
|
162 | - |
|
163 | - return [ |
|
164 | - 'start-line' => $startLine, |
|
165 | - 'headers' => $headers, |
|
166 | - 'body' => $body, |
|
167 | - ]; |
|
168 | - } |
|
169 | - |
|
170 | - /** |
|
171 | - * Constructs a URI for an HTTP request message. |
|
172 | - * |
|
173 | - * @param string $path Path from the start-line |
|
174 | - * @param array $headers Array of headers (each value an array). |
|
175 | - */ |
|
176 | - public static function parseRequestUri(string $path, array $headers): string |
|
177 | - { |
|
178 | - $hostKey = array_filter(array_keys($headers), function ($k) { |
|
179 | - // Numeric array keys are converted to int by PHP. |
|
180 | - $k = (string) $k; |
|
181 | - |
|
182 | - return strtolower($k) === 'host'; |
|
183 | - }); |
|
184 | - |
|
185 | - // If no host is found, then a full URI cannot be constructed. |
|
186 | - if (!$hostKey) { |
|
187 | - return $path; |
|
188 | - } |
|
189 | - |
|
190 | - $host = $headers[reset($hostKey)][0]; |
|
191 | - $scheme = substr($host, -4) === ':443' ? 'https' : 'http'; |
|
192 | - |
|
193 | - return $scheme.'://'.$host.'/'.ltrim($path, '/'); |
|
194 | - } |
|
195 | - |
|
196 | - /** |
|
197 | - * Parses a request message string into a request object. |
|
198 | - * |
|
199 | - * @param string $message Request message string. |
|
200 | - */ |
|
201 | - public static function parseRequest(string $message): RequestInterface |
|
202 | - { |
|
203 | - $data = self::parseMessage($message); |
|
204 | - $matches = []; |
|
205 | - if (!preg_match('/^[\S]+\s+([a-zA-Z]+:\/\/|\/).*/', $data['start-line'], $matches)) { |
|
206 | - throw new \InvalidArgumentException('Invalid request string'); |
|
207 | - } |
|
208 | - $parts = explode(' ', $data['start-line'], 3); |
|
209 | - $version = isset($parts[2]) ? explode('/', $parts[2])[1] : '1.1'; |
|
210 | - |
|
211 | - $request = new Request( |
|
212 | - $parts[0], |
|
213 | - $matches[1] === '/' ? self::parseRequestUri($parts[1], $data['headers']) : $parts[1], |
|
214 | - $data['headers'], |
|
215 | - $data['body'], |
|
216 | - $version |
|
217 | - ); |
|
218 | - |
|
219 | - return $matches[1] === '/' ? $request : $request->withRequestTarget($parts[1]); |
|
220 | - } |
|
221 | - |
|
222 | - /** |
|
223 | - * Parses a response message string into a response object. |
|
224 | - * |
|
225 | - * @param string $message Response message string. |
|
226 | - */ |
|
227 | - public static function parseResponse(string $message): ResponseInterface |
|
228 | - { |
|
229 | - $data = self::parseMessage($message); |
|
230 | - // According to https://datatracker.ietf.org/doc/html/rfc7230#section-3.1.2 |
|
231 | - // the space between status-code and reason-phrase is required. But |
|
232 | - // browsers accept responses without space and reason as well. |
|
233 | - if (!preg_match('/^HTTP\/.* [0-9]{3}( .*|$)/', $data['start-line'])) { |
|
234 | - throw new \InvalidArgumentException('Invalid response string: '.$data['start-line']); |
|
235 | - } |
|
236 | - $parts = explode(' ', $data['start-line'], 3); |
|
237 | - |
|
238 | - return new Response( |
|
239 | - (int) $parts[1], |
|
240 | - $data['headers'], |
|
241 | - $data['body'], |
|
242 | - explode('/', $parts[0])[1], |
|
243 | - $parts[2] ?? null |
|
244 | - ); |
|
245 | - } |
|
13 | + /** |
|
14 | + * Returns the string representation of an HTTP message. |
|
15 | + * |
|
16 | + * @param MessageInterface $message Message to convert to a string. |
|
17 | + */ |
|
18 | + public static function toString(MessageInterface $message): string |
|
19 | + { |
|
20 | + if ($message instanceof RequestInterface) { |
|
21 | + $msg = trim($message->getMethod().' ' |
|
22 | + .$message->getRequestTarget()) |
|
23 | + .' HTTP/'.$message->getProtocolVersion(); |
|
24 | + if (!$message->hasHeader('host')) { |
|
25 | + $msg .= "\r\nHost: ".$message->getUri()->getHost(); |
|
26 | + } |
|
27 | + } elseif ($message instanceof ResponseInterface) { |
|
28 | + $msg = 'HTTP/'.$message->getProtocolVersion().' ' |
|
29 | + .$message->getStatusCode().' ' |
|
30 | + .$message->getReasonPhrase(); |
|
31 | + } else { |
|
32 | + throw new \InvalidArgumentException('Unknown message type'); |
|
33 | + } |
|
34 | + |
|
35 | + foreach ($message->getHeaders() as $name => $values) { |
|
36 | + if (is_string($name) && strtolower($name) === 'set-cookie') { |
|
37 | + foreach ($values as $value) { |
|
38 | + $msg .= "\r\n{$name}: ".$value; |
|
39 | + } |
|
40 | + } else { |
|
41 | + $msg .= "\r\n{$name}: ".implode(', ', $values); |
|
42 | + } |
|
43 | + } |
|
44 | + |
|
45 | + return "{$msg}\r\n\r\n".$message->getBody(); |
|
46 | + } |
|
47 | + |
|
48 | + /** |
|
49 | + * Get a short summary of the message body. |
|
50 | + * |
|
51 | + * Will return `null` if the response is not printable. |
|
52 | + * |
|
53 | + * @param MessageInterface $message The message to get the body summary |
|
54 | + * @param int $truncateAt The maximum allowed size of the summary |
|
55 | + */ |
|
56 | + public static function bodySummary(MessageInterface $message, int $truncateAt = 120): ?string |
|
57 | + { |
|
58 | + $body = $message->getBody(); |
|
59 | + |
|
60 | + if (!$body->isSeekable() || !$body->isReadable()) { |
|
61 | + return null; |
|
62 | + } |
|
63 | + |
|
64 | + $size = $body->getSize(); |
|
65 | + |
|
66 | + if ($size === 0) { |
|
67 | + return null; |
|
68 | + } |
|
69 | + |
|
70 | + $body->rewind(); |
|
71 | + $summary = $body->read($truncateAt); |
|
72 | + $body->rewind(); |
|
73 | + |
|
74 | + if ($size > $truncateAt) { |
|
75 | + $summary .= ' (truncated...)'; |
|
76 | + } |
|
77 | + |
|
78 | + // Matches any printable character, including unicode characters: |
|
79 | + // letters, marks, numbers, punctuation, spacing, and separators. |
|
80 | + if (preg_match('/[^\pL\pM\pN\pP\pS\pZ\n\r\t]/u', $summary) !== 0) { |
|
81 | + return null; |
|
82 | + } |
|
83 | + |
|
84 | + return $summary; |
|
85 | + } |
|
86 | + |
|
87 | + /** |
|
88 | + * Attempts to rewind a message body and throws an exception on failure. |
|
89 | + * |
|
90 | + * The body of the message will only be rewound if a call to `tell()` |
|
91 | + * returns a value other than `0`. |
|
92 | + * |
|
93 | + * @param MessageInterface $message Message to rewind |
|
94 | + * |
|
95 | + * @throws \RuntimeException |
|
96 | + */ |
|
97 | + public static function rewindBody(MessageInterface $message): void |
|
98 | + { |
|
99 | + $body = $message->getBody(); |
|
100 | + |
|
101 | + if ($body->tell()) { |
|
102 | + $body->rewind(); |
|
103 | + } |
|
104 | + } |
|
105 | + |
|
106 | + /** |
|
107 | + * Parses an HTTP message into an associative array. |
|
108 | + * |
|
109 | + * The array contains the "start-line" key containing the start line of |
|
110 | + * the message, "headers" key containing an associative array of header |
|
111 | + * array values, and a "body" key containing the body of the message. |
|
112 | + * |
|
113 | + * @param string $message HTTP request or response to parse. |
|
114 | + */ |
|
115 | + public static function parseMessage(string $message): array |
|
116 | + { |
|
117 | + if (!$message) { |
|
118 | + throw new \InvalidArgumentException('Invalid message'); |
|
119 | + } |
|
120 | + |
|
121 | + $message = ltrim($message, "\r\n"); |
|
122 | + |
|
123 | + $messageParts = preg_split("/\r?\n\r?\n/", $message, 2); |
|
124 | + |
|
125 | + if ($messageParts === false || count($messageParts) !== 2) { |
|
126 | + throw new \InvalidArgumentException('Invalid message: Missing header delimiter'); |
|
127 | + } |
|
128 | + |
|
129 | + [$rawHeaders, $body] = $messageParts; |
|
130 | + $rawHeaders .= "\r\n"; // Put back the delimiter we split previously |
|
131 | + $headerParts = preg_split("/\r?\n/", $rawHeaders, 2); |
|
132 | + |
|
133 | + if ($headerParts === false || count($headerParts) !== 2) { |
|
134 | + throw new \InvalidArgumentException('Invalid message: Missing status line'); |
|
135 | + } |
|
136 | + |
|
137 | + [$startLine, $rawHeaders] = $headerParts; |
|
138 | + |
|
139 | + if (preg_match("/(?:^HTTP\/|^[A-Z]+ \S+ HTTP\/)(\d+(?:\.\d+)?)/i", $startLine, $matches) && $matches[1] === '1.0') { |
|
140 | + // Header folding is deprecated for HTTP/1.1, but allowed in HTTP/1.0 |
|
141 | + $rawHeaders = preg_replace(Rfc7230::HEADER_FOLD_REGEX, ' ', $rawHeaders); |
|
142 | + } |
|
143 | + |
|
144 | + /** @var array[] $headerLines */ |
|
145 | + $count = preg_match_all(Rfc7230::HEADER_REGEX, $rawHeaders, $headerLines, PREG_SET_ORDER); |
|
146 | + |
|
147 | + // If these aren't the same, then one line didn't match and there's an invalid header. |
|
148 | + if ($count !== substr_count($rawHeaders, "\n")) { |
|
149 | + // Folding is deprecated, see https://datatracker.ietf.org/doc/html/rfc7230#section-3.2.4 |
|
150 | + if (preg_match(Rfc7230::HEADER_FOLD_REGEX, $rawHeaders)) { |
|
151 | + throw new \InvalidArgumentException('Invalid header syntax: Obsolete line folding'); |
|
152 | + } |
|
153 | + |
|
154 | + throw new \InvalidArgumentException('Invalid header syntax'); |
|
155 | + } |
|
156 | + |
|
157 | + $headers = []; |
|
158 | + |
|
159 | + foreach ($headerLines as $headerLine) { |
|
160 | + $headers[$headerLine[1]][] = $headerLine[2]; |
|
161 | + } |
|
162 | + |
|
163 | + return [ |
|
164 | + 'start-line' => $startLine, |
|
165 | + 'headers' => $headers, |
|
166 | + 'body' => $body, |
|
167 | + ]; |
|
168 | + } |
|
169 | + |
|
170 | + /** |
|
171 | + * Constructs a URI for an HTTP request message. |
|
172 | + * |
|
173 | + * @param string $path Path from the start-line |
|
174 | + * @param array $headers Array of headers (each value an array). |
|
175 | + */ |
|
176 | + public static function parseRequestUri(string $path, array $headers): string |
|
177 | + { |
|
178 | + $hostKey = array_filter(array_keys($headers), function ($k) { |
|
179 | + // Numeric array keys are converted to int by PHP. |
|
180 | + $k = (string) $k; |
|
181 | + |
|
182 | + return strtolower($k) === 'host'; |
|
183 | + }); |
|
184 | + |
|
185 | + // If no host is found, then a full URI cannot be constructed. |
|
186 | + if (!$hostKey) { |
|
187 | + return $path; |
|
188 | + } |
|
189 | + |
|
190 | + $host = $headers[reset($hostKey)][0]; |
|
191 | + $scheme = substr($host, -4) === ':443' ? 'https' : 'http'; |
|
192 | + |
|
193 | + return $scheme.'://'.$host.'/'.ltrim($path, '/'); |
|
194 | + } |
|
195 | + |
|
196 | + /** |
|
197 | + * Parses a request message string into a request object. |
|
198 | + * |
|
199 | + * @param string $message Request message string. |
|
200 | + */ |
|
201 | + public static function parseRequest(string $message): RequestInterface |
|
202 | + { |
|
203 | + $data = self::parseMessage($message); |
|
204 | + $matches = []; |
|
205 | + if (!preg_match('/^[\S]+\s+([a-zA-Z]+:\/\/|\/).*/', $data['start-line'], $matches)) { |
|
206 | + throw new \InvalidArgumentException('Invalid request string'); |
|
207 | + } |
|
208 | + $parts = explode(' ', $data['start-line'], 3); |
|
209 | + $version = isset($parts[2]) ? explode('/', $parts[2])[1] : '1.1'; |
|
210 | + |
|
211 | + $request = new Request( |
|
212 | + $parts[0], |
|
213 | + $matches[1] === '/' ? self::parseRequestUri($parts[1], $data['headers']) : $parts[1], |
|
214 | + $data['headers'], |
|
215 | + $data['body'], |
|
216 | + $version |
|
217 | + ); |
|
218 | + |
|
219 | + return $matches[1] === '/' ? $request : $request->withRequestTarget($parts[1]); |
|
220 | + } |
|
221 | + |
|
222 | + /** |
|
223 | + * Parses a response message string into a response object. |
|
224 | + * |
|
225 | + * @param string $message Response message string. |
|
226 | + */ |
|
227 | + public static function parseResponse(string $message): ResponseInterface |
|
228 | + { |
|
229 | + $data = self::parseMessage($message); |
|
230 | + // According to https://datatracker.ietf.org/doc/html/rfc7230#section-3.1.2 |
|
231 | + // the space between status-code and reason-phrase is required. But |
|
232 | + // browsers accept responses without space and reason as well. |
|
233 | + if (!preg_match('/^HTTP\/.* [0-9]{3}( .*|$)/', $data['start-line'])) { |
|
234 | + throw new \InvalidArgumentException('Invalid response string: '.$data['start-line']); |
|
235 | + } |
|
236 | + $parts = explode(' ', $data['start-line'], 3); |
|
237 | + |
|
238 | + return new Response( |
|
239 | + (int) $parts[1], |
|
240 | + $data['headers'], |
|
241 | + $data['body'], |
|
242 | + explode('/', $parts[0])[1], |
|
243 | + $parts[2] ?? null |
|
244 | + ); |
|
245 | + } |
|
246 | 246 | } |
@@ -175,9 +175,9 @@ discard block |
||
175 | 175 | */ |
176 | 176 | public static function parseRequestUri(string $path, array $headers): string |
177 | 177 | { |
178 | - $hostKey = array_filter(array_keys($headers), function ($k) { |
|
178 | + $hostKey = array_filter(array_keys($headers), function($k) { |
|
179 | 179 | // Numeric array keys are converted to int by PHP. |
180 | - $k = (string) $k; |
|
180 | + $k = (string)$k; |
|
181 | 181 | |
182 | 182 | return strtolower($k) === 'host'; |
183 | 183 | }); |
@@ -236,7 +236,7 @@ discard block |
||
236 | 236 | $parts = explode(' ', $data['start-line'], 3); |
237 | 237 | |
238 | 238 | return new Response( |
239 | - (int) $parts[1], |
|
239 | + (int)$parts[1], |
|
240 | 240 | $data['headers'], |
241 | 241 | $data['body'], |
242 | 242 | explode('/', $parts[0])[1], |
@@ -8,8 +8,7 @@ |
||
8 | 8 | use OCA\FullTextSearch_Elasticsearch\Vendor\Psr\Http\Message\RequestInterface; |
9 | 9 | use OCA\FullTextSearch_Elasticsearch\Vendor\Psr\Http\Message\ResponseInterface; |
10 | 10 | |
11 | -final class Message |
|
12 | -{ |
|
11 | +final class Message { |
|
13 | 12 | /** |
14 | 13 | * Returns the string representation of an HTTP message. |
15 | 14 | * |
@@ -25,70 +25,70 @@ |
||
25 | 25 | */ |
26 | 26 | final class HttpFactory implements RequestFactoryInterface, ResponseFactoryInterface, ServerRequestFactoryInterface, StreamFactoryInterface, UploadedFileFactoryInterface, UriFactoryInterface |
27 | 27 | { |
28 | - public function createUploadedFile( |
|
29 | - StreamInterface $stream, |
|
30 | - int $size = null, |
|
31 | - int $error = \UPLOAD_ERR_OK, |
|
32 | - string $clientFilename = null, |
|
33 | - string $clientMediaType = null |
|
34 | - ): UploadedFileInterface { |
|
35 | - if ($size === null) { |
|
36 | - $size = $stream->getSize(); |
|
37 | - } |
|
28 | + public function createUploadedFile( |
|
29 | + StreamInterface $stream, |
|
30 | + int $size = null, |
|
31 | + int $error = \UPLOAD_ERR_OK, |
|
32 | + string $clientFilename = null, |
|
33 | + string $clientMediaType = null |
|
34 | + ): UploadedFileInterface { |
|
35 | + if ($size === null) { |
|
36 | + $size = $stream->getSize(); |
|
37 | + } |
|
38 | 38 | |
39 | - return new UploadedFile($stream, $size, $error, $clientFilename, $clientMediaType); |
|
40 | - } |
|
39 | + return new UploadedFile($stream, $size, $error, $clientFilename, $clientMediaType); |
|
40 | + } |
|
41 | 41 | |
42 | - public function createStream(string $content = ''): StreamInterface |
|
43 | - { |
|
44 | - return Utils::streamFor($content); |
|
45 | - } |
|
42 | + public function createStream(string $content = ''): StreamInterface |
|
43 | + { |
|
44 | + return Utils::streamFor($content); |
|
45 | + } |
|
46 | 46 | |
47 | - public function createStreamFromFile(string $file, string $mode = 'r'): StreamInterface |
|
48 | - { |
|
49 | - try { |
|
50 | - $resource = Utils::tryFopen($file, $mode); |
|
51 | - } catch (\RuntimeException $e) { |
|
52 | - if ('' === $mode || false === \in_array($mode[0], ['r', 'w', 'a', 'x', 'c'], true)) { |
|
53 | - throw new \InvalidArgumentException(sprintf('Invalid file opening mode "%s"', $mode), 0, $e); |
|
54 | - } |
|
47 | + public function createStreamFromFile(string $file, string $mode = 'r'): StreamInterface |
|
48 | + { |
|
49 | + try { |
|
50 | + $resource = Utils::tryFopen($file, $mode); |
|
51 | + } catch (\RuntimeException $e) { |
|
52 | + if ('' === $mode || false === \in_array($mode[0], ['r', 'w', 'a', 'x', 'c'], true)) { |
|
53 | + throw new \InvalidArgumentException(sprintf('Invalid file opening mode "%s"', $mode), 0, $e); |
|
54 | + } |
|
55 | 55 | |
56 | - throw $e; |
|
57 | - } |
|
56 | + throw $e; |
|
57 | + } |
|
58 | 58 | |
59 | - return Utils::streamFor($resource); |
|
60 | - } |
|
59 | + return Utils::streamFor($resource); |
|
60 | + } |
|
61 | 61 | |
62 | - public function createStreamFromResource($resource): StreamInterface |
|
63 | - { |
|
64 | - return Utils::streamFor($resource); |
|
65 | - } |
|
62 | + public function createStreamFromResource($resource): StreamInterface |
|
63 | + { |
|
64 | + return Utils::streamFor($resource); |
|
65 | + } |
|
66 | 66 | |
67 | - public function createServerRequest(string $method, $uri, array $serverParams = []): ServerRequestInterface |
|
68 | - { |
|
69 | - if (empty($method)) { |
|
70 | - if (!empty($serverParams['REQUEST_METHOD'])) { |
|
71 | - $method = $serverParams['REQUEST_METHOD']; |
|
72 | - } else { |
|
73 | - throw new \InvalidArgumentException('Cannot determine HTTP method'); |
|
74 | - } |
|
75 | - } |
|
67 | + public function createServerRequest(string $method, $uri, array $serverParams = []): ServerRequestInterface |
|
68 | + { |
|
69 | + if (empty($method)) { |
|
70 | + if (!empty($serverParams['REQUEST_METHOD'])) { |
|
71 | + $method = $serverParams['REQUEST_METHOD']; |
|
72 | + } else { |
|
73 | + throw new \InvalidArgumentException('Cannot determine HTTP method'); |
|
74 | + } |
|
75 | + } |
|
76 | 76 | |
77 | - return new ServerRequest($method, $uri, [], null, '1.1', $serverParams); |
|
78 | - } |
|
77 | + return new ServerRequest($method, $uri, [], null, '1.1', $serverParams); |
|
78 | + } |
|
79 | 79 | |
80 | - public function createResponse(int $code = 200, string $reasonPhrase = ''): ResponseInterface |
|
81 | - { |
|
82 | - return new Response($code, [], null, '1.1', $reasonPhrase); |
|
83 | - } |
|
80 | + public function createResponse(int $code = 200, string $reasonPhrase = ''): ResponseInterface |
|
81 | + { |
|
82 | + return new Response($code, [], null, '1.1', $reasonPhrase); |
|
83 | + } |
|
84 | 84 | |
85 | - public function createRequest(string $method, $uri): RequestInterface |
|
86 | - { |
|
87 | - return new Request($method, $uri); |
|
88 | - } |
|
85 | + public function createRequest(string $method, $uri): RequestInterface |
|
86 | + { |
|
87 | + return new Request($method, $uri); |
|
88 | + } |
|
89 | 89 | |
90 | - public function createUri(string $uri = ''): UriInterface |
|
91 | - { |
|
92 | - return new Uri($uri); |
|
93 | - } |
|
90 | + public function createUri(string $uri = ''): UriInterface |
|
91 | + { |
|
92 | + return new Uri($uri); |
|
93 | + } |
|
94 | 94 | } |
@@ -15,206 +15,206 @@ |
||
15 | 15 | */ |
16 | 16 | final class UriNormalizer |
17 | 17 | { |
18 | - /** |
|
19 | - * Default normalizations which only include the ones that preserve semantics. |
|
20 | - */ |
|
21 | - public const PRESERVING_NORMALIZATIONS = |
|
22 | - self::CAPITALIZE_PERCENT_ENCODING | |
|
23 | - self::DECODE_UNRESERVED_CHARACTERS | |
|
24 | - self::CONVERT_EMPTY_PATH | |
|
25 | - self::REMOVE_DEFAULT_HOST | |
|
26 | - self::REMOVE_DEFAULT_PORT | |
|
27 | - self::REMOVE_DOT_SEGMENTS; |
|
28 | - |
|
29 | - /** |
|
30 | - * All letters within a percent-encoding triplet (e.g., "%3A") are case-insensitive, and should be capitalized. |
|
31 | - * |
|
32 | - * Example: http://example.org/a%c2%b1b → http://example.org/a%C2%B1b |
|
33 | - */ |
|
34 | - public const CAPITALIZE_PERCENT_ENCODING = 1; |
|
35 | - |
|
36 | - /** |
|
37 | - * Decodes percent-encoded octets of unreserved characters. |
|
38 | - * |
|
39 | - * For consistency, percent-encoded octets in the ranges of ALPHA (%41–%5A and %61–%7A), DIGIT (%30–%39), |
|
40 | - * hyphen (%2D), period (%2E), underscore (%5F), or tilde (%7E) should not be created by URI producers and, |
|
41 | - * when found in a URI, should be decoded to their corresponding unreserved characters by URI normalizers. |
|
42 | - * |
|
43 | - * Example: http://example.org/%7Eusern%61me/ → http://example.org/~username/ |
|
44 | - */ |
|
45 | - public const DECODE_UNRESERVED_CHARACTERS = 2; |
|
46 | - |
|
47 | - /** |
|
48 | - * Converts the empty path to "/" for http and https URIs. |
|
49 | - * |
|
50 | - * Example: http://example.org → http://example.org/ |
|
51 | - */ |
|
52 | - public const CONVERT_EMPTY_PATH = 4; |
|
53 | - |
|
54 | - /** |
|
55 | - * Removes the default host of the given URI scheme from the URI. |
|
56 | - * |
|
57 | - * Only the "file" scheme defines the default host "localhost". |
|
58 | - * All of `file:/myfile`, `file:///myfile`, and `file://localhost/myfile` |
|
59 | - * are equivalent according to RFC 3986. The first format is not accepted |
|
60 | - * by PHPs stream functions and thus already normalized implicitly to the |
|
61 | - * second format in the Uri class. See `OCA\FullTextSearch_Elasticsearch\Vendor\GuzzleHttp\Psr7\Uri::composeComponents`. |
|
62 | - * |
|
63 | - * Example: file://localhost/myfile → file:///myfile |
|
64 | - */ |
|
65 | - public const REMOVE_DEFAULT_HOST = 8; |
|
66 | - |
|
67 | - /** |
|
68 | - * Removes the default port of the given URI scheme from the URI. |
|
69 | - * |
|
70 | - * Example: http://example.org:80/ → http://example.org/ |
|
71 | - */ |
|
72 | - public const REMOVE_DEFAULT_PORT = 16; |
|
73 | - |
|
74 | - /** |
|
75 | - * Removes unnecessary dot-segments. |
|
76 | - * |
|
77 | - * Dot-segments in relative-path references are not removed as it would |
|
78 | - * change the semantics of the URI reference. |
|
79 | - * |
|
80 | - * Example: http://example.org/../a/b/../c/./d.html → http://example.org/a/c/d.html |
|
81 | - */ |
|
82 | - public const REMOVE_DOT_SEGMENTS = 32; |
|
83 | - |
|
84 | - /** |
|
85 | - * Paths which include two or more adjacent slashes are converted to one. |
|
86 | - * |
|
87 | - * Webservers usually ignore duplicate slashes and treat those URIs equivalent. |
|
88 | - * But in theory those URIs do not need to be equivalent. So this normalization |
|
89 | - * may change the semantics. Encoded slashes (%2F) are not removed. |
|
90 | - * |
|
91 | - * Example: http://example.org//foo///bar.html → http://example.org/foo/bar.html |
|
92 | - */ |
|
93 | - public const REMOVE_DUPLICATE_SLASHES = 64; |
|
94 | - |
|
95 | - /** |
|
96 | - * Sort query parameters with their values in alphabetical order. |
|
97 | - * |
|
98 | - * However, the order of parameters in a URI may be significant (this is not defined by the standard). |
|
99 | - * So this normalization is not safe and may change the semantics of the URI. |
|
100 | - * |
|
101 | - * Example: ?lang=en&article=fred → ?article=fred&lang=en |
|
102 | - * |
|
103 | - * Note: The sorting is neither locale nor Unicode aware (the URI query does not get decoded at all) as the |
|
104 | - * purpose is to be able to compare URIs in a reproducible way, not to have the params sorted perfectly. |
|
105 | - */ |
|
106 | - public const SORT_QUERY_PARAMETERS = 128; |
|
107 | - |
|
108 | - /** |
|
109 | - * Returns a normalized URI. |
|
110 | - * |
|
111 | - * The scheme and host component are already normalized to lowercase per PSR-7 UriInterface. |
|
112 | - * This methods adds additional normalizations that can be configured with the $flags parameter. |
|
113 | - * |
|
114 | - * PSR-7 UriInterface cannot distinguish between an empty component and a missing component as |
|
115 | - * getQuery(), getFragment() etc. always return a string. This means the URIs "/?#" and "/" are |
|
116 | - * treated equivalent which is not necessarily true according to RFC 3986. But that difference |
|
117 | - * is highly uncommon in reality. So this potential normalization is implied in PSR-7 as well. |
|
118 | - * |
|
119 | - * @param UriInterface $uri The URI to normalize |
|
120 | - * @param int $flags A bitmask of normalizations to apply, see constants |
|
121 | - * |
|
122 | - * @see https://datatracker.ietf.org/doc/html/rfc3986#section-6.2 |
|
123 | - */ |
|
124 | - public static function normalize(UriInterface $uri, int $flags = self::PRESERVING_NORMALIZATIONS): UriInterface |
|
125 | - { |
|
126 | - if ($flags & self::CAPITALIZE_PERCENT_ENCODING) { |
|
127 | - $uri = self::capitalizePercentEncoding($uri); |
|
128 | - } |
|
129 | - |
|
130 | - if ($flags & self::DECODE_UNRESERVED_CHARACTERS) { |
|
131 | - $uri = self::decodeUnreservedCharacters($uri); |
|
132 | - } |
|
133 | - |
|
134 | - if ($flags & self::CONVERT_EMPTY_PATH && $uri->getPath() === '' |
|
135 | - && ($uri->getScheme() === 'http' || $uri->getScheme() === 'https') |
|
136 | - ) { |
|
137 | - $uri = $uri->withPath('/'); |
|
138 | - } |
|
139 | - |
|
140 | - if ($flags & self::REMOVE_DEFAULT_HOST && $uri->getScheme() === 'file' && $uri->getHost() === 'localhost') { |
|
141 | - $uri = $uri->withHost(''); |
|
142 | - } |
|
143 | - |
|
144 | - if ($flags & self::REMOVE_DEFAULT_PORT && $uri->getPort() !== null && Uri::isDefaultPort($uri)) { |
|
145 | - $uri = $uri->withPort(null); |
|
146 | - } |
|
147 | - |
|
148 | - if ($flags & self::REMOVE_DOT_SEGMENTS && !Uri::isRelativePathReference($uri)) { |
|
149 | - $uri = $uri->withPath(UriResolver::removeDotSegments($uri->getPath())); |
|
150 | - } |
|
151 | - |
|
152 | - if ($flags & self::REMOVE_DUPLICATE_SLASHES) { |
|
153 | - $uri = $uri->withPath(preg_replace('#//++#', '/', $uri->getPath())); |
|
154 | - } |
|
155 | - |
|
156 | - if ($flags & self::SORT_QUERY_PARAMETERS && $uri->getQuery() !== '') { |
|
157 | - $queryKeyValues = explode('&', $uri->getQuery()); |
|
158 | - sort($queryKeyValues); |
|
159 | - $uri = $uri->withQuery(implode('&', $queryKeyValues)); |
|
160 | - } |
|
161 | - |
|
162 | - return $uri; |
|
163 | - } |
|
164 | - |
|
165 | - /** |
|
166 | - * Whether two URIs can be considered equivalent. |
|
167 | - * |
|
168 | - * Both URIs are normalized automatically before comparison with the given $normalizations bitmask. The method also |
|
169 | - * accepts relative URI references and returns true when they are equivalent. This of course assumes they will be |
|
170 | - * resolved against the same base URI. If this is not the case, determination of equivalence or difference of |
|
171 | - * relative references does not mean anything. |
|
172 | - * |
|
173 | - * @param UriInterface $uri1 An URI to compare |
|
174 | - * @param UriInterface $uri2 An URI to compare |
|
175 | - * @param int $normalizations A bitmask of normalizations to apply, see constants |
|
176 | - * |
|
177 | - * @see https://datatracker.ietf.org/doc/html/rfc3986#section-6.1 |
|
178 | - */ |
|
179 | - public static function isEquivalent(UriInterface $uri1, UriInterface $uri2, int $normalizations = self::PRESERVING_NORMALIZATIONS): bool |
|
180 | - { |
|
181 | - return (string) self::normalize($uri1, $normalizations) === (string) self::normalize($uri2, $normalizations); |
|
182 | - } |
|
183 | - |
|
184 | - private static function capitalizePercentEncoding(UriInterface $uri): UriInterface |
|
185 | - { |
|
186 | - $regex = '/(?:%[A-Fa-f0-9]{2})++/'; |
|
187 | - |
|
188 | - $callback = function (array $match): string { |
|
189 | - return strtoupper($match[0]); |
|
190 | - }; |
|
191 | - |
|
192 | - return |
|
193 | - $uri->withPath( |
|
194 | - preg_replace_callback($regex, $callback, $uri->getPath()) |
|
195 | - )->withQuery( |
|
196 | - preg_replace_callback($regex, $callback, $uri->getQuery()) |
|
197 | - ); |
|
198 | - } |
|
199 | - |
|
200 | - private static function decodeUnreservedCharacters(UriInterface $uri): UriInterface |
|
201 | - { |
|
202 | - $regex = '/%(?:2D|2E|5F|7E|3[0-9]|[46][1-9A-F]|[57][0-9A])/i'; |
|
203 | - |
|
204 | - $callback = function (array $match): string { |
|
205 | - return rawurldecode($match[0]); |
|
206 | - }; |
|
207 | - |
|
208 | - return |
|
209 | - $uri->withPath( |
|
210 | - preg_replace_callback($regex, $callback, $uri->getPath()) |
|
211 | - )->withQuery( |
|
212 | - preg_replace_callback($regex, $callback, $uri->getQuery()) |
|
213 | - ); |
|
214 | - } |
|
215 | - |
|
216 | - private function __construct() |
|
217 | - { |
|
218 | - // cannot be instantiated |
|
219 | - } |
|
18 | + /** |
|
19 | + * Default normalizations which only include the ones that preserve semantics. |
|
20 | + */ |
|
21 | + public const PRESERVING_NORMALIZATIONS = |
|
22 | + self::CAPITALIZE_PERCENT_ENCODING | |
|
23 | + self::DECODE_UNRESERVED_CHARACTERS | |
|
24 | + self::CONVERT_EMPTY_PATH | |
|
25 | + self::REMOVE_DEFAULT_HOST | |
|
26 | + self::REMOVE_DEFAULT_PORT | |
|
27 | + self::REMOVE_DOT_SEGMENTS; |
|
28 | + |
|
29 | + /** |
|
30 | + * All letters within a percent-encoding triplet (e.g., "%3A") are case-insensitive, and should be capitalized. |
|
31 | + * |
|
32 | + * Example: http://example.org/a%c2%b1b → http://example.org/a%C2%B1b |
|
33 | + */ |
|
34 | + public const CAPITALIZE_PERCENT_ENCODING = 1; |
|
35 | + |
|
36 | + /** |
|
37 | + * Decodes percent-encoded octets of unreserved characters. |
|
38 | + * |
|
39 | + * For consistency, percent-encoded octets in the ranges of ALPHA (%41–%5A and %61–%7A), DIGIT (%30–%39), |
|
40 | + * hyphen (%2D), period (%2E), underscore (%5F), or tilde (%7E) should not be created by URI producers and, |
|
41 | + * when found in a URI, should be decoded to their corresponding unreserved characters by URI normalizers. |
|
42 | + * |
|
43 | + * Example: http://example.org/%7Eusern%61me/ → http://example.org/~username/ |
|
44 | + */ |
|
45 | + public const DECODE_UNRESERVED_CHARACTERS = 2; |
|
46 | + |
|
47 | + /** |
|
48 | + * Converts the empty path to "/" for http and https URIs. |
|
49 | + * |
|
50 | + * Example: http://example.org → http://example.org/ |
|
51 | + */ |
|
52 | + public const CONVERT_EMPTY_PATH = 4; |
|
53 | + |
|
54 | + /** |
|
55 | + * Removes the default host of the given URI scheme from the URI. |
|
56 | + * |
|
57 | + * Only the "file" scheme defines the default host "localhost". |
|
58 | + * All of `file:/myfile`, `file:///myfile`, and `file://localhost/myfile` |
|
59 | + * are equivalent according to RFC 3986. The first format is not accepted |
|
60 | + * by PHPs stream functions and thus already normalized implicitly to the |
|
61 | + * second format in the Uri class. See `OCA\FullTextSearch_Elasticsearch\Vendor\GuzzleHttp\Psr7\Uri::composeComponents`. |
|
62 | + * |
|
63 | + * Example: file://localhost/myfile → file:///myfile |
|
64 | + */ |
|
65 | + public const REMOVE_DEFAULT_HOST = 8; |
|
66 | + |
|
67 | + /** |
|
68 | + * Removes the default port of the given URI scheme from the URI. |
|
69 | + * |
|
70 | + * Example: http://example.org:80/ → http://example.org/ |
|
71 | + */ |
|
72 | + public const REMOVE_DEFAULT_PORT = 16; |
|
73 | + |
|
74 | + /** |
|
75 | + * Removes unnecessary dot-segments. |
|
76 | + * |
|
77 | + * Dot-segments in relative-path references are not removed as it would |
|
78 | + * change the semantics of the URI reference. |
|
79 | + * |
|
80 | + * Example: http://example.org/../a/b/../c/./d.html → http://example.org/a/c/d.html |
|
81 | + */ |
|
82 | + public const REMOVE_DOT_SEGMENTS = 32; |
|
83 | + |
|
84 | + /** |
|
85 | + * Paths which include two or more adjacent slashes are converted to one. |
|
86 | + * |
|
87 | + * Webservers usually ignore duplicate slashes and treat those URIs equivalent. |
|
88 | + * But in theory those URIs do not need to be equivalent. So this normalization |
|
89 | + * may change the semantics. Encoded slashes (%2F) are not removed. |
|
90 | + * |
|
91 | + * Example: http://example.org//foo///bar.html → http://example.org/foo/bar.html |
|
92 | + */ |
|
93 | + public const REMOVE_DUPLICATE_SLASHES = 64; |
|
94 | + |
|
95 | + /** |
|
96 | + * Sort query parameters with their values in alphabetical order. |
|
97 | + * |
|
98 | + * However, the order of parameters in a URI may be significant (this is not defined by the standard). |
|
99 | + * So this normalization is not safe and may change the semantics of the URI. |
|
100 | + * |
|
101 | + * Example: ?lang=en&article=fred → ?article=fred&lang=en |
|
102 | + * |
|
103 | + * Note: The sorting is neither locale nor Unicode aware (the URI query does not get decoded at all) as the |
|
104 | + * purpose is to be able to compare URIs in a reproducible way, not to have the params sorted perfectly. |
|
105 | + */ |
|
106 | + public const SORT_QUERY_PARAMETERS = 128; |
|
107 | + |
|
108 | + /** |
|
109 | + * Returns a normalized URI. |
|
110 | + * |
|
111 | + * The scheme and host component are already normalized to lowercase per PSR-7 UriInterface. |
|
112 | + * This methods adds additional normalizations that can be configured with the $flags parameter. |
|
113 | + * |
|
114 | + * PSR-7 UriInterface cannot distinguish between an empty component and a missing component as |
|
115 | + * getQuery(), getFragment() etc. always return a string. This means the URIs "/?#" and "/" are |
|
116 | + * treated equivalent which is not necessarily true according to RFC 3986. But that difference |
|
117 | + * is highly uncommon in reality. So this potential normalization is implied in PSR-7 as well. |
|
118 | + * |
|
119 | + * @param UriInterface $uri The URI to normalize |
|
120 | + * @param int $flags A bitmask of normalizations to apply, see constants |
|
121 | + * |
|
122 | + * @see https://datatracker.ietf.org/doc/html/rfc3986#section-6.2 |
|
123 | + */ |
|
124 | + public static function normalize(UriInterface $uri, int $flags = self::PRESERVING_NORMALIZATIONS): UriInterface |
|
125 | + { |
|
126 | + if ($flags & self::CAPITALIZE_PERCENT_ENCODING) { |
|
127 | + $uri = self::capitalizePercentEncoding($uri); |
|
128 | + } |
|
129 | + |
|
130 | + if ($flags & self::DECODE_UNRESERVED_CHARACTERS) { |
|
131 | + $uri = self::decodeUnreservedCharacters($uri); |
|
132 | + } |
|
133 | + |
|
134 | + if ($flags & self::CONVERT_EMPTY_PATH && $uri->getPath() === '' |
|
135 | + && ($uri->getScheme() === 'http' || $uri->getScheme() === 'https') |
|
136 | + ) { |
|
137 | + $uri = $uri->withPath('/'); |
|
138 | + } |
|
139 | + |
|
140 | + if ($flags & self::REMOVE_DEFAULT_HOST && $uri->getScheme() === 'file' && $uri->getHost() === 'localhost') { |
|
141 | + $uri = $uri->withHost(''); |
|
142 | + } |
|
143 | + |
|
144 | + if ($flags & self::REMOVE_DEFAULT_PORT && $uri->getPort() !== null && Uri::isDefaultPort($uri)) { |
|
145 | + $uri = $uri->withPort(null); |
|
146 | + } |
|
147 | + |
|
148 | + if ($flags & self::REMOVE_DOT_SEGMENTS && !Uri::isRelativePathReference($uri)) { |
|
149 | + $uri = $uri->withPath(UriResolver::removeDotSegments($uri->getPath())); |
|
150 | + } |
|
151 | + |
|
152 | + if ($flags & self::REMOVE_DUPLICATE_SLASHES) { |
|
153 | + $uri = $uri->withPath(preg_replace('#//++#', '/', $uri->getPath())); |
|
154 | + } |
|
155 | + |
|
156 | + if ($flags & self::SORT_QUERY_PARAMETERS && $uri->getQuery() !== '') { |
|
157 | + $queryKeyValues = explode('&', $uri->getQuery()); |
|
158 | + sort($queryKeyValues); |
|
159 | + $uri = $uri->withQuery(implode('&', $queryKeyValues)); |
|
160 | + } |
|
161 | + |
|
162 | + return $uri; |
|
163 | + } |
|
164 | + |
|
165 | + /** |
|
166 | + * Whether two URIs can be considered equivalent. |
|
167 | + * |
|
168 | + * Both URIs are normalized automatically before comparison with the given $normalizations bitmask. The method also |
|
169 | + * accepts relative URI references and returns true when they are equivalent. This of course assumes they will be |
|
170 | + * resolved against the same base URI. If this is not the case, determination of equivalence or difference of |
|
171 | + * relative references does not mean anything. |
|
172 | + * |
|
173 | + * @param UriInterface $uri1 An URI to compare |
|
174 | + * @param UriInterface $uri2 An URI to compare |
|
175 | + * @param int $normalizations A bitmask of normalizations to apply, see constants |
|
176 | + * |
|
177 | + * @see https://datatracker.ietf.org/doc/html/rfc3986#section-6.1 |
|
178 | + */ |
|
179 | + public static function isEquivalent(UriInterface $uri1, UriInterface $uri2, int $normalizations = self::PRESERVING_NORMALIZATIONS): bool |
|
180 | + { |
|
181 | + return (string) self::normalize($uri1, $normalizations) === (string) self::normalize($uri2, $normalizations); |
|
182 | + } |
|
183 | + |
|
184 | + private static function capitalizePercentEncoding(UriInterface $uri): UriInterface |
|
185 | + { |
|
186 | + $regex = '/(?:%[A-Fa-f0-9]{2})++/'; |
|
187 | + |
|
188 | + $callback = function (array $match): string { |
|
189 | + return strtoupper($match[0]); |
|
190 | + }; |
|
191 | + |
|
192 | + return |
|
193 | + $uri->withPath( |
|
194 | + preg_replace_callback($regex, $callback, $uri->getPath()) |
|
195 | + )->withQuery( |
|
196 | + preg_replace_callback($regex, $callback, $uri->getQuery()) |
|
197 | + ); |
|
198 | + } |
|
199 | + |
|
200 | + private static function decodeUnreservedCharacters(UriInterface $uri): UriInterface |
|
201 | + { |
|
202 | + $regex = '/%(?:2D|2E|5F|7E|3[0-9]|[46][1-9A-F]|[57][0-9A])/i'; |
|
203 | + |
|
204 | + $callback = function (array $match): string { |
|
205 | + return rawurldecode($match[0]); |
|
206 | + }; |
|
207 | + |
|
208 | + return |
|
209 | + $uri->withPath( |
|
210 | + preg_replace_callback($regex, $callback, $uri->getPath()) |
|
211 | + )->withQuery( |
|
212 | + preg_replace_callback($regex, $callback, $uri->getQuery()) |
|
213 | + ); |
|
214 | + } |
|
215 | + |
|
216 | + private function __construct() |
|
217 | + { |
|
218 | + // cannot be instantiated |
|
219 | + } |
|
220 | 220 | } |
@@ -178,14 +178,14 @@ discard block |
||
178 | 178 | */ |
179 | 179 | public static function isEquivalent(UriInterface $uri1, UriInterface $uri2, int $normalizations = self::PRESERVING_NORMALIZATIONS): bool |
180 | 180 | { |
181 | - return (string) self::normalize($uri1, $normalizations) === (string) self::normalize($uri2, $normalizations); |
|
181 | + return (string)self::normalize($uri1, $normalizations) === (string)self::normalize($uri2, $normalizations); |
|
182 | 182 | } |
183 | 183 | |
184 | 184 | private static function capitalizePercentEncoding(UriInterface $uri): UriInterface |
185 | 185 | { |
186 | 186 | $regex = '/(?:%[A-Fa-f0-9]{2})++/'; |
187 | 187 | |
188 | - $callback = function (array $match): string { |
|
188 | + $callback = function(array $match): string { |
|
189 | 189 | return strtoupper($match[0]); |
190 | 190 | }; |
191 | 191 | |
@@ -201,7 +201,7 @@ discard block |
||
201 | 201 | { |
202 | 202 | $regex = '/%(?:2D|2E|5F|7E|3[0-9]|[46][1-9A-F]|[57][0-9A])/i'; |
203 | 203 | |
204 | - $callback = function (array $match): string { |
|
204 | + $callback = function(array $match): string { |
|
205 | 205 | return rawurldecode($match[0]); |
206 | 206 | }; |
207 | 207 |
@@ -13,8 +13,7 @@ |
||
13 | 13 | * |
14 | 14 | * @see https://datatracker.ietf.org/doc/html/rfc3986#section-6 |
15 | 15 | */ |
16 | -final class UriNormalizer |
|
17 | -{ |
|
16 | +final class UriNormalizer { |
|
18 | 17 | /** |
19 | 18 | * Default normalizations which only include the ones that preserve semantics. |
20 | 19 | */ |
@@ -11,18 +11,18 @@ |
||
11 | 11 | */ |
12 | 12 | final class NoSeekStream implements StreamInterface |
13 | 13 | { |
14 | - use StreamDecoratorTrait; |
|
14 | + use StreamDecoratorTrait; |
|
15 | 15 | |
16 | - /** @var StreamInterface */ |
|
17 | - private $stream; |
|
16 | + /** @var StreamInterface */ |
|
17 | + private $stream; |
|
18 | 18 | |
19 | - public function seek($offset, $whence = SEEK_SET): void |
|
20 | - { |
|
21 | - throw new \RuntimeException('Cannot seek a NoSeekStream'); |
|
22 | - } |
|
19 | + public function seek($offset, $whence = SEEK_SET): void |
|
20 | + { |
|
21 | + throw new \RuntimeException('Cannot seek a NoSeekStream'); |
|
22 | + } |
|
23 | 23 | |
24 | - public function isSeekable(): bool |
|
25 | - { |
|
26 | - return false; |
|
27 | - } |
|
24 | + public function isSeekable(): bool |
|
25 | + { |
|
26 | + return false; |
|
27 | + } |
|
28 | 28 | } |
@@ -9,8 +9,7 @@ |
||
9 | 9 | /** |
10 | 10 | * Stream decorator that prevents a stream from being seeked. |
11 | 11 | */ |
12 | -final class NoSeekStream implements StreamInterface |
|
13 | -{ |
|
12 | +final class NoSeekStream implements StreamInterface { |
|
14 | 13 | use StreamDecoratorTrait; |
15 | 14 | |
16 | 15 | /** @var StreamInterface */ |
@@ -13,40 +13,40 @@ |
||
13 | 13 | */ |
14 | 14 | final class UriComparator |
15 | 15 | { |
16 | - /** |
|
17 | - * Determines if a modified URL should be considered cross-origin with |
|
18 | - * respect to an original URL. |
|
19 | - */ |
|
20 | - public static function isCrossOrigin(UriInterface $original, UriInterface $modified): bool |
|
21 | - { |
|
22 | - if (\strcasecmp($original->getHost(), $modified->getHost()) !== 0) { |
|
23 | - return true; |
|
24 | - } |
|
25 | - |
|
26 | - if ($original->getScheme() !== $modified->getScheme()) { |
|
27 | - return true; |
|
28 | - } |
|
29 | - |
|
30 | - if (self::computePort($original) !== self::computePort($modified)) { |
|
31 | - return true; |
|
32 | - } |
|
33 | - |
|
34 | - return false; |
|
35 | - } |
|
36 | - |
|
37 | - private static function computePort(UriInterface $uri): int |
|
38 | - { |
|
39 | - $port = $uri->getPort(); |
|
40 | - |
|
41 | - if (null !== $port) { |
|
42 | - return $port; |
|
43 | - } |
|
44 | - |
|
45 | - return 'https' === $uri->getScheme() ? 443 : 80; |
|
46 | - } |
|
47 | - |
|
48 | - private function __construct() |
|
49 | - { |
|
50 | - // cannot be instantiated |
|
51 | - } |
|
16 | + /** |
|
17 | + * Determines if a modified URL should be considered cross-origin with |
|
18 | + * respect to an original URL. |
|
19 | + */ |
|
20 | + public static function isCrossOrigin(UriInterface $original, UriInterface $modified): bool |
|
21 | + { |
|
22 | + if (\strcasecmp($original->getHost(), $modified->getHost()) !== 0) { |
|
23 | + return true; |
|
24 | + } |
|
25 | + |
|
26 | + if ($original->getScheme() !== $modified->getScheme()) { |
|
27 | + return true; |
|
28 | + } |
|
29 | + |
|
30 | + if (self::computePort($original) !== self::computePort($modified)) { |
|
31 | + return true; |
|
32 | + } |
|
33 | + |
|
34 | + return false; |
|
35 | + } |
|
36 | + |
|
37 | + private static function computePort(UriInterface $uri): int |
|
38 | + { |
|
39 | + $port = $uri->getPort(); |
|
40 | + |
|
41 | + if (null !== $port) { |
|
42 | + return $port; |
|
43 | + } |
|
44 | + |
|
45 | + return 'https' === $uri->getScheme() ? 443 : 80; |
|
46 | + } |
|
47 | + |
|
48 | + private function __construct() |
|
49 | + { |
|
50 | + // cannot be instantiated |
|
51 | + } |
|
52 | 52 | } |
@@ -11,8 +11,7 @@ |
||
11 | 11 | * |
12 | 12 | * @author Graham Campbell |
13 | 13 | */ |
14 | -final class UriComparator |
|
15 | -{ |
|
14 | +final class UriComparator { |
|
16 | 15 | /** |
17 | 16 | * Determines if a modified URL should be considered cross-origin with |
18 | 17 | * respect to an original URL. |
@@ -15,166 +15,166 @@ |
||
15 | 15 | #[\AllowDynamicProperties] |
16 | 16 | final class FnStream implements StreamInterface |
17 | 17 | { |
18 | - private const SLOTS = [ |
|
19 | - '__toString', 'close', 'detach', 'rewind', |
|
20 | - 'getSize', 'tell', 'eof', 'isSeekable', 'seek', 'isWritable', 'write', |
|
21 | - 'isReadable', 'read', 'getContents', 'getMetadata', |
|
22 | - ]; |
|
23 | - |
|
24 | - /** @var array<string, callable> */ |
|
25 | - private $methods; |
|
26 | - |
|
27 | - /** |
|
28 | - * @param array<string, callable> $methods Hash of method name to a callable. |
|
29 | - */ |
|
30 | - public function __construct(array $methods) |
|
31 | - { |
|
32 | - $this->methods = $methods; |
|
33 | - |
|
34 | - // Create the functions on the class |
|
35 | - foreach ($methods as $name => $fn) { |
|
36 | - $this->{'_fn_'.$name} = $fn; |
|
37 | - } |
|
38 | - } |
|
39 | - |
|
40 | - /** |
|
41 | - * Lazily determine which methods are not implemented. |
|
42 | - * |
|
43 | - * @throws \BadMethodCallException |
|
44 | - */ |
|
45 | - public function __get(string $name): void |
|
46 | - { |
|
47 | - throw new \BadMethodCallException(str_replace('_fn_', '', $name) |
|
48 | - .'() is not implemented in the FnStream'); |
|
49 | - } |
|
50 | - |
|
51 | - /** |
|
52 | - * The close method is called on the underlying stream only if possible. |
|
53 | - */ |
|
54 | - public function __destruct() |
|
55 | - { |
|
56 | - if (isset($this->_fn_close)) { |
|
57 | - ($this->_fn_close)(); |
|
58 | - } |
|
59 | - } |
|
60 | - |
|
61 | - /** |
|
62 | - * An unserialize would allow the __destruct to run when the unserialized value goes out of scope. |
|
63 | - * |
|
64 | - * @throws \LogicException |
|
65 | - */ |
|
66 | - public function __wakeup(): void |
|
67 | - { |
|
68 | - throw new \LogicException('FnStream should never be unserialized'); |
|
69 | - } |
|
70 | - |
|
71 | - /** |
|
72 | - * Adds custom functionality to an underlying stream by intercepting |
|
73 | - * specific method calls. |
|
74 | - * |
|
75 | - * @param StreamInterface $stream Stream to decorate |
|
76 | - * @param array<string, callable> $methods Hash of method name to a closure |
|
77 | - * |
|
78 | - * @return FnStream |
|
79 | - */ |
|
80 | - public static function decorate(StreamInterface $stream, array $methods) |
|
81 | - { |
|
82 | - // If any of the required methods were not provided, then simply |
|
83 | - // proxy to the decorated stream. |
|
84 | - foreach (array_diff(self::SLOTS, array_keys($methods)) as $diff) { |
|
85 | - /** @var callable $callable */ |
|
86 | - $callable = [$stream, $diff]; |
|
87 | - $methods[$diff] = $callable; |
|
88 | - } |
|
89 | - |
|
90 | - return new self($methods); |
|
91 | - } |
|
92 | - |
|
93 | - public function __toString(): string |
|
94 | - { |
|
95 | - try { |
|
96 | - /** @var string */ |
|
97 | - return ($this->_fn___toString)(); |
|
98 | - } catch (\Throwable $e) { |
|
99 | - if (\PHP_VERSION_ID >= 70400) { |
|
100 | - throw $e; |
|
101 | - } |
|
102 | - trigger_error(sprintf('%s::__toString exception: %s', self::class, (string) $e), E_USER_ERROR); |
|
103 | - |
|
104 | - return ''; |
|
105 | - } |
|
106 | - } |
|
107 | - |
|
108 | - public function close(): void |
|
109 | - { |
|
110 | - ($this->_fn_close)(); |
|
111 | - } |
|
112 | - |
|
113 | - public function detach() |
|
114 | - { |
|
115 | - return ($this->_fn_detach)(); |
|
116 | - } |
|
117 | - |
|
118 | - public function getSize(): ?int |
|
119 | - { |
|
120 | - return ($this->_fn_getSize)(); |
|
121 | - } |
|
122 | - |
|
123 | - public function tell(): int |
|
124 | - { |
|
125 | - return ($this->_fn_tell)(); |
|
126 | - } |
|
127 | - |
|
128 | - public function eof(): bool |
|
129 | - { |
|
130 | - return ($this->_fn_eof)(); |
|
131 | - } |
|
132 | - |
|
133 | - public function isSeekable(): bool |
|
134 | - { |
|
135 | - return ($this->_fn_isSeekable)(); |
|
136 | - } |
|
137 | - |
|
138 | - public function rewind(): void |
|
139 | - { |
|
140 | - ($this->_fn_rewind)(); |
|
141 | - } |
|
142 | - |
|
143 | - public function seek($offset, $whence = SEEK_SET): void |
|
144 | - { |
|
145 | - ($this->_fn_seek)($offset, $whence); |
|
146 | - } |
|
147 | - |
|
148 | - public function isWritable(): bool |
|
149 | - { |
|
150 | - return ($this->_fn_isWritable)(); |
|
151 | - } |
|
152 | - |
|
153 | - public function write($string): int |
|
154 | - { |
|
155 | - return ($this->_fn_write)($string); |
|
156 | - } |
|
157 | - |
|
158 | - public function isReadable(): bool |
|
159 | - { |
|
160 | - return ($this->_fn_isReadable)(); |
|
161 | - } |
|
162 | - |
|
163 | - public function read($length): string |
|
164 | - { |
|
165 | - return ($this->_fn_read)($length); |
|
166 | - } |
|
167 | - |
|
168 | - public function getContents(): string |
|
169 | - { |
|
170 | - return ($this->_fn_getContents)(); |
|
171 | - } |
|
172 | - |
|
173 | - /** |
|
174 | - * @return mixed |
|
175 | - */ |
|
176 | - public function getMetadata($key = null) |
|
177 | - { |
|
178 | - return ($this->_fn_getMetadata)($key); |
|
179 | - } |
|
18 | + private const SLOTS = [ |
|
19 | + '__toString', 'close', 'detach', 'rewind', |
|
20 | + 'getSize', 'tell', 'eof', 'isSeekable', 'seek', 'isWritable', 'write', |
|
21 | + 'isReadable', 'read', 'getContents', 'getMetadata', |
|
22 | + ]; |
|
23 | + |
|
24 | + /** @var array<string, callable> */ |
|
25 | + private $methods; |
|
26 | + |
|
27 | + /** |
|
28 | + * @param array<string, callable> $methods Hash of method name to a callable. |
|
29 | + */ |
|
30 | + public function __construct(array $methods) |
|
31 | + { |
|
32 | + $this->methods = $methods; |
|
33 | + |
|
34 | + // Create the functions on the class |
|
35 | + foreach ($methods as $name => $fn) { |
|
36 | + $this->{'_fn_'.$name} = $fn; |
|
37 | + } |
|
38 | + } |
|
39 | + |
|
40 | + /** |
|
41 | + * Lazily determine which methods are not implemented. |
|
42 | + * |
|
43 | + * @throws \BadMethodCallException |
|
44 | + */ |
|
45 | + public function __get(string $name): void |
|
46 | + { |
|
47 | + throw new \BadMethodCallException(str_replace('_fn_', '', $name) |
|
48 | + .'() is not implemented in the FnStream'); |
|
49 | + } |
|
50 | + |
|
51 | + /** |
|
52 | + * The close method is called on the underlying stream only if possible. |
|
53 | + */ |
|
54 | + public function __destruct() |
|
55 | + { |
|
56 | + if (isset($this->_fn_close)) { |
|
57 | + ($this->_fn_close)(); |
|
58 | + } |
|
59 | + } |
|
60 | + |
|
61 | + /** |
|
62 | + * An unserialize would allow the __destruct to run when the unserialized value goes out of scope. |
|
63 | + * |
|
64 | + * @throws \LogicException |
|
65 | + */ |
|
66 | + public function __wakeup(): void |
|
67 | + { |
|
68 | + throw new \LogicException('FnStream should never be unserialized'); |
|
69 | + } |
|
70 | + |
|
71 | + /** |
|
72 | + * Adds custom functionality to an underlying stream by intercepting |
|
73 | + * specific method calls. |
|
74 | + * |
|
75 | + * @param StreamInterface $stream Stream to decorate |
|
76 | + * @param array<string, callable> $methods Hash of method name to a closure |
|
77 | + * |
|
78 | + * @return FnStream |
|
79 | + */ |
|
80 | + public static function decorate(StreamInterface $stream, array $methods) |
|
81 | + { |
|
82 | + // If any of the required methods were not provided, then simply |
|
83 | + // proxy to the decorated stream. |
|
84 | + foreach (array_diff(self::SLOTS, array_keys($methods)) as $diff) { |
|
85 | + /** @var callable $callable */ |
|
86 | + $callable = [$stream, $diff]; |
|
87 | + $methods[$diff] = $callable; |
|
88 | + } |
|
89 | + |
|
90 | + return new self($methods); |
|
91 | + } |
|
92 | + |
|
93 | + public function __toString(): string |
|
94 | + { |
|
95 | + try { |
|
96 | + /** @var string */ |
|
97 | + return ($this->_fn___toString)(); |
|
98 | + } catch (\Throwable $e) { |
|
99 | + if (\PHP_VERSION_ID >= 70400) { |
|
100 | + throw $e; |
|
101 | + } |
|
102 | + trigger_error(sprintf('%s::__toString exception: %s', self::class, (string) $e), E_USER_ERROR); |
|
103 | + |
|
104 | + return ''; |
|
105 | + } |
|
106 | + } |
|
107 | + |
|
108 | + public function close(): void |
|
109 | + { |
|
110 | + ($this->_fn_close)(); |
|
111 | + } |
|
112 | + |
|
113 | + public function detach() |
|
114 | + { |
|
115 | + return ($this->_fn_detach)(); |
|
116 | + } |
|
117 | + |
|
118 | + public function getSize(): ?int |
|
119 | + { |
|
120 | + return ($this->_fn_getSize)(); |
|
121 | + } |
|
122 | + |
|
123 | + public function tell(): int |
|
124 | + { |
|
125 | + return ($this->_fn_tell)(); |
|
126 | + } |
|
127 | + |
|
128 | + public function eof(): bool |
|
129 | + { |
|
130 | + return ($this->_fn_eof)(); |
|
131 | + } |
|
132 | + |
|
133 | + public function isSeekable(): bool |
|
134 | + { |
|
135 | + return ($this->_fn_isSeekable)(); |
|
136 | + } |
|
137 | + |
|
138 | + public function rewind(): void |
|
139 | + { |
|
140 | + ($this->_fn_rewind)(); |
|
141 | + } |
|
142 | + |
|
143 | + public function seek($offset, $whence = SEEK_SET): void |
|
144 | + { |
|
145 | + ($this->_fn_seek)($offset, $whence); |
|
146 | + } |
|
147 | + |
|
148 | + public function isWritable(): bool |
|
149 | + { |
|
150 | + return ($this->_fn_isWritable)(); |
|
151 | + } |
|
152 | + |
|
153 | + public function write($string): int |
|
154 | + { |
|
155 | + return ($this->_fn_write)($string); |
|
156 | + } |
|
157 | + |
|
158 | + public function isReadable(): bool |
|
159 | + { |
|
160 | + return ($this->_fn_isReadable)(); |
|
161 | + } |
|
162 | + |
|
163 | + public function read($length): string |
|
164 | + { |
|
165 | + return ($this->_fn_read)($length); |
|
166 | + } |
|
167 | + |
|
168 | + public function getContents(): string |
|
169 | + { |
|
170 | + return ($this->_fn_getContents)(); |
|
171 | + } |
|
172 | + |
|
173 | + /** |
|
174 | + * @return mixed |
|
175 | + */ |
|
176 | + public function getMetadata($key = null) |
|
177 | + { |
|
178 | + return ($this->_fn_getMetadata)($key); |
|
179 | + } |
|
180 | 180 | } |
@@ -99,7 +99,7 @@ |
||
99 | 99 | if (\PHP_VERSION_ID >= 70400) { |
100 | 100 | throw $e; |
101 | 101 | } |
102 | - trigger_error(sprintf('%s::__toString exception: %s', self::class, (string) $e), E_USER_ERROR); |
|
102 | + trigger_error(sprintf('%s::__toString exception: %s', self::class, (string)$e), E_USER_ERROR); |
|
103 | 103 | |
104 | 104 | return ''; |
105 | 105 | } |
@@ -13,8 +13,7 @@ |
||
13 | 13 | * to create a concrete class for a simple extension point. |
14 | 14 | */ |
15 | 15 | #[\AllowDynamicProperties] |
16 | -final class FnStream implements StreamInterface |
|
17 | -{ |
|
16 | +final class FnStream implements StreamInterface { |
|
18 | 17 | private const SLOTS = [ |
19 | 18 | '__toString', 'close', 'detach', 'rewind', |
20 | 19 | 'getSize', 'tell', 'eof', 'isSeekable', 'seek', 'isWritable', 'write', |
@@ -16,132 +16,132 @@ |
||
16 | 16 | */ |
17 | 17 | final class BufferStream implements StreamInterface |
18 | 18 | { |
19 | - /** @var int */ |
|
20 | - private $hwm; |
|
21 | - |
|
22 | - /** @var string */ |
|
23 | - private $buffer = ''; |
|
24 | - |
|
25 | - /** |
|
26 | - * @param int $hwm High water mark, representing the preferred maximum |
|
27 | - * buffer size. If the size of the buffer exceeds the high |
|
28 | - * water mark, then calls to write will continue to succeed |
|
29 | - * but will return 0 to inform writers to slow down |
|
30 | - * until the buffer has been drained by reading from it. |
|
31 | - */ |
|
32 | - public function __construct(int $hwm = 16384) |
|
33 | - { |
|
34 | - $this->hwm = $hwm; |
|
35 | - } |
|
36 | - |
|
37 | - public function __toString(): string |
|
38 | - { |
|
39 | - return $this->getContents(); |
|
40 | - } |
|
41 | - |
|
42 | - public function getContents(): string |
|
43 | - { |
|
44 | - $buffer = $this->buffer; |
|
45 | - $this->buffer = ''; |
|
46 | - |
|
47 | - return $buffer; |
|
48 | - } |
|
49 | - |
|
50 | - public function close(): void |
|
51 | - { |
|
52 | - $this->buffer = ''; |
|
53 | - } |
|
54 | - |
|
55 | - public function detach() |
|
56 | - { |
|
57 | - $this->close(); |
|
58 | - |
|
59 | - return null; |
|
60 | - } |
|
61 | - |
|
62 | - public function getSize(): ?int |
|
63 | - { |
|
64 | - return strlen($this->buffer); |
|
65 | - } |
|
66 | - |
|
67 | - public function isReadable(): bool |
|
68 | - { |
|
69 | - return true; |
|
70 | - } |
|
71 | - |
|
72 | - public function isWritable(): bool |
|
73 | - { |
|
74 | - return true; |
|
75 | - } |
|
76 | - |
|
77 | - public function isSeekable(): bool |
|
78 | - { |
|
79 | - return false; |
|
80 | - } |
|
81 | - |
|
82 | - public function rewind(): void |
|
83 | - { |
|
84 | - $this->seek(0); |
|
85 | - } |
|
86 | - |
|
87 | - public function seek($offset, $whence = SEEK_SET): void |
|
88 | - { |
|
89 | - throw new \RuntimeException('Cannot seek a BufferStream'); |
|
90 | - } |
|
91 | - |
|
92 | - public function eof(): bool |
|
93 | - { |
|
94 | - return strlen($this->buffer) === 0; |
|
95 | - } |
|
96 | - |
|
97 | - public function tell(): int |
|
98 | - { |
|
99 | - throw new \RuntimeException('Cannot determine the position of a BufferStream'); |
|
100 | - } |
|
101 | - |
|
102 | - /** |
|
103 | - * Reads data from the buffer. |
|
104 | - */ |
|
105 | - public function read($length): string |
|
106 | - { |
|
107 | - $currentLength = strlen($this->buffer); |
|
108 | - |
|
109 | - if ($length >= $currentLength) { |
|
110 | - // No need to slice the buffer because we don't have enough data. |
|
111 | - $result = $this->buffer; |
|
112 | - $this->buffer = ''; |
|
113 | - } else { |
|
114 | - // Slice up the result to provide a subset of the buffer. |
|
115 | - $result = substr($this->buffer, 0, $length); |
|
116 | - $this->buffer = substr($this->buffer, $length); |
|
117 | - } |
|
118 | - |
|
119 | - return $result; |
|
120 | - } |
|
121 | - |
|
122 | - /** |
|
123 | - * Writes data to the buffer. |
|
124 | - */ |
|
125 | - public function write($string): int |
|
126 | - { |
|
127 | - $this->buffer .= $string; |
|
128 | - |
|
129 | - if (strlen($this->buffer) >= $this->hwm) { |
|
130 | - return 0; |
|
131 | - } |
|
132 | - |
|
133 | - return strlen($string); |
|
134 | - } |
|
135 | - |
|
136 | - /** |
|
137 | - * @return mixed |
|
138 | - */ |
|
139 | - public function getMetadata($key = null) |
|
140 | - { |
|
141 | - if ($key === 'hwm') { |
|
142 | - return $this->hwm; |
|
143 | - } |
|
144 | - |
|
145 | - return $key ? null : []; |
|
146 | - } |
|
19 | + /** @var int */ |
|
20 | + private $hwm; |
|
21 | + |
|
22 | + /** @var string */ |
|
23 | + private $buffer = ''; |
|
24 | + |
|
25 | + /** |
|
26 | + * @param int $hwm High water mark, representing the preferred maximum |
|
27 | + * buffer size. If the size of the buffer exceeds the high |
|
28 | + * water mark, then calls to write will continue to succeed |
|
29 | + * but will return 0 to inform writers to slow down |
|
30 | + * until the buffer has been drained by reading from it. |
|
31 | + */ |
|
32 | + public function __construct(int $hwm = 16384) |
|
33 | + { |
|
34 | + $this->hwm = $hwm; |
|
35 | + } |
|
36 | + |
|
37 | + public function __toString(): string |
|
38 | + { |
|
39 | + return $this->getContents(); |
|
40 | + } |
|
41 | + |
|
42 | + public function getContents(): string |
|
43 | + { |
|
44 | + $buffer = $this->buffer; |
|
45 | + $this->buffer = ''; |
|
46 | + |
|
47 | + return $buffer; |
|
48 | + } |
|
49 | + |
|
50 | + public function close(): void |
|
51 | + { |
|
52 | + $this->buffer = ''; |
|
53 | + } |
|
54 | + |
|
55 | + public function detach() |
|
56 | + { |
|
57 | + $this->close(); |
|
58 | + |
|
59 | + return null; |
|
60 | + } |
|
61 | + |
|
62 | + public function getSize(): ?int |
|
63 | + { |
|
64 | + return strlen($this->buffer); |
|
65 | + } |
|
66 | + |
|
67 | + public function isReadable(): bool |
|
68 | + { |
|
69 | + return true; |
|
70 | + } |
|
71 | + |
|
72 | + public function isWritable(): bool |
|
73 | + { |
|
74 | + return true; |
|
75 | + } |
|
76 | + |
|
77 | + public function isSeekable(): bool |
|
78 | + { |
|
79 | + return false; |
|
80 | + } |
|
81 | + |
|
82 | + public function rewind(): void |
|
83 | + { |
|
84 | + $this->seek(0); |
|
85 | + } |
|
86 | + |
|
87 | + public function seek($offset, $whence = SEEK_SET): void |
|
88 | + { |
|
89 | + throw new \RuntimeException('Cannot seek a BufferStream'); |
|
90 | + } |
|
91 | + |
|
92 | + public function eof(): bool |
|
93 | + { |
|
94 | + return strlen($this->buffer) === 0; |
|
95 | + } |
|
96 | + |
|
97 | + public function tell(): int |
|
98 | + { |
|
99 | + throw new \RuntimeException('Cannot determine the position of a BufferStream'); |
|
100 | + } |
|
101 | + |
|
102 | + /** |
|
103 | + * Reads data from the buffer. |
|
104 | + */ |
|
105 | + public function read($length): string |
|
106 | + { |
|
107 | + $currentLength = strlen($this->buffer); |
|
108 | + |
|
109 | + if ($length >= $currentLength) { |
|
110 | + // No need to slice the buffer because we don't have enough data. |
|
111 | + $result = $this->buffer; |
|
112 | + $this->buffer = ''; |
|
113 | + } else { |
|
114 | + // Slice up the result to provide a subset of the buffer. |
|
115 | + $result = substr($this->buffer, 0, $length); |
|
116 | + $this->buffer = substr($this->buffer, $length); |
|
117 | + } |
|
118 | + |
|
119 | + return $result; |
|
120 | + } |
|
121 | + |
|
122 | + /** |
|
123 | + * Writes data to the buffer. |
|
124 | + */ |
|
125 | + public function write($string): int |
|
126 | + { |
|
127 | + $this->buffer .= $string; |
|
128 | + |
|
129 | + if (strlen($this->buffer) >= $this->hwm) { |
|
130 | + return 0; |
|
131 | + } |
|
132 | + |
|
133 | + return strlen($string); |
|
134 | + } |
|
135 | + |
|
136 | + /** |
|
137 | + * @return mixed |
|
138 | + */ |
|
139 | + public function getMetadata($key = null) |
|
140 | + { |
|
141 | + if ($key === 'hwm') { |
|
142 | + return $this->hwm; |
|
143 | + } |
|
144 | + |
|
145 | + return $key ? null : []; |
|
146 | + } |
|
147 | 147 | } |
@@ -14,8 +14,7 @@ |
||
14 | 14 | * what the configured high water mark of the stream is, or the maximum |
15 | 15 | * preferred size of the buffer. |
16 | 16 | */ |
17 | -final class BufferStream implements StreamInterface |
|
18 | -{ |
|
17 | +final class BufferStream implements StreamInterface { |
|
19 | 18 | /** @var int */ |
20 | 19 | private $hwm; |
21 | 20 |