@@ -10,298 +10,298 @@ |
||
10 | 10 | */ |
11 | 11 | class CookieJar implements CookieJarInterface |
12 | 12 | { |
13 | - /** |
|
14 | - * @var SetCookie[] Loaded cookie data |
|
15 | - */ |
|
16 | - private $cookies = []; |
|
17 | - |
|
18 | - /** |
|
19 | - * @var bool |
|
20 | - */ |
|
21 | - private $strictMode; |
|
22 | - |
|
23 | - /** |
|
24 | - * @param bool $strictMode Set to true to throw exceptions when invalid |
|
25 | - * cookies are added to the cookie jar. |
|
26 | - * @param array $cookieArray Array of SetCookie objects or a hash of |
|
27 | - * arrays that can be used with the SetCookie |
|
28 | - * constructor |
|
29 | - */ |
|
30 | - public function __construct(bool $strictMode = false, array $cookieArray = []) |
|
31 | - { |
|
32 | - $this->strictMode = $strictMode; |
|
33 | - |
|
34 | - foreach ($cookieArray as $cookie) { |
|
35 | - if (!($cookie instanceof SetCookie)) { |
|
36 | - $cookie = new SetCookie($cookie); |
|
37 | - } |
|
38 | - $this->setCookie($cookie); |
|
39 | - } |
|
40 | - } |
|
41 | - |
|
42 | - /** |
|
43 | - * Create a new Cookie jar from an associative array and domain. |
|
44 | - * |
|
45 | - * @param array $cookies Cookies to create the jar from |
|
46 | - * @param string $domain Domain to set the cookies to |
|
47 | - */ |
|
48 | - public static function fromArray(array $cookies, string $domain): self |
|
49 | - { |
|
50 | - $cookieJar = new self(); |
|
51 | - foreach ($cookies as $name => $value) { |
|
52 | - $cookieJar->setCookie(new SetCookie([ |
|
53 | - 'Domain' => $domain, |
|
54 | - 'Name' => $name, |
|
55 | - 'Value' => $value, |
|
56 | - 'Discard' => true, |
|
57 | - ])); |
|
58 | - } |
|
59 | - |
|
60 | - return $cookieJar; |
|
61 | - } |
|
62 | - |
|
63 | - /** |
|
64 | - * Evaluate if this cookie should be persisted to storage |
|
65 | - * that survives between requests. |
|
66 | - * |
|
67 | - * @param SetCookie $cookie Being evaluated. |
|
68 | - * @param bool $allowSessionCookies If we should persist session cookies |
|
69 | - */ |
|
70 | - public static function shouldPersist(SetCookie $cookie, bool $allowSessionCookies = false): bool |
|
71 | - { |
|
72 | - if ($cookie->getExpires() || $allowSessionCookies) { |
|
73 | - if (!$cookie->getDiscard()) { |
|
74 | - return true; |
|
75 | - } |
|
76 | - } |
|
77 | - |
|
78 | - return false; |
|
79 | - } |
|
80 | - |
|
81 | - /** |
|
82 | - * Finds and returns the cookie based on the name |
|
83 | - * |
|
84 | - * @param string $name cookie name to search for |
|
85 | - * |
|
86 | - * @return SetCookie|null cookie that was found or null if not found |
|
87 | - */ |
|
88 | - public function getCookieByName(string $name): ?SetCookie |
|
89 | - { |
|
90 | - foreach ($this->cookies as $cookie) { |
|
91 | - if ($cookie->getName() !== null && \strcasecmp($cookie->getName(), $name) === 0) { |
|
92 | - return $cookie; |
|
93 | - } |
|
94 | - } |
|
95 | - |
|
96 | - return null; |
|
97 | - } |
|
98 | - |
|
99 | - public function toArray(): array |
|
100 | - { |
|
101 | - return \array_map(static function (SetCookie $cookie): array { |
|
102 | - return $cookie->toArray(); |
|
103 | - }, $this->getIterator()->getArrayCopy()); |
|
104 | - } |
|
105 | - |
|
106 | - public function clear(string $domain = null, string $path = null, string $name = null): void |
|
107 | - { |
|
108 | - if (!$domain) { |
|
109 | - $this->cookies = []; |
|
110 | - |
|
111 | - return; |
|
112 | - } elseif (!$path) { |
|
113 | - $this->cookies = \array_filter( |
|
114 | - $this->cookies, |
|
115 | - static function (SetCookie $cookie) use ($domain): bool { |
|
116 | - return !$cookie->matchesDomain($domain); |
|
117 | - } |
|
118 | - ); |
|
119 | - } elseif (!$name) { |
|
120 | - $this->cookies = \array_filter( |
|
121 | - $this->cookies, |
|
122 | - static function (SetCookie $cookie) use ($path, $domain): bool { |
|
123 | - return !($cookie->matchesPath($path) |
|
124 | - && $cookie->matchesDomain($domain)); |
|
125 | - } |
|
126 | - ); |
|
127 | - } else { |
|
128 | - $this->cookies = \array_filter( |
|
129 | - $this->cookies, |
|
130 | - static function (SetCookie $cookie) use ($path, $domain, $name) { |
|
131 | - return !($cookie->getName() == $name |
|
132 | - && $cookie->matchesPath($path) |
|
133 | - && $cookie->matchesDomain($domain)); |
|
134 | - } |
|
135 | - ); |
|
136 | - } |
|
137 | - } |
|
138 | - |
|
139 | - public function clearSessionCookies(): void |
|
140 | - { |
|
141 | - $this->cookies = \array_filter( |
|
142 | - $this->cookies, |
|
143 | - static function (SetCookie $cookie): bool { |
|
144 | - return !$cookie->getDiscard() && $cookie->getExpires(); |
|
145 | - } |
|
146 | - ); |
|
147 | - } |
|
148 | - |
|
149 | - public function setCookie(SetCookie $cookie): bool |
|
150 | - { |
|
151 | - // If the name string is empty (but not 0), ignore the set-cookie |
|
152 | - // string entirely. |
|
153 | - $name = $cookie->getName(); |
|
154 | - if (!$name && $name !== '0') { |
|
155 | - return false; |
|
156 | - } |
|
157 | - |
|
158 | - // Only allow cookies with set and valid domain, name, value |
|
159 | - $result = $cookie->validate(); |
|
160 | - if ($result !== true) { |
|
161 | - if ($this->strictMode) { |
|
162 | - throw new \RuntimeException('Invalid cookie: '.$result); |
|
163 | - } |
|
164 | - $this->removeCookieIfEmpty($cookie); |
|
165 | - |
|
166 | - return false; |
|
167 | - } |
|
168 | - |
|
169 | - // Resolve conflicts with previously set cookies |
|
170 | - foreach ($this->cookies as $i => $c) { |
|
171 | - // Two cookies are identical, when their path, and domain are |
|
172 | - // identical. |
|
173 | - if ($c->getPath() != $cookie->getPath() |
|
174 | - || $c->getDomain() != $cookie->getDomain() |
|
175 | - || $c->getName() != $cookie->getName() |
|
176 | - ) { |
|
177 | - continue; |
|
178 | - } |
|
179 | - |
|
180 | - // The previously set cookie is a discard cookie and this one is |
|
181 | - // not so allow the new cookie to be set |
|
182 | - if (!$cookie->getDiscard() && $c->getDiscard()) { |
|
183 | - unset($this->cookies[$i]); |
|
184 | - continue; |
|
185 | - } |
|
186 | - |
|
187 | - // If the new cookie's expiration is further into the future, then |
|
188 | - // replace the old cookie |
|
189 | - if ($cookie->getExpires() > $c->getExpires()) { |
|
190 | - unset($this->cookies[$i]); |
|
191 | - continue; |
|
192 | - } |
|
193 | - |
|
194 | - // If the value has changed, we better change it |
|
195 | - if ($cookie->getValue() !== $c->getValue()) { |
|
196 | - unset($this->cookies[$i]); |
|
197 | - continue; |
|
198 | - } |
|
199 | - |
|
200 | - // The cookie exists, so no need to continue |
|
201 | - return false; |
|
202 | - } |
|
203 | - |
|
204 | - $this->cookies[] = $cookie; |
|
205 | - |
|
206 | - return true; |
|
207 | - } |
|
208 | - |
|
209 | - public function count(): int |
|
210 | - { |
|
211 | - return \count($this->cookies); |
|
212 | - } |
|
213 | - |
|
214 | - /** |
|
215 | - * @return \ArrayIterator<int, SetCookie> |
|
216 | - */ |
|
217 | - public function getIterator(): \ArrayIterator |
|
218 | - { |
|
219 | - return new \ArrayIterator(\array_values($this->cookies)); |
|
220 | - } |
|
221 | - |
|
222 | - public function extractCookies(RequestInterface $request, ResponseInterface $response): void |
|
223 | - { |
|
224 | - if ($cookieHeader = $response->getHeader('Set-Cookie')) { |
|
225 | - foreach ($cookieHeader as $cookie) { |
|
226 | - $sc = SetCookie::fromString($cookie); |
|
227 | - if (!$sc->getDomain()) { |
|
228 | - $sc->setDomain($request->getUri()->getHost()); |
|
229 | - } |
|
230 | - if (0 !== \strpos($sc->getPath(), '/')) { |
|
231 | - $sc->setPath($this->getCookiePathFromRequest($request)); |
|
232 | - } |
|
233 | - if (!$sc->matchesDomain($request->getUri()->getHost())) { |
|
234 | - continue; |
|
235 | - } |
|
236 | - // Note: At this point `$sc->getDomain()` being a public suffix should |
|
237 | - // be rejected, but we don't want to pull in the full PSL dependency. |
|
238 | - $this->setCookie($sc); |
|
239 | - } |
|
240 | - } |
|
241 | - } |
|
242 | - |
|
243 | - /** |
|
244 | - * Computes cookie path following RFC 6265 section 5.1.4 |
|
245 | - * |
|
246 | - * @see https://datatracker.ietf.org/doc/html/rfc6265#section-5.1.4 |
|
247 | - */ |
|
248 | - private function getCookiePathFromRequest(RequestInterface $request): string |
|
249 | - { |
|
250 | - $uriPath = $request->getUri()->getPath(); |
|
251 | - if ('' === $uriPath) { |
|
252 | - return '/'; |
|
253 | - } |
|
254 | - if (0 !== \strpos($uriPath, '/')) { |
|
255 | - return '/'; |
|
256 | - } |
|
257 | - if ('/' === $uriPath) { |
|
258 | - return '/'; |
|
259 | - } |
|
260 | - $lastSlashPos = \strrpos($uriPath, '/'); |
|
261 | - if (0 === $lastSlashPos || false === $lastSlashPos) { |
|
262 | - return '/'; |
|
263 | - } |
|
264 | - |
|
265 | - return \substr($uriPath, 0, $lastSlashPos); |
|
266 | - } |
|
267 | - |
|
268 | - public function withCookieHeader(RequestInterface $request): RequestInterface |
|
269 | - { |
|
270 | - $values = []; |
|
271 | - $uri = $request->getUri(); |
|
272 | - $scheme = $uri->getScheme(); |
|
273 | - $host = $uri->getHost(); |
|
274 | - $path = $uri->getPath() ?: '/'; |
|
275 | - |
|
276 | - foreach ($this->cookies as $cookie) { |
|
277 | - if ($cookie->matchesPath($path) |
|
278 | - && $cookie->matchesDomain($host) |
|
279 | - && !$cookie->isExpired() |
|
280 | - && (!$cookie->getSecure() || $scheme === 'https') |
|
281 | - ) { |
|
282 | - $values[] = $cookie->getName().'=' |
|
283 | - .$cookie->getValue(); |
|
284 | - } |
|
285 | - } |
|
286 | - |
|
287 | - return $values |
|
288 | - ? $request->withHeader('Cookie', \implode('; ', $values)) |
|
289 | - : $request; |
|
290 | - } |
|
291 | - |
|
292 | - /** |
|
293 | - * If a cookie already exists and the server asks to set it again with a |
|
294 | - * null value, the cookie must be deleted. |
|
295 | - */ |
|
296 | - private function removeCookieIfEmpty(SetCookie $cookie): void |
|
297 | - { |
|
298 | - $cookieValue = $cookie->getValue(); |
|
299 | - if ($cookieValue === null || $cookieValue === '') { |
|
300 | - $this->clear( |
|
301 | - $cookie->getDomain(), |
|
302 | - $cookie->getPath(), |
|
303 | - $cookie->getName() |
|
304 | - ); |
|
305 | - } |
|
306 | - } |
|
13 | + /** |
|
14 | + * @var SetCookie[] Loaded cookie data |
|
15 | + */ |
|
16 | + private $cookies = []; |
|
17 | + |
|
18 | + /** |
|
19 | + * @var bool |
|
20 | + */ |
|
21 | + private $strictMode; |
|
22 | + |
|
23 | + /** |
|
24 | + * @param bool $strictMode Set to true to throw exceptions when invalid |
|
25 | + * cookies are added to the cookie jar. |
|
26 | + * @param array $cookieArray Array of SetCookie objects or a hash of |
|
27 | + * arrays that can be used with the SetCookie |
|
28 | + * constructor |
|
29 | + */ |
|
30 | + public function __construct(bool $strictMode = false, array $cookieArray = []) |
|
31 | + { |
|
32 | + $this->strictMode = $strictMode; |
|
33 | + |
|
34 | + foreach ($cookieArray as $cookie) { |
|
35 | + if (!($cookie instanceof SetCookie)) { |
|
36 | + $cookie = new SetCookie($cookie); |
|
37 | + } |
|
38 | + $this->setCookie($cookie); |
|
39 | + } |
|
40 | + } |
|
41 | + |
|
42 | + /** |
|
43 | + * Create a new Cookie jar from an associative array and domain. |
|
44 | + * |
|
45 | + * @param array $cookies Cookies to create the jar from |
|
46 | + * @param string $domain Domain to set the cookies to |
|
47 | + */ |
|
48 | + public static function fromArray(array $cookies, string $domain): self |
|
49 | + { |
|
50 | + $cookieJar = new self(); |
|
51 | + foreach ($cookies as $name => $value) { |
|
52 | + $cookieJar->setCookie(new SetCookie([ |
|
53 | + 'Domain' => $domain, |
|
54 | + 'Name' => $name, |
|
55 | + 'Value' => $value, |
|
56 | + 'Discard' => true, |
|
57 | + ])); |
|
58 | + } |
|
59 | + |
|
60 | + return $cookieJar; |
|
61 | + } |
|
62 | + |
|
63 | + /** |
|
64 | + * Evaluate if this cookie should be persisted to storage |
|
65 | + * that survives between requests. |
|
66 | + * |
|
67 | + * @param SetCookie $cookie Being evaluated. |
|
68 | + * @param bool $allowSessionCookies If we should persist session cookies |
|
69 | + */ |
|
70 | + public static function shouldPersist(SetCookie $cookie, bool $allowSessionCookies = false): bool |
|
71 | + { |
|
72 | + if ($cookie->getExpires() || $allowSessionCookies) { |
|
73 | + if (!$cookie->getDiscard()) { |
|
74 | + return true; |
|
75 | + } |
|
76 | + } |
|
77 | + |
|
78 | + return false; |
|
79 | + } |
|
80 | + |
|
81 | + /** |
|
82 | + * Finds and returns the cookie based on the name |
|
83 | + * |
|
84 | + * @param string $name cookie name to search for |
|
85 | + * |
|
86 | + * @return SetCookie|null cookie that was found or null if not found |
|
87 | + */ |
|
88 | + public function getCookieByName(string $name): ?SetCookie |
|
89 | + { |
|
90 | + foreach ($this->cookies as $cookie) { |
|
91 | + if ($cookie->getName() !== null && \strcasecmp($cookie->getName(), $name) === 0) { |
|
92 | + return $cookie; |
|
93 | + } |
|
94 | + } |
|
95 | + |
|
96 | + return null; |
|
97 | + } |
|
98 | + |
|
99 | + public function toArray(): array |
|
100 | + { |
|
101 | + return \array_map(static function (SetCookie $cookie): array { |
|
102 | + return $cookie->toArray(); |
|
103 | + }, $this->getIterator()->getArrayCopy()); |
|
104 | + } |
|
105 | + |
|
106 | + public function clear(string $domain = null, string $path = null, string $name = null): void |
|
107 | + { |
|
108 | + if (!$domain) { |
|
109 | + $this->cookies = []; |
|
110 | + |
|
111 | + return; |
|
112 | + } elseif (!$path) { |
|
113 | + $this->cookies = \array_filter( |
|
114 | + $this->cookies, |
|
115 | + static function (SetCookie $cookie) use ($domain): bool { |
|
116 | + return !$cookie->matchesDomain($domain); |
|
117 | + } |
|
118 | + ); |
|
119 | + } elseif (!$name) { |
|
120 | + $this->cookies = \array_filter( |
|
121 | + $this->cookies, |
|
122 | + static function (SetCookie $cookie) use ($path, $domain): bool { |
|
123 | + return !($cookie->matchesPath($path) |
|
124 | + && $cookie->matchesDomain($domain)); |
|
125 | + } |
|
126 | + ); |
|
127 | + } else { |
|
128 | + $this->cookies = \array_filter( |
|
129 | + $this->cookies, |
|
130 | + static function (SetCookie $cookie) use ($path, $domain, $name) { |
|
131 | + return !($cookie->getName() == $name |
|
132 | + && $cookie->matchesPath($path) |
|
133 | + && $cookie->matchesDomain($domain)); |
|
134 | + } |
|
135 | + ); |
|
136 | + } |
|
137 | + } |
|
138 | + |
|
139 | + public function clearSessionCookies(): void |
|
140 | + { |
|
141 | + $this->cookies = \array_filter( |
|
142 | + $this->cookies, |
|
143 | + static function (SetCookie $cookie): bool { |
|
144 | + return !$cookie->getDiscard() && $cookie->getExpires(); |
|
145 | + } |
|
146 | + ); |
|
147 | + } |
|
148 | + |
|
149 | + public function setCookie(SetCookie $cookie): bool |
|
150 | + { |
|
151 | + // If the name string is empty (but not 0), ignore the set-cookie |
|
152 | + // string entirely. |
|
153 | + $name = $cookie->getName(); |
|
154 | + if (!$name && $name !== '0') { |
|
155 | + return false; |
|
156 | + } |
|
157 | + |
|
158 | + // Only allow cookies with set and valid domain, name, value |
|
159 | + $result = $cookie->validate(); |
|
160 | + if ($result !== true) { |
|
161 | + if ($this->strictMode) { |
|
162 | + throw new \RuntimeException('Invalid cookie: '.$result); |
|
163 | + } |
|
164 | + $this->removeCookieIfEmpty($cookie); |
|
165 | + |
|
166 | + return false; |
|
167 | + } |
|
168 | + |
|
169 | + // Resolve conflicts with previously set cookies |
|
170 | + foreach ($this->cookies as $i => $c) { |
|
171 | + // Two cookies are identical, when their path, and domain are |
|
172 | + // identical. |
|
173 | + if ($c->getPath() != $cookie->getPath() |
|
174 | + || $c->getDomain() != $cookie->getDomain() |
|
175 | + || $c->getName() != $cookie->getName() |
|
176 | + ) { |
|
177 | + continue; |
|
178 | + } |
|
179 | + |
|
180 | + // The previously set cookie is a discard cookie and this one is |
|
181 | + // not so allow the new cookie to be set |
|
182 | + if (!$cookie->getDiscard() && $c->getDiscard()) { |
|
183 | + unset($this->cookies[$i]); |
|
184 | + continue; |
|
185 | + } |
|
186 | + |
|
187 | + // If the new cookie's expiration is further into the future, then |
|
188 | + // replace the old cookie |
|
189 | + if ($cookie->getExpires() > $c->getExpires()) { |
|
190 | + unset($this->cookies[$i]); |
|
191 | + continue; |
|
192 | + } |
|
193 | + |
|
194 | + // If the value has changed, we better change it |
|
195 | + if ($cookie->getValue() !== $c->getValue()) { |
|
196 | + unset($this->cookies[$i]); |
|
197 | + continue; |
|
198 | + } |
|
199 | + |
|
200 | + // The cookie exists, so no need to continue |
|
201 | + return false; |
|
202 | + } |
|
203 | + |
|
204 | + $this->cookies[] = $cookie; |
|
205 | + |
|
206 | + return true; |
|
207 | + } |
|
208 | + |
|
209 | + public function count(): int |
|
210 | + { |
|
211 | + return \count($this->cookies); |
|
212 | + } |
|
213 | + |
|
214 | + /** |
|
215 | + * @return \ArrayIterator<int, SetCookie> |
|
216 | + */ |
|
217 | + public function getIterator(): \ArrayIterator |
|
218 | + { |
|
219 | + return new \ArrayIterator(\array_values($this->cookies)); |
|
220 | + } |
|
221 | + |
|
222 | + public function extractCookies(RequestInterface $request, ResponseInterface $response): void |
|
223 | + { |
|
224 | + if ($cookieHeader = $response->getHeader('Set-Cookie')) { |
|
225 | + foreach ($cookieHeader as $cookie) { |
|
226 | + $sc = SetCookie::fromString($cookie); |
|
227 | + if (!$sc->getDomain()) { |
|
228 | + $sc->setDomain($request->getUri()->getHost()); |
|
229 | + } |
|
230 | + if (0 !== \strpos($sc->getPath(), '/')) { |
|
231 | + $sc->setPath($this->getCookiePathFromRequest($request)); |
|
232 | + } |
|
233 | + if (!$sc->matchesDomain($request->getUri()->getHost())) { |
|
234 | + continue; |
|
235 | + } |
|
236 | + // Note: At this point `$sc->getDomain()` being a public suffix should |
|
237 | + // be rejected, but we don't want to pull in the full PSL dependency. |
|
238 | + $this->setCookie($sc); |
|
239 | + } |
|
240 | + } |
|
241 | + } |
|
242 | + |
|
243 | + /** |
|
244 | + * Computes cookie path following RFC 6265 section 5.1.4 |
|
245 | + * |
|
246 | + * @see https://datatracker.ietf.org/doc/html/rfc6265#section-5.1.4 |
|
247 | + */ |
|
248 | + private function getCookiePathFromRequest(RequestInterface $request): string |
|
249 | + { |
|
250 | + $uriPath = $request->getUri()->getPath(); |
|
251 | + if ('' === $uriPath) { |
|
252 | + return '/'; |
|
253 | + } |
|
254 | + if (0 !== \strpos($uriPath, '/')) { |
|
255 | + return '/'; |
|
256 | + } |
|
257 | + if ('/' === $uriPath) { |
|
258 | + return '/'; |
|
259 | + } |
|
260 | + $lastSlashPos = \strrpos($uriPath, '/'); |
|
261 | + if (0 === $lastSlashPos || false === $lastSlashPos) { |
|
262 | + return '/'; |
|
263 | + } |
|
264 | + |
|
265 | + return \substr($uriPath, 0, $lastSlashPos); |
|
266 | + } |
|
267 | + |
|
268 | + public function withCookieHeader(RequestInterface $request): RequestInterface |
|
269 | + { |
|
270 | + $values = []; |
|
271 | + $uri = $request->getUri(); |
|
272 | + $scheme = $uri->getScheme(); |
|
273 | + $host = $uri->getHost(); |
|
274 | + $path = $uri->getPath() ?: '/'; |
|
275 | + |
|
276 | + foreach ($this->cookies as $cookie) { |
|
277 | + if ($cookie->matchesPath($path) |
|
278 | + && $cookie->matchesDomain($host) |
|
279 | + && !$cookie->isExpired() |
|
280 | + && (!$cookie->getSecure() || $scheme === 'https') |
|
281 | + ) { |
|
282 | + $values[] = $cookie->getName().'=' |
|
283 | + .$cookie->getValue(); |
|
284 | + } |
|
285 | + } |
|
286 | + |
|
287 | + return $values |
|
288 | + ? $request->withHeader('Cookie', \implode('; ', $values)) |
|
289 | + : $request; |
|
290 | + } |
|
291 | + |
|
292 | + /** |
|
293 | + * If a cookie already exists and the server asks to set it again with a |
|
294 | + * null value, the cookie must be deleted. |
|
295 | + */ |
|
296 | + private function removeCookieIfEmpty(SetCookie $cookie): void |
|
297 | + { |
|
298 | + $cookieValue = $cookie->getValue(); |
|
299 | + if ($cookieValue === null || $cookieValue === '') { |
|
300 | + $this->clear( |
|
301 | + $cookie->getDomain(), |
|
302 | + $cookie->getPath(), |
|
303 | + $cookie->getName() |
|
304 | + ); |
|
305 | + } |
|
306 | + } |
|
307 | 307 | } |
@@ -98,7 +98,7 @@ discard block |
||
98 | 98 | |
99 | 99 | public function toArray(): array |
100 | 100 | { |
101 | - return \array_map(static function (SetCookie $cookie): array { |
|
101 | + return \array_map(static function(SetCookie $cookie): array { |
|
102 | 102 | return $cookie->toArray(); |
103 | 103 | }, $this->getIterator()->getArrayCopy()); |
104 | 104 | } |
@@ -112,14 +112,14 @@ discard block |
||
112 | 112 | } elseif (!$path) { |
113 | 113 | $this->cookies = \array_filter( |
114 | 114 | $this->cookies, |
115 | - static function (SetCookie $cookie) use ($domain): bool { |
|
115 | + static function(SetCookie $cookie) use ($domain): bool { |
|
116 | 116 | return !$cookie->matchesDomain($domain); |
117 | 117 | } |
118 | 118 | ); |
119 | 119 | } elseif (!$name) { |
120 | 120 | $this->cookies = \array_filter( |
121 | 121 | $this->cookies, |
122 | - static function (SetCookie $cookie) use ($path, $domain): bool { |
|
122 | + static function(SetCookie $cookie) use ($path, $domain): bool { |
|
123 | 123 | return !($cookie->matchesPath($path) |
124 | 124 | && $cookie->matchesDomain($domain)); |
125 | 125 | } |
@@ -127,7 +127,7 @@ discard block |
||
127 | 127 | } else { |
128 | 128 | $this->cookies = \array_filter( |
129 | 129 | $this->cookies, |
130 | - static function (SetCookie $cookie) use ($path, $domain, $name) { |
|
130 | + static function(SetCookie $cookie) use ($path, $domain, $name) { |
|
131 | 131 | return !($cookie->getName() == $name |
132 | 132 | && $cookie->matchesPath($path) |
133 | 133 | && $cookie->matchesDomain($domain)); |
@@ -140,7 +140,7 @@ discard block |
||
140 | 140 | { |
141 | 141 | $this->cookies = \array_filter( |
142 | 142 | $this->cookies, |
143 | - static function (SetCookie $cookie): bool { |
|
143 | + static function(SetCookie $cookie): bool { |
|
144 | 144 | return !$cookie->getDiscard() && $cookie->getExpires(); |
145 | 145 | } |
146 | 146 | ); |
@@ -8,8 +8,7 @@ |
||
8 | 8 | /** |
9 | 9 | * Cookie jar that stores cookies as an array |
10 | 10 | */ |
11 | -class CookieJar implements CookieJarInterface |
|
12 | -{ |
|
11 | +class CookieJar implements CookieJarInterface { |
|
13 | 12 | /** |
14 | 13 | * @var SetCookie[] Loaded cookie data |
15 | 14 | */ |
@@ -2,6 +2,5 @@ |
||
2 | 2 | |
3 | 3 | namespace OCA\FullTextSearch_Elasticsearch\Vendor\GuzzleHttp\Exception; |
4 | 4 | |
5 | -class TooManyRedirectsException extends RequestException |
|
6 | -{ |
|
5 | +class TooManyRedirectsException extends RequestException { |
|
7 | 6 | } |
@@ -5,6 +5,5 @@ |
||
5 | 5 | /** |
6 | 6 | * Exception when a client error is encountered (4xx codes) |
7 | 7 | */ |
8 | -class ClientException extends BadResponseException |
|
9 | -{ |
|
8 | +class ClientException extends BadResponseException { |
|
10 | 9 | } |
@@ -5,6 +5,5 @@ |
||
5 | 5 | /** |
6 | 6 | * Exception when a server error is encountered (5xx codes) |
7 | 7 | */ |
8 | -class ServerException extends BadResponseException |
|
9 | -{ |
|
8 | +class ServerException extends BadResponseException { |
|
10 | 9 | } |
@@ -4,6 +4,5 @@ |
||
4 | 4 | |
5 | 5 | use OCA\FullTextSearch_Elasticsearch\Vendor\Psr\Http\Client\ClientExceptionInterface; |
6 | 6 | |
7 | -interface GuzzleException extends ClientExceptionInterface |
|
8 | -{ |
|
7 | +interface GuzzleException extends ClientExceptionInterface { |
|
9 | 8 | } |
@@ -14,153 +14,153 @@ |
||
14 | 14 | */ |
15 | 15 | class RequestException extends TransferException implements RequestExceptionInterface |
16 | 16 | { |
17 | - /** |
|
18 | - * @var RequestInterface |
|
19 | - */ |
|
20 | - private $request; |
|
21 | - |
|
22 | - /** |
|
23 | - * @var ResponseInterface|null |
|
24 | - */ |
|
25 | - private $response; |
|
26 | - |
|
27 | - /** |
|
28 | - * @var array |
|
29 | - */ |
|
30 | - private $handlerContext; |
|
31 | - |
|
32 | - public function __construct( |
|
33 | - string $message, |
|
34 | - RequestInterface $request, |
|
35 | - ResponseInterface $response = null, |
|
36 | - \Throwable $previous = null, |
|
37 | - array $handlerContext = [] |
|
38 | - ) { |
|
39 | - // Set the code of the exception if the response is set and not future. |
|
40 | - $code = $response ? $response->getStatusCode() : 0; |
|
41 | - parent::__construct($message, $code, $previous); |
|
42 | - $this->request = $request; |
|
43 | - $this->response = $response; |
|
44 | - $this->handlerContext = $handlerContext; |
|
45 | - } |
|
46 | - |
|
47 | - /** |
|
48 | - * Wrap non-RequestExceptions with a RequestException |
|
49 | - */ |
|
50 | - public static function wrapException(RequestInterface $request, \Throwable $e): RequestException |
|
51 | - { |
|
52 | - return $e instanceof RequestException ? $e : new RequestException($e->getMessage(), $request, null, $e); |
|
53 | - } |
|
54 | - |
|
55 | - /** |
|
56 | - * Factory method to create a new exception with a normalized error message |
|
57 | - * |
|
58 | - * @param RequestInterface $request Request sent |
|
59 | - * @param ResponseInterface $response Response received |
|
60 | - * @param \Throwable|null $previous Previous exception |
|
61 | - * @param array $handlerContext Optional handler context |
|
62 | - * @param BodySummarizerInterface|null $bodySummarizer Optional body summarizer |
|
63 | - */ |
|
64 | - public static function create( |
|
65 | - RequestInterface $request, |
|
66 | - ResponseInterface $response = null, |
|
67 | - \Throwable $previous = null, |
|
68 | - array $handlerContext = [], |
|
69 | - BodySummarizerInterface $bodySummarizer = null |
|
70 | - ): self { |
|
71 | - if (!$response) { |
|
72 | - return new self( |
|
73 | - 'Error completing request', |
|
74 | - $request, |
|
75 | - null, |
|
76 | - $previous, |
|
77 | - $handlerContext |
|
78 | - ); |
|
79 | - } |
|
80 | - |
|
81 | - $level = (int) \floor($response->getStatusCode() / 100); |
|
82 | - if ($level === 4) { |
|
83 | - $label = 'Client error'; |
|
84 | - $className = ClientException::class; |
|
85 | - } elseif ($level === 5) { |
|
86 | - $label = 'Server error'; |
|
87 | - $className = ServerException::class; |
|
88 | - } else { |
|
89 | - $label = 'Unsuccessful request'; |
|
90 | - $className = __CLASS__; |
|
91 | - } |
|
92 | - |
|
93 | - $uri = $request->getUri(); |
|
94 | - $uri = static::obfuscateUri($uri); |
|
95 | - |
|
96 | - // Client Error: `GET /` resulted in a `404 Not Found` response: |
|
97 | - // <html> ... (truncated) |
|
98 | - $message = \sprintf( |
|
99 | - '%s: `%s %s` resulted in a `%s %s` response', |
|
100 | - $label, |
|
101 | - $request->getMethod(), |
|
102 | - $uri->__toString(), |
|
103 | - $response->getStatusCode(), |
|
104 | - $response->getReasonPhrase() |
|
105 | - ); |
|
106 | - |
|
107 | - $summary = ($bodySummarizer ?? new BodySummarizer())->summarize($response); |
|
108 | - |
|
109 | - if ($summary !== null) { |
|
110 | - $message .= ":\n{$summary}\n"; |
|
111 | - } |
|
112 | - |
|
113 | - return new $className($message, $request, $response, $previous, $handlerContext); |
|
114 | - } |
|
115 | - |
|
116 | - /** |
|
117 | - * Obfuscates URI if there is a username and a password present |
|
118 | - */ |
|
119 | - private static function obfuscateUri(UriInterface $uri): UriInterface |
|
120 | - { |
|
121 | - $userInfo = $uri->getUserInfo(); |
|
122 | - |
|
123 | - if (false !== ($pos = \strpos($userInfo, ':'))) { |
|
124 | - return $uri->withUserInfo(\substr($userInfo, 0, $pos), '***'); |
|
125 | - } |
|
126 | - |
|
127 | - return $uri; |
|
128 | - } |
|
129 | - |
|
130 | - /** |
|
131 | - * Get the request that caused the exception |
|
132 | - */ |
|
133 | - public function getRequest(): RequestInterface |
|
134 | - { |
|
135 | - return $this->request; |
|
136 | - } |
|
137 | - |
|
138 | - /** |
|
139 | - * Get the associated response |
|
140 | - */ |
|
141 | - public function getResponse(): ?ResponseInterface |
|
142 | - { |
|
143 | - return $this->response; |
|
144 | - } |
|
145 | - |
|
146 | - /** |
|
147 | - * Check if a response was received |
|
148 | - */ |
|
149 | - public function hasResponse(): bool |
|
150 | - { |
|
151 | - return $this->response !== null; |
|
152 | - } |
|
153 | - |
|
154 | - /** |
|
155 | - * Get contextual information about the error from the underlying handler. |
|
156 | - * |
|
157 | - * The contents of this array will vary depending on which handler you are |
|
158 | - * using. It may also be just an empty array. Relying on this data will |
|
159 | - * couple you to a specific handler, but can give more debug information |
|
160 | - * when needed. |
|
161 | - */ |
|
162 | - public function getHandlerContext(): array |
|
163 | - { |
|
164 | - return $this->handlerContext; |
|
165 | - } |
|
17 | + /** |
|
18 | + * @var RequestInterface |
|
19 | + */ |
|
20 | + private $request; |
|
21 | + |
|
22 | + /** |
|
23 | + * @var ResponseInterface|null |
|
24 | + */ |
|
25 | + private $response; |
|
26 | + |
|
27 | + /** |
|
28 | + * @var array |
|
29 | + */ |
|
30 | + private $handlerContext; |
|
31 | + |
|
32 | + public function __construct( |
|
33 | + string $message, |
|
34 | + RequestInterface $request, |
|
35 | + ResponseInterface $response = null, |
|
36 | + \Throwable $previous = null, |
|
37 | + array $handlerContext = [] |
|
38 | + ) { |
|
39 | + // Set the code of the exception if the response is set and not future. |
|
40 | + $code = $response ? $response->getStatusCode() : 0; |
|
41 | + parent::__construct($message, $code, $previous); |
|
42 | + $this->request = $request; |
|
43 | + $this->response = $response; |
|
44 | + $this->handlerContext = $handlerContext; |
|
45 | + } |
|
46 | + |
|
47 | + /** |
|
48 | + * Wrap non-RequestExceptions with a RequestException |
|
49 | + */ |
|
50 | + public static function wrapException(RequestInterface $request, \Throwable $e): RequestException |
|
51 | + { |
|
52 | + return $e instanceof RequestException ? $e : new RequestException($e->getMessage(), $request, null, $e); |
|
53 | + } |
|
54 | + |
|
55 | + /** |
|
56 | + * Factory method to create a new exception with a normalized error message |
|
57 | + * |
|
58 | + * @param RequestInterface $request Request sent |
|
59 | + * @param ResponseInterface $response Response received |
|
60 | + * @param \Throwable|null $previous Previous exception |
|
61 | + * @param array $handlerContext Optional handler context |
|
62 | + * @param BodySummarizerInterface|null $bodySummarizer Optional body summarizer |
|
63 | + */ |
|
64 | + public static function create( |
|
65 | + RequestInterface $request, |
|
66 | + ResponseInterface $response = null, |
|
67 | + \Throwable $previous = null, |
|
68 | + array $handlerContext = [], |
|
69 | + BodySummarizerInterface $bodySummarizer = null |
|
70 | + ): self { |
|
71 | + if (!$response) { |
|
72 | + return new self( |
|
73 | + 'Error completing request', |
|
74 | + $request, |
|
75 | + null, |
|
76 | + $previous, |
|
77 | + $handlerContext |
|
78 | + ); |
|
79 | + } |
|
80 | + |
|
81 | + $level = (int) \floor($response->getStatusCode() / 100); |
|
82 | + if ($level === 4) { |
|
83 | + $label = 'Client error'; |
|
84 | + $className = ClientException::class; |
|
85 | + } elseif ($level === 5) { |
|
86 | + $label = 'Server error'; |
|
87 | + $className = ServerException::class; |
|
88 | + } else { |
|
89 | + $label = 'Unsuccessful request'; |
|
90 | + $className = __CLASS__; |
|
91 | + } |
|
92 | + |
|
93 | + $uri = $request->getUri(); |
|
94 | + $uri = static::obfuscateUri($uri); |
|
95 | + |
|
96 | + // Client Error: `GET /` resulted in a `404 Not Found` response: |
|
97 | + // <html> ... (truncated) |
|
98 | + $message = \sprintf( |
|
99 | + '%s: `%s %s` resulted in a `%s %s` response', |
|
100 | + $label, |
|
101 | + $request->getMethod(), |
|
102 | + $uri->__toString(), |
|
103 | + $response->getStatusCode(), |
|
104 | + $response->getReasonPhrase() |
|
105 | + ); |
|
106 | + |
|
107 | + $summary = ($bodySummarizer ?? new BodySummarizer())->summarize($response); |
|
108 | + |
|
109 | + if ($summary !== null) { |
|
110 | + $message .= ":\n{$summary}\n"; |
|
111 | + } |
|
112 | + |
|
113 | + return new $className($message, $request, $response, $previous, $handlerContext); |
|
114 | + } |
|
115 | + |
|
116 | + /** |
|
117 | + * Obfuscates URI if there is a username and a password present |
|
118 | + */ |
|
119 | + private static function obfuscateUri(UriInterface $uri): UriInterface |
|
120 | + { |
|
121 | + $userInfo = $uri->getUserInfo(); |
|
122 | + |
|
123 | + if (false !== ($pos = \strpos($userInfo, ':'))) { |
|
124 | + return $uri->withUserInfo(\substr($userInfo, 0, $pos), '***'); |
|
125 | + } |
|
126 | + |
|
127 | + return $uri; |
|
128 | + } |
|
129 | + |
|
130 | + /** |
|
131 | + * Get the request that caused the exception |
|
132 | + */ |
|
133 | + public function getRequest(): RequestInterface |
|
134 | + { |
|
135 | + return $this->request; |
|
136 | + } |
|
137 | + |
|
138 | + /** |
|
139 | + * Get the associated response |
|
140 | + */ |
|
141 | + public function getResponse(): ?ResponseInterface |
|
142 | + { |
|
143 | + return $this->response; |
|
144 | + } |
|
145 | + |
|
146 | + /** |
|
147 | + * Check if a response was received |
|
148 | + */ |
|
149 | + public function hasResponse(): bool |
|
150 | + { |
|
151 | + return $this->response !== null; |
|
152 | + } |
|
153 | + |
|
154 | + /** |
|
155 | + * Get contextual information about the error from the underlying handler. |
|
156 | + * |
|
157 | + * The contents of this array will vary depending on which handler you are |
|
158 | + * using. It may also be just an empty array. Relying on this data will |
|
159 | + * couple you to a specific handler, but can give more debug information |
|
160 | + * when needed. |
|
161 | + */ |
|
162 | + public function getHandlerContext(): array |
|
163 | + { |
|
164 | + return $this->handlerContext; |
|
165 | + } |
|
166 | 166 | } |
@@ -78,7 +78,7 @@ |
||
78 | 78 | ); |
79 | 79 | } |
80 | 80 | |
81 | - $level = (int) \floor($response->getStatusCode() / 100); |
|
81 | + $level = (int)\floor($response->getStatusCode() / 100); |
|
82 | 82 | if ($level === 4) { |
83 | 83 | $label = 'Client error'; |
84 | 84 | $className = ClientException::class; |
@@ -12,8 +12,7 @@ |
||
12 | 12 | /** |
13 | 13 | * HTTP Request exception |
14 | 14 | */ |
15 | -class RequestException extends TransferException implements RequestExceptionInterface |
|
16 | -{ |
|
15 | +class RequestException extends TransferException implements RequestExceptionInterface { |
|
17 | 16 | /** |
18 | 17 | * @var RequestInterface |
19 | 18 | */ |
@@ -10,30 +10,30 @@ |
||
10 | 10 | */ |
11 | 11 | class BadResponseException extends RequestException |
12 | 12 | { |
13 | - public function __construct( |
|
14 | - string $message, |
|
15 | - RequestInterface $request, |
|
16 | - ResponseInterface $response, |
|
17 | - \Throwable $previous = null, |
|
18 | - array $handlerContext = [] |
|
19 | - ) { |
|
20 | - parent::__construct($message, $request, $response, $previous, $handlerContext); |
|
21 | - } |
|
13 | + public function __construct( |
|
14 | + string $message, |
|
15 | + RequestInterface $request, |
|
16 | + ResponseInterface $response, |
|
17 | + \Throwable $previous = null, |
|
18 | + array $handlerContext = [] |
|
19 | + ) { |
|
20 | + parent::__construct($message, $request, $response, $previous, $handlerContext); |
|
21 | + } |
|
22 | 22 | |
23 | - /** |
|
24 | - * Current exception and the ones that extend it will always have a response. |
|
25 | - */ |
|
26 | - public function hasResponse(): bool |
|
27 | - { |
|
28 | - return true; |
|
29 | - } |
|
23 | + /** |
|
24 | + * Current exception and the ones that extend it will always have a response. |
|
25 | + */ |
|
26 | + public function hasResponse(): bool |
|
27 | + { |
|
28 | + return true; |
|
29 | + } |
|
30 | 30 | |
31 | - /** |
|
32 | - * This function narrows the return type from the parent class and does not allow it to be nullable. |
|
33 | - */ |
|
34 | - public function getResponse(): ResponseInterface |
|
35 | - { |
|
36 | - /** @var ResponseInterface */ |
|
37 | - return parent::getResponse(); |
|
38 | - } |
|
31 | + /** |
|
32 | + * This function narrows the return type from the parent class and does not allow it to be nullable. |
|
33 | + */ |
|
34 | + public function getResponse(): ResponseInterface |
|
35 | + { |
|
36 | + /** @var ResponseInterface */ |
|
37 | + return parent::getResponse(); |
|
38 | + } |
|
39 | 39 | } |
@@ -8,8 +8,7 @@ |
||
8 | 8 | /** |
9 | 9 | * Exception when an HTTP error occurs (4xx or 5xx error) |
10 | 10 | */ |
11 | -class BadResponseException extends RequestException |
|
12 | -{ |
|
11 | +class BadResponseException extends RequestException { |
|
13 | 12 | public function __construct( |
14 | 13 | string $message, |
15 | 14 | RequestInterface $request, |
@@ -12,45 +12,45 @@ |
||
12 | 12 | */ |
13 | 13 | class ConnectException extends TransferException implements NetworkExceptionInterface |
14 | 14 | { |
15 | - /** |
|
16 | - * @var RequestInterface |
|
17 | - */ |
|
18 | - private $request; |
|
15 | + /** |
|
16 | + * @var RequestInterface |
|
17 | + */ |
|
18 | + private $request; |
|
19 | 19 | |
20 | - /** |
|
21 | - * @var array |
|
22 | - */ |
|
23 | - private $handlerContext; |
|
20 | + /** |
|
21 | + * @var array |
|
22 | + */ |
|
23 | + private $handlerContext; |
|
24 | 24 | |
25 | - public function __construct( |
|
26 | - string $message, |
|
27 | - RequestInterface $request, |
|
28 | - \Throwable $previous = null, |
|
29 | - array $handlerContext = [] |
|
30 | - ) { |
|
31 | - parent::__construct($message, 0, $previous); |
|
32 | - $this->request = $request; |
|
33 | - $this->handlerContext = $handlerContext; |
|
34 | - } |
|
25 | + public function __construct( |
|
26 | + string $message, |
|
27 | + RequestInterface $request, |
|
28 | + \Throwable $previous = null, |
|
29 | + array $handlerContext = [] |
|
30 | + ) { |
|
31 | + parent::__construct($message, 0, $previous); |
|
32 | + $this->request = $request; |
|
33 | + $this->handlerContext = $handlerContext; |
|
34 | + } |
|
35 | 35 | |
36 | - /** |
|
37 | - * Get the request that caused the exception |
|
38 | - */ |
|
39 | - public function getRequest(): RequestInterface |
|
40 | - { |
|
41 | - return $this->request; |
|
42 | - } |
|
36 | + /** |
|
37 | + * Get the request that caused the exception |
|
38 | + */ |
|
39 | + public function getRequest(): RequestInterface |
|
40 | + { |
|
41 | + return $this->request; |
|
42 | + } |
|
43 | 43 | |
44 | - /** |
|
45 | - * Get contextual information about the error from the underlying handler. |
|
46 | - * |
|
47 | - * The contents of this array will vary depending on which handler you are |
|
48 | - * using. It may also be just an empty array. Relying on this data will |
|
49 | - * couple you to a specific handler, but can give more debug information |
|
50 | - * when needed. |
|
51 | - */ |
|
52 | - public function getHandlerContext(): array |
|
53 | - { |
|
54 | - return $this->handlerContext; |
|
55 | - } |
|
44 | + /** |
|
45 | + * Get contextual information about the error from the underlying handler. |
|
46 | + * |
|
47 | + * The contents of this array will vary depending on which handler you are |
|
48 | + * using. It may also be just an empty array. Relying on this data will |
|
49 | + * couple you to a specific handler, but can give more debug information |
|
50 | + * when needed. |
|
51 | + */ |
|
52 | + public function getHandlerContext(): array |
|
53 | + { |
|
54 | + return $this->handlerContext; |
|
55 | + } |
|
56 | 56 | } |
@@ -10,8 +10,7 @@ |
||
10 | 10 | * |
11 | 11 | * Note that no response is present for a ConnectException |
12 | 12 | */ |
13 | -class ConnectException extends TransferException implements NetworkExceptionInterface |
|
14 | -{ |
|
13 | +class ConnectException extends TransferException implements NetworkExceptionInterface { |
|
15 | 14 | /** |
16 | 15 | * @var RequestInterface |
17 | 16 | */ |
@@ -11,453 +11,453 @@ |
||
11 | 11 | |
12 | 12 | final class Utils |
13 | 13 | { |
14 | - /** |
|
15 | - * Remove the items given by the keys, case insensitively from the data. |
|
16 | - * |
|
17 | - * @param (string|int)[] $keys |
|
18 | - */ |
|
19 | - public static function caselessRemove(array $keys, array $data): array |
|
20 | - { |
|
21 | - $result = []; |
|
22 | - |
|
23 | - foreach ($keys as &$key) { |
|
24 | - $key = strtolower((string) $key); |
|
25 | - } |
|
26 | - |
|
27 | - foreach ($data as $k => $v) { |
|
28 | - if (!in_array(strtolower((string) $k), $keys)) { |
|
29 | - $result[$k] = $v; |
|
30 | - } |
|
31 | - } |
|
32 | - |
|
33 | - return $result; |
|
34 | - } |
|
35 | - |
|
36 | - /** |
|
37 | - * Copy the contents of a stream into another stream until the given number |
|
38 | - * of bytes have been read. |
|
39 | - * |
|
40 | - * @param StreamInterface $source Stream to read from |
|
41 | - * @param StreamInterface $dest Stream to write to |
|
42 | - * @param int $maxLen Maximum number of bytes to read. Pass -1 |
|
43 | - * to read the entire stream. |
|
44 | - * |
|
45 | - * @throws \RuntimeException on error. |
|
46 | - */ |
|
47 | - public static function copyToStream(StreamInterface $source, StreamInterface $dest, int $maxLen = -1): void |
|
48 | - { |
|
49 | - $bufferSize = 8192; |
|
50 | - |
|
51 | - if ($maxLen === -1) { |
|
52 | - while (!$source->eof()) { |
|
53 | - if (!$dest->write($source->read($bufferSize))) { |
|
54 | - break; |
|
55 | - } |
|
56 | - } |
|
57 | - } else { |
|
58 | - $remaining = $maxLen; |
|
59 | - while ($remaining > 0 && !$source->eof()) { |
|
60 | - $buf = $source->read(min($bufferSize, $remaining)); |
|
61 | - $len = strlen($buf); |
|
62 | - if (!$len) { |
|
63 | - break; |
|
64 | - } |
|
65 | - $remaining -= $len; |
|
66 | - $dest->write($buf); |
|
67 | - } |
|
68 | - } |
|
69 | - } |
|
70 | - |
|
71 | - /** |
|
72 | - * Copy the contents of a stream into a string until the given number of |
|
73 | - * bytes have been read. |
|
74 | - * |
|
75 | - * @param StreamInterface $stream Stream to read |
|
76 | - * @param int $maxLen Maximum number of bytes to read. Pass -1 |
|
77 | - * to read the entire stream. |
|
78 | - * |
|
79 | - * @throws \RuntimeException on error. |
|
80 | - */ |
|
81 | - public static function copyToString(StreamInterface $stream, int $maxLen = -1): string |
|
82 | - { |
|
83 | - $buffer = ''; |
|
84 | - |
|
85 | - if ($maxLen === -1) { |
|
86 | - while (!$stream->eof()) { |
|
87 | - $buf = $stream->read(1048576); |
|
88 | - if ($buf === '') { |
|
89 | - break; |
|
90 | - } |
|
91 | - $buffer .= $buf; |
|
92 | - } |
|
93 | - |
|
94 | - return $buffer; |
|
95 | - } |
|
96 | - |
|
97 | - $len = 0; |
|
98 | - while (!$stream->eof() && $len < $maxLen) { |
|
99 | - $buf = $stream->read($maxLen - $len); |
|
100 | - if ($buf === '') { |
|
101 | - break; |
|
102 | - } |
|
103 | - $buffer .= $buf; |
|
104 | - $len = strlen($buffer); |
|
105 | - } |
|
106 | - |
|
107 | - return $buffer; |
|
108 | - } |
|
109 | - |
|
110 | - /** |
|
111 | - * Calculate a hash of a stream. |
|
112 | - * |
|
113 | - * This method reads the entire stream to calculate a rolling hash, based |
|
114 | - * on PHP's `hash_init` functions. |
|
115 | - * |
|
116 | - * @param StreamInterface $stream Stream to calculate the hash for |
|
117 | - * @param string $algo Hash algorithm (e.g. md5, crc32, etc) |
|
118 | - * @param bool $rawOutput Whether or not to use raw output |
|
119 | - * |
|
120 | - * @throws \RuntimeException on error. |
|
121 | - */ |
|
122 | - public static function hash(StreamInterface $stream, string $algo, bool $rawOutput = false): string |
|
123 | - { |
|
124 | - $pos = $stream->tell(); |
|
125 | - |
|
126 | - if ($pos > 0) { |
|
127 | - $stream->rewind(); |
|
128 | - } |
|
129 | - |
|
130 | - $ctx = hash_init($algo); |
|
131 | - while (!$stream->eof()) { |
|
132 | - hash_update($ctx, $stream->read(1048576)); |
|
133 | - } |
|
134 | - |
|
135 | - $out = hash_final($ctx, $rawOutput); |
|
136 | - $stream->seek($pos); |
|
137 | - |
|
138 | - return $out; |
|
139 | - } |
|
140 | - |
|
141 | - /** |
|
142 | - * Clone and modify a request with the given changes. |
|
143 | - * |
|
144 | - * This method is useful for reducing the number of clones needed to mutate |
|
145 | - * a message. |
|
146 | - * |
|
147 | - * The changes can be one of: |
|
148 | - * - method: (string) Changes the HTTP method. |
|
149 | - * - set_headers: (array) Sets the given headers. |
|
150 | - * - remove_headers: (array) Remove the given headers. |
|
151 | - * - body: (mixed) Sets the given body. |
|
152 | - * - uri: (UriInterface) Set the URI. |
|
153 | - * - query: (string) Set the query string value of the URI. |
|
154 | - * - version: (string) Set the protocol version. |
|
155 | - * |
|
156 | - * @param RequestInterface $request Request to clone and modify. |
|
157 | - * @param array $changes Changes to apply. |
|
158 | - */ |
|
159 | - public static function modifyRequest(RequestInterface $request, array $changes): RequestInterface |
|
160 | - { |
|
161 | - if (!$changes) { |
|
162 | - return $request; |
|
163 | - } |
|
164 | - |
|
165 | - $headers = $request->getHeaders(); |
|
166 | - |
|
167 | - if (!isset($changes['uri'])) { |
|
168 | - $uri = $request->getUri(); |
|
169 | - } else { |
|
170 | - // Remove the host header if one is on the URI |
|
171 | - if ($host = $changes['uri']->getHost()) { |
|
172 | - $changes['set_headers']['Host'] = $host; |
|
173 | - |
|
174 | - if ($port = $changes['uri']->getPort()) { |
|
175 | - $standardPorts = ['http' => 80, 'https' => 443]; |
|
176 | - $scheme = $changes['uri']->getScheme(); |
|
177 | - if (isset($standardPorts[$scheme]) && $port != $standardPorts[$scheme]) { |
|
178 | - $changes['set_headers']['Host'] .= ':'.$port; |
|
179 | - } |
|
180 | - } |
|
181 | - } |
|
182 | - $uri = $changes['uri']; |
|
183 | - } |
|
184 | - |
|
185 | - if (!empty($changes['remove_headers'])) { |
|
186 | - $headers = self::caselessRemove($changes['remove_headers'], $headers); |
|
187 | - } |
|
188 | - |
|
189 | - if (!empty($changes['set_headers'])) { |
|
190 | - $headers = self::caselessRemove(array_keys($changes['set_headers']), $headers); |
|
191 | - $headers = $changes['set_headers'] + $headers; |
|
192 | - } |
|
193 | - |
|
194 | - if (isset($changes['query'])) { |
|
195 | - $uri = $uri->withQuery($changes['query']); |
|
196 | - } |
|
197 | - |
|
198 | - if ($request instanceof ServerRequestInterface) { |
|
199 | - $new = (new ServerRequest( |
|
200 | - $changes['method'] ?? $request->getMethod(), |
|
201 | - $uri, |
|
202 | - $headers, |
|
203 | - $changes['body'] ?? $request->getBody(), |
|
204 | - $changes['version'] ?? $request->getProtocolVersion(), |
|
205 | - $request->getServerParams() |
|
206 | - )) |
|
207 | - ->withParsedBody($request->getParsedBody()) |
|
208 | - ->withQueryParams($request->getQueryParams()) |
|
209 | - ->withCookieParams($request->getCookieParams()) |
|
210 | - ->withUploadedFiles($request->getUploadedFiles()); |
|
211 | - |
|
212 | - foreach ($request->getAttributes() as $key => $value) { |
|
213 | - $new = $new->withAttribute($key, $value); |
|
214 | - } |
|
215 | - |
|
216 | - return $new; |
|
217 | - } |
|
218 | - |
|
219 | - return new Request( |
|
220 | - $changes['method'] ?? $request->getMethod(), |
|
221 | - $uri, |
|
222 | - $headers, |
|
223 | - $changes['body'] ?? $request->getBody(), |
|
224 | - $changes['version'] ?? $request->getProtocolVersion() |
|
225 | - ); |
|
226 | - } |
|
227 | - |
|
228 | - /** |
|
229 | - * Read a line from the stream up to the maximum allowed buffer length. |
|
230 | - * |
|
231 | - * @param StreamInterface $stream Stream to read from |
|
232 | - * @param int|null $maxLength Maximum buffer length |
|
233 | - */ |
|
234 | - public static function readLine(StreamInterface $stream, int $maxLength = null): string |
|
235 | - { |
|
236 | - $buffer = ''; |
|
237 | - $size = 0; |
|
238 | - |
|
239 | - while (!$stream->eof()) { |
|
240 | - if ('' === ($byte = $stream->read(1))) { |
|
241 | - return $buffer; |
|
242 | - } |
|
243 | - $buffer .= $byte; |
|
244 | - // Break when a new line is found or the max length - 1 is reached |
|
245 | - if ($byte === "\n" || ++$size === $maxLength - 1) { |
|
246 | - break; |
|
247 | - } |
|
248 | - } |
|
249 | - |
|
250 | - return $buffer; |
|
251 | - } |
|
252 | - |
|
253 | - /** |
|
254 | - * Create a new stream based on the input type. |
|
255 | - * |
|
256 | - * Options is an associative array that can contain the following keys: |
|
257 | - * - metadata: Array of custom metadata. |
|
258 | - * - size: Size of the stream. |
|
259 | - * |
|
260 | - * This method accepts the following `$resource` types: |
|
261 | - * - `OCA\FullTextSearch_Elasticsearch\Vendor\Psr\Http\Message\StreamInterface`: Returns the value as-is. |
|
262 | - * - `string`: Creates a stream object that uses the given string as the contents. |
|
263 | - * - `resource`: Creates a stream object that wraps the given PHP stream resource. |
|
264 | - * - `Iterator`: If the provided value implements `Iterator`, then a read-only |
|
265 | - * stream object will be created that wraps the given iterable. Each time the |
|
266 | - * stream is read from, data from the iterator will fill a buffer and will be |
|
267 | - * continuously called until the buffer is equal to the requested read size. |
|
268 | - * Subsequent read calls will first read from the buffer and then call `next` |
|
269 | - * on the underlying iterator until it is exhausted. |
|
270 | - * - `object` with `__toString()`: If the object has the `__toString()` method, |
|
271 | - * the object will be cast to a string and then a stream will be returned that |
|
272 | - * uses the string value. |
|
273 | - * - `NULL`: When `null` is passed, an empty stream object is returned. |
|
274 | - * - `callable` When a callable is passed, a read-only stream object will be |
|
275 | - * created that invokes the given callable. The callable is invoked with the |
|
276 | - * number of suggested bytes to read. The callable can return any number of |
|
277 | - * bytes, but MUST return `false` when there is no more data to return. The |
|
278 | - * stream object that wraps the callable will invoke the callable until the |
|
279 | - * number of requested bytes are available. Any additional bytes will be |
|
280 | - * buffered and used in subsequent reads. |
|
281 | - * |
|
282 | - * @param resource|string|int|float|bool|StreamInterface|callable|\Iterator|null $resource Entity body data |
|
283 | - * @param array{size?: int, metadata?: array} $options Additional options |
|
284 | - * |
|
285 | - * @throws \InvalidArgumentException if the $resource arg is not valid. |
|
286 | - */ |
|
287 | - public static function streamFor($resource = '', array $options = []): StreamInterface |
|
288 | - { |
|
289 | - if (is_scalar($resource)) { |
|
290 | - $stream = self::tryFopen('php://temp', 'r+'); |
|
291 | - if ($resource !== '') { |
|
292 | - fwrite($stream, (string) $resource); |
|
293 | - fseek($stream, 0); |
|
294 | - } |
|
295 | - |
|
296 | - return new Stream($stream, $options); |
|
297 | - } |
|
298 | - |
|
299 | - switch (gettype($resource)) { |
|
300 | - case 'resource': |
|
301 | - /* |
|
14 | + /** |
|
15 | + * Remove the items given by the keys, case insensitively from the data. |
|
16 | + * |
|
17 | + * @param (string|int)[] $keys |
|
18 | + */ |
|
19 | + public static function caselessRemove(array $keys, array $data): array |
|
20 | + { |
|
21 | + $result = []; |
|
22 | + |
|
23 | + foreach ($keys as &$key) { |
|
24 | + $key = strtolower((string) $key); |
|
25 | + } |
|
26 | + |
|
27 | + foreach ($data as $k => $v) { |
|
28 | + if (!in_array(strtolower((string) $k), $keys)) { |
|
29 | + $result[$k] = $v; |
|
30 | + } |
|
31 | + } |
|
32 | + |
|
33 | + return $result; |
|
34 | + } |
|
35 | + |
|
36 | + /** |
|
37 | + * Copy the contents of a stream into another stream until the given number |
|
38 | + * of bytes have been read. |
|
39 | + * |
|
40 | + * @param StreamInterface $source Stream to read from |
|
41 | + * @param StreamInterface $dest Stream to write to |
|
42 | + * @param int $maxLen Maximum number of bytes to read. Pass -1 |
|
43 | + * to read the entire stream. |
|
44 | + * |
|
45 | + * @throws \RuntimeException on error. |
|
46 | + */ |
|
47 | + public static function copyToStream(StreamInterface $source, StreamInterface $dest, int $maxLen = -1): void |
|
48 | + { |
|
49 | + $bufferSize = 8192; |
|
50 | + |
|
51 | + if ($maxLen === -1) { |
|
52 | + while (!$source->eof()) { |
|
53 | + if (!$dest->write($source->read($bufferSize))) { |
|
54 | + break; |
|
55 | + } |
|
56 | + } |
|
57 | + } else { |
|
58 | + $remaining = $maxLen; |
|
59 | + while ($remaining > 0 && !$source->eof()) { |
|
60 | + $buf = $source->read(min($bufferSize, $remaining)); |
|
61 | + $len = strlen($buf); |
|
62 | + if (!$len) { |
|
63 | + break; |
|
64 | + } |
|
65 | + $remaining -= $len; |
|
66 | + $dest->write($buf); |
|
67 | + } |
|
68 | + } |
|
69 | + } |
|
70 | + |
|
71 | + /** |
|
72 | + * Copy the contents of a stream into a string until the given number of |
|
73 | + * bytes have been read. |
|
74 | + * |
|
75 | + * @param StreamInterface $stream Stream to read |
|
76 | + * @param int $maxLen Maximum number of bytes to read. Pass -1 |
|
77 | + * to read the entire stream. |
|
78 | + * |
|
79 | + * @throws \RuntimeException on error. |
|
80 | + */ |
|
81 | + public static function copyToString(StreamInterface $stream, int $maxLen = -1): string |
|
82 | + { |
|
83 | + $buffer = ''; |
|
84 | + |
|
85 | + if ($maxLen === -1) { |
|
86 | + while (!$stream->eof()) { |
|
87 | + $buf = $stream->read(1048576); |
|
88 | + if ($buf === '') { |
|
89 | + break; |
|
90 | + } |
|
91 | + $buffer .= $buf; |
|
92 | + } |
|
93 | + |
|
94 | + return $buffer; |
|
95 | + } |
|
96 | + |
|
97 | + $len = 0; |
|
98 | + while (!$stream->eof() && $len < $maxLen) { |
|
99 | + $buf = $stream->read($maxLen - $len); |
|
100 | + if ($buf === '') { |
|
101 | + break; |
|
102 | + } |
|
103 | + $buffer .= $buf; |
|
104 | + $len = strlen($buffer); |
|
105 | + } |
|
106 | + |
|
107 | + return $buffer; |
|
108 | + } |
|
109 | + |
|
110 | + /** |
|
111 | + * Calculate a hash of a stream. |
|
112 | + * |
|
113 | + * This method reads the entire stream to calculate a rolling hash, based |
|
114 | + * on PHP's `hash_init` functions. |
|
115 | + * |
|
116 | + * @param StreamInterface $stream Stream to calculate the hash for |
|
117 | + * @param string $algo Hash algorithm (e.g. md5, crc32, etc) |
|
118 | + * @param bool $rawOutput Whether or not to use raw output |
|
119 | + * |
|
120 | + * @throws \RuntimeException on error. |
|
121 | + */ |
|
122 | + public static function hash(StreamInterface $stream, string $algo, bool $rawOutput = false): string |
|
123 | + { |
|
124 | + $pos = $stream->tell(); |
|
125 | + |
|
126 | + if ($pos > 0) { |
|
127 | + $stream->rewind(); |
|
128 | + } |
|
129 | + |
|
130 | + $ctx = hash_init($algo); |
|
131 | + while (!$stream->eof()) { |
|
132 | + hash_update($ctx, $stream->read(1048576)); |
|
133 | + } |
|
134 | + |
|
135 | + $out = hash_final($ctx, $rawOutput); |
|
136 | + $stream->seek($pos); |
|
137 | + |
|
138 | + return $out; |
|
139 | + } |
|
140 | + |
|
141 | + /** |
|
142 | + * Clone and modify a request with the given changes. |
|
143 | + * |
|
144 | + * This method is useful for reducing the number of clones needed to mutate |
|
145 | + * a message. |
|
146 | + * |
|
147 | + * The changes can be one of: |
|
148 | + * - method: (string) Changes the HTTP method. |
|
149 | + * - set_headers: (array) Sets the given headers. |
|
150 | + * - remove_headers: (array) Remove the given headers. |
|
151 | + * - body: (mixed) Sets the given body. |
|
152 | + * - uri: (UriInterface) Set the URI. |
|
153 | + * - query: (string) Set the query string value of the URI. |
|
154 | + * - version: (string) Set the protocol version. |
|
155 | + * |
|
156 | + * @param RequestInterface $request Request to clone and modify. |
|
157 | + * @param array $changes Changes to apply. |
|
158 | + */ |
|
159 | + public static function modifyRequest(RequestInterface $request, array $changes): RequestInterface |
|
160 | + { |
|
161 | + if (!$changes) { |
|
162 | + return $request; |
|
163 | + } |
|
164 | + |
|
165 | + $headers = $request->getHeaders(); |
|
166 | + |
|
167 | + if (!isset($changes['uri'])) { |
|
168 | + $uri = $request->getUri(); |
|
169 | + } else { |
|
170 | + // Remove the host header if one is on the URI |
|
171 | + if ($host = $changes['uri']->getHost()) { |
|
172 | + $changes['set_headers']['Host'] = $host; |
|
173 | + |
|
174 | + if ($port = $changes['uri']->getPort()) { |
|
175 | + $standardPorts = ['http' => 80, 'https' => 443]; |
|
176 | + $scheme = $changes['uri']->getScheme(); |
|
177 | + if (isset($standardPorts[$scheme]) && $port != $standardPorts[$scheme]) { |
|
178 | + $changes['set_headers']['Host'] .= ':'.$port; |
|
179 | + } |
|
180 | + } |
|
181 | + } |
|
182 | + $uri = $changes['uri']; |
|
183 | + } |
|
184 | + |
|
185 | + if (!empty($changes['remove_headers'])) { |
|
186 | + $headers = self::caselessRemove($changes['remove_headers'], $headers); |
|
187 | + } |
|
188 | + |
|
189 | + if (!empty($changes['set_headers'])) { |
|
190 | + $headers = self::caselessRemove(array_keys($changes['set_headers']), $headers); |
|
191 | + $headers = $changes['set_headers'] + $headers; |
|
192 | + } |
|
193 | + |
|
194 | + if (isset($changes['query'])) { |
|
195 | + $uri = $uri->withQuery($changes['query']); |
|
196 | + } |
|
197 | + |
|
198 | + if ($request instanceof ServerRequestInterface) { |
|
199 | + $new = (new ServerRequest( |
|
200 | + $changes['method'] ?? $request->getMethod(), |
|
201 | + $uri, |
|
202 | + $headers, |
|
203 | + $changes['body'] ?? $request->getBody(), |
|
204 | + $changes['version'] ?? $request->getProtocolVersion(), |
|
205 | + $request->getServerParams() |
|
206 | + )) |
|
207 | + ->withParsedBody($request->getParsedBody()) |
|
208 | + ->withQueryParams($request->getQueryParams()) |
|
209 | + ->withCookieParams($request->getCookieParams()) |
|
210 | + ->withUploadedFiles($request->getUploadedFiles()); |
|
211 | + |
|
212 | + foreach ($request->getAttributes() as $key => $value) { |
|
213 | + $new = $new->withAttribute($key, $value); |
|
214 | + } |
|
215 | + |
|
216 | + return $new; |
|
217 | + } |
|
218 | + |
|
219 | + return new Request( |
|
220 | + $changes['method'] ?? $request->getMethod(), |
|
221 | + $uri, |
|
222 | + $headers, |
|
223 | + $changes['body'] ?? $request->getBody(), |
|
224 | + $changes['version'] ?? $request->getProtocolVersion() |
|
225 | + ); |
|
226 | + } |
|
227 | + |
|
228 | + /** |
|
229 | + * Read a line from the stream up to the maximum allowed buffer length. |
|
230 | + * |
|
231 | + * @param StreamInterface $stream Stream to read from |
|
232 | + * @param int|null $maxLength Maximum buffer length |
|
233 | + */ |
|
234 | + public static function readLine(StreamInterface $stream, int $maxLength = null): string |
|
235 | + { |
|
236 | + $buffer = ''; |
|
237 | + $size = 0; |
|
238 | + |
|
239 | + while (!$stream->eof()) { |
|
240 | + if ('' === ($byte = $stream->read(1))) { |
|
241 | + return $buffer; |
|
242 | + } |
|
243 | + $buffer .= $byte; |
|
244 | + // Break when a new line is found or the max length - 1 is reached |
|
245 | + if ($byte === "\n" || ++$size === $maxLength - 1) { |
|
246 | + break; |
|
247 | + } |
|
248 | + } |
|
249 | + |
|
250 | + return $buffer; |
|
251 | + } |
|
252 | + |
|
253 | + /** |
|
254 | + * Create a new stream based on the input type. |
|
255 | + * |
|
256 | + * Options is an associative array that can contain the following keys: |
|
257 | + * - metadata: Array of custom metadata. |
|
258 | + * - size: Size of the stream. |
|
259 | + * |
|
260 | + * This method accepts the following `$resource` types: |
|
261 | + * - `OCA\FullTextSearch_Elasticsearch\Vendor\Psr\Http\Message\StreamInterface`: Returns the value as-is. |
|
262 | + * - `string`: Creates a stream object that uses the given string as the contents. |
|
263 | + * - `resource`: Creates a stream object that wraps the given PHP stream resource. |
|
264 | + * - `Iterator`: If the provided value implements `Iterator`, then a read-only |
|
265 | + * stream object will be created that wraps the given iterable. Each time the |
|
266 | + * stream is read from, data from the iterator will fill a buffer and will be |
|
267 | + * continuously called until the buffer is equal to the requested read size. |
|
268 | + * Subsequent read calls will first read from the buffer and then call `next` |
|
269 | + * on the underlying iterator until it is exhausted. |
|
270 | + * - `object` with `__toString()`: If the object has the `__toString()` method, |
|
271 | + * the object will be cast to a string and then a stream will be returned that |
|
272 | + * uses the string value. |
|
273 | + * - `NULL`: When `null` is passed, an empty stream object is returned. |
|
274 | + * - `callable` When a callable is passed, a read-only stream object will be |
|
275 | + * created that invokes the given callable. The callable is invoked with the |
|
276 | + * number of suggested bytes to read. The callable can return any number of |
|
277 | + * bytes, but MUST return `false` when there is no more data to return. The |
|
278 | + * stream object that wraps the callable will invoke the callable until the |
|
279 | + * number of requested bytes are available. Any additional bytes will be |
|
280 | + * buffered and used in subsequent reads. |
|
281 | + * |
|
282 | + * @param resource|string|int|float|bool|StreamInterface|callable|\Iterator|null $resource Entity body data |
|
283 | + * @param array{size?: int, metadata?: array} $options Additional options |
|
284 | + * |
|
285 | + * @throws \InvalidArgumentException if the $resource arg is not valid. |
|
286 | + */ |
|
287 | + public static function streamFor($resource = '', array $options = []): StreamInterface |
|
288 | + { |
|
289 | + if (is_scalar($resource)) { |
|
290 | + $stream = self::tryFopen('php://temp', 'r+'); |
|
291 | + if ($resource !== '') { |
|
292 | + fwrite($stream, (string) $resource); |
|
293 | + fseek($stream, 0); |
|
294 | + } |
|
295 | + |
|
296 | + return new Stream($stream, $options); |
|
297 | + } |
|
298 | + |
|
299 | + switch (gettype($resource)) { |
|
300 | + case 'resource': |
|
301 | + /* |
|
302 | 302 | * The 'php://input' is a special stream with quirks and inconsistencies. |
303 | 303 | * We avoid using that stream by reading it into php://temp |
304 | 304 | */ |
305 | 305 | |
306 | - /** @var resource $resource */ |
|
307 | - if ((\stream_get_meta_data($resource)['uri'] ?? '') === 'php://input') { |
|
308 | - $stream = self::tryFopen('php://temp', 'w+'); |
|
309 | - stream_copy_to_stream($resource, $stream); |
|
310 | - fseek($stream, 0); |
|
311 | - $resource = $stream; |
|
312 | - } |
|
313 | - |
|
314 | - return new Stream($resource, $options); |
|
315 | - case 'object': |
|
316 | - /** @var object $resource */ |
|
317 | - if ($resource instanceof StreamInterface) { |
|
318 | - return $resource; |
|
319 | - } elseif ($resource instanceof \Iterator) { |
|
320 | - return new PumpStream(function () use ($resource) { |
|
321 | - if (!$resource->valid()) { |
|
322 | - return false; |
|
323 | - } |
|
324 | - $result = $resource->current(); |
|
325 | - $resource->next(); |
|
326 | - |
|
327 | - return $result; |
|
328 | - }, $options); |
|
329 | - } elseif (method_exists($resource, '__toString')) { |
|
330 | - return self::streamFor((string) $resource, $options); |
|
331 | - } |
|
332 | - break; |
|
333 | - case 'NULL': |
|
334 | - return new Stream(self::tryFopen('php://temp', 'r+'), $options); |
|
335 | - } |
|
336 | - |
|
337 | - if (is_callable($resource)) { |
|
338 | - return new PumpStream($resource, $options); |
|
339 | - } |
|
340 | - |
|
341 | - throw new \InvalidArgumentException('Invalid resource type: '.gettype($resource)); |
|
342 | - } |
|
343 | - |
|
344 | - /** |
|
345 | - * Safely opens a PHP stream resource using a filename. |
|
346 | - * |
|
347 | - * When fopen fails, PHP normally raises a warning. This function adds an |
|
348 | - * error handler that checks for errors and throws an exception instead. |
|
349 | - * |
|
350 | - * @param string $filename File to open |
|
351 | - * @param string $mode Mode used to open the file |
|
352 | - * |
|
353 | - * @return resource |
|
354 | - * |
|
355 | - * @throws \RuntimeException if the file cannot be opened |
|
356 | - */ |
|
357 | - public static function tryFopen(string $filename, string $mode) |
|
358 | - { |
|
359 | - $ex = null; |
|
360 | - set_error_handler(static function (int $errno, string $errstr) use ($filename, $mode, &$ex): bool { |
|
361 | - $ex = new \RuntimeException(sprintf( |
|
362 | - 'Unable to open "%s" using mode "%s": %s', |
|
363 | - $filename, |
|
364 | - $mode, |
|
365 | - $errstr |
|
366 | - )); |
|
367 | - |
|
368 | - return true; |
|
369 | - }); |
|
370 | - |
|
371 | - try { |
|
372 | - /** @var resource $handle */ |
|
373 | - $handle = fopen($filename, $mode); |
|
374 | - } catch (\Throwable $e) { |
|
375 | - $ex = new \RuntimeException(sprintf( |
|
376 | - 'Unable to open "%s" using mode "%s": %s', |
|
377 | - $filename, |
|
378 | - $mode, |
|
379 | - $e->getMessage() |
|
380 | - ), 0, $e); |
|
381 | - } |
|
382 | - |
|
383 | - restore_error_handler(); |
|
384 | - |
|
385 | - if ($ex) { |
|
386 | - /** @var $ex \RuntimeException */ |
|
387 | - throw $ex; |
|
388 | - } |
|
389 | - |
|
390 | - return $handle; |
|
391 | - } |
|
392 | - |
|
393 | - /** |
|
394 | - * Safely gets the contents of a given stream. |
|
395 | - * |
|
396 | - * When stream_get_contents fails, PHP normally raises a warning. This |
|
397 | - * function adds an error handler that checks for errors and throws an |
|
398 | - * exception instead. |
|
399 | - * |
|
400 | - * @param resource $stream |
|
401 | - * |
|
402 | - * @throws \RuntimeException if the stream cannot be read |
|
403 | - */ |
|
404 | - public static function tryGetContents($stream): string |
|
405 | - { |
|
406 | - $ex = null; |
|
407 | - set_error_handler(static function (int $errno, string $errstr) use (&$ex): bool { |
|
408 | - $ex = new \RuntimeException(sprintf( |
|
409 | - 'Unable to read stream contents: %s', |
|
410 | - $errstr |
|
411 | - )); |
|
412 | - |
|
413 | - return true; |
|
414 | - }); |
|
415 | - |
|
416 | - try { |
|
417 | - /** @var string|false $contents */ |
|
418 | - $contents = stream_get_contents($stream); |
|
419 | - |
|
420 | - if ($contents === false) { |
|
421 | - $ex = new \RuntimeException('Unable to read stream contents'); |
|
422 | - } |
|
423 | - } catch (\Throwable $e) { |
|
424 | - $ex = new \RuntimeException(sprintf( |
|
425 | - 'Unable to read stream contents: %s', |
|
426 | - $e->getMessage() |
|
427 | - ), 0, $e); |
|
428 | - } |
|
429 | - |
|
430 | - restore_error_handler(); |
|
431 | - |
|
432 | - if ($ex) { |
|
433 | - /** @var $ex \RuntimeException */ |
|
434 | - throw $ex; |
|
435 | - } |
|
436 | - |
|
437 | - return $contents; |
|
438 | - } |
|
439 | - |
|
440 | - /** |
|
441 | - * Returns a UriInterface for the given value. |
|
442 | - * |
|
443 | - * This function accepts a string or UriInterface and returns a |
|
444 | - * UriInterface for the given value. If the value is already a |
|
445 | - * UriInterface, it is returned as-is. |
|
446 | - * |
|
447 | - * @param string|UriInterface $uri |
|
448 | - * |
|
449 | - * @throws \InvalidArgumentException |
|
450 | - */ |
|
451 | - public static function uriFor($uri): UriInterface |
|
452 | - { |
|
453 | - if ($uri instanceof UriInterface) { |
|
454 | - return $uri; |
|
455 | - } |
|
456 | - |
|
457 | - if (is_string($uri)) { |
|
458 | - return new Uri($uri); |
|
459 | - } |
|
460 | - |
|
461 | - throw new \InvalidArgumentException('URI must be a string or UriInterface'); |
|
462 | - } |
|
306 | + /** @var resource $resource */ |
|
307 | + if ((\stream_get_meta_data($resource)['uri'] ?? '') === 'php://input') { |
|
308 | + $stream = self::tryFopen('php://temp', 'w+'); |
|
309 | + stream_copy_to_stream($resource, $stream); |
|
310 | + fseek($stream, 0); |
|
311 | + $resource = $stream; |
|
312 | + } |
|
313 | + |
|
314 | + return new Stream($resource, $options); |
|
315 | + case 'object': |
|
316 | + /** @var object $resource */ |
|
317 | + if ($resource instanceof StreamInterface) { |
|
318 | + return $resource; |
|
319 | + } elseif ($resource instanceof \Iterator) { |
|
320 | + return new PumpStream(function () use ($resource) { |
|
321 | + if (!$resource->valid()) { |
|
322 | + return false; |
|
323 | + } |
|
324 | + $result = $resource->current(); |
|
325 | + $resource->next(); |
|
326 | + |
|
327 | + return $result; |
|
328 | + }, $options); |
|
329 | + } elseif (method_exists($resource, '__toString')) { |
|
330 | + return self::streamFor((string) $resource, $options); |
|
331 | + } |
|
332 | + break; |
|
333 | + case 'NULL': |
|
334 | + return new Stream(self::tryFopen('php://temp', 'r+'), $options); |
|
335 | + } |
|
336 | + |
|
337 | + if (is_callable($resource)) { |
|
338 | + return new PumpStream($resource, $options); |
|
339 | + } |
|
340 | + |
|
341 | + throw new \InvalidArgumentException('Invalid resource type: '.gettype($resource)); |
|
342 | + } |
|
343 | + |
|
344 | + /** |
|
345 | + * Safely opens a PHP stream resource using a filename. |
|
346 | + * |
|
347 | + * When fopen fails, PHP normally raises a warning. This function adds an |
|
348 | + * error handler that checks for errors and throws an exception instead. |
|
349 | + * |
|
350 | + * @param string $filename File to open |
|
351 | + * @param string $mode Mode used to open the file |
|
352 | + * |
|
353 | + * @return resource |
|
354 | + * |
|
355 | + * @throws \RuntimeException if the file cannot be opened |
|
356 | + */ |
|
357 | + public static function tryFopen(string $filename, string $mode) |
|
358 | + { |
|
359 | + $ex = null; |
|
360 | + set_error_handler(static function (int $errno, string $errstr) use ($filename, $mode, &$ex): bool { |
|
361 | + $ex = new \RuntimeException(sprintf( |
|
362 | + 'Unable to open "%s" using mode "%s": %s', |
|
363 | + $filename, |
|
364 | + $mode, |
|
365 | + $errstr |
|
366 | + )); |
|
367 | + |
|
368 | + return true; |
|
369 | + }); |
|
370 | + |
|
371 | + try { |
|
372 | + /** @var resource $handle */ |
|
373 | + $handle = fopen($filename, $mode); |
|
374 | + } catch (\Throwable $e) { |
|
375 | + $ex = new \RuntimeException(sprintf( |
|
376 | + 'Unable to open "%s" using mode "%s": %s', |
|
377 | + $filename, |
|
378 | + $mode, |
|
379 | + $e->getMessage() |
|
380 | + ), 0, $e); |
|
381 | + } |
|
382 | + |
|
383 | + restore_error_handler(); |
|
384 | + |
|
385 | + if ($ex) { |
|
386 | + /** @var $ex \RuntimeException */ |
|
387 | + throw $ex; |
|
388 | + } |
|
389 | + |
|
390 | + return $handle; |
|
391 | + } |
|
392 | + |
|
393 | + /** |
|
394 | + * Safely gets the contents of a given stream. |
|
395 | + * |
|
396 | + * When stream_get_contents fails, PHP normally raises a warning. This |
|
397 | + * function adds an error handler that checks for errors and throws an |
|
398 | + * exception instead. |
|
399 | + * |
|
400 | + * @param resource $stream |
|
401 | + * |
|
402 | + * @throws \RuntimeException if the stream cannot be read |
|
403 | + */ |
|
404 | + public static function tryGetContents($stream): string |
|
405 | + { |
|
406 | + $ex = null; |
|
407 | + set_error_handler(static function (int $errno, string $errstr) use (&$ex): bool { |
|
408 | + $ex = new \RuntimeException(sprintf( |
|
409 | + 'Unable to read stream contents: %s', |
|
410 | + $errstr |
|
411 | + )); |
|
412 | + |
|
413 | + return true; |
|
414 | + }); |
|
415 | + |
|
416 | + try { |
|
417 | + /** @var string|false $contents */ |
|
418 | + $contents = stream_get_contents($stream); |
|
419 | + |
|
420 | + if ($contents === false) { |
|
421 | + $ex = new \RuntimeException('Unable to read stream contents'); |
|
422 | + } |
|
423 | + } catch (\Throwable $e) { |
|
424 | + $ex = new \RuntimeException(sprintf( |
|
425 | + 'Unable to read stream contents: %s', |
|
426 | + $e->getMessage() |
|
427 | + ), 0, $e); |
|
428 | + } |
|
429 | + |
|
430 | + restore_error_handler(); |
|
431 | + |
|
432 | + if ($ex) { |
|
433 | + /** @var $ex \RuntimeException */ |
|
434 | + throw $ex; |
|
435 | + } |
|
436 | + |
|
437 | + return $contents; |
|
438 | + } |
|
439 | + |
|
440 | + /** |
|
441 | + * Returns a UriInterface for the given value. |
|
442 | + * |
|
443 | + * This function accepts a string or UriInterface and returns a |
|
444 | + * UriInterface for the given value. If the value is already a |
|
445 | + * UriInterface, it is returned as-is. |
|
446 | + * |
|
447 | + * @param string|UriInterface $uri |
|
448 | + * |
|
449 | + * @throws \InvalidArgumentException |
|
450 | + */ |
|
451 | + public static function uriFor($uri): UriInterface |
|
452 | + { |
|
453 | + if ($uri instanceof UriInterface) { |
|
454 | + return $uri; |
|
455 | + } |
|
456 | + |
|
457 | + if (is_string($uri)) { |
|
458 | + return new Uri($uri); |
|
459 | + } |
|
460 | + |
|
461 | + throw new \InvalidArgumentException('URI must be a string or UriInterface'); |
|
462 | + } |
|
463 | 463 | } |
@@ -21,11 +21,11 @@ discard block |
||
21 | 21 | $result = []; |
22 | 22 | |
23 | 23 | foreach ($keys as &$key) { |
24 | - $key = strtolower((string) $key); |
|
24 | + $key = strtolower((string)$key); |
|
25 | 25 | } |
26 | 26 | |
27 | 27 | foreach ($data as $k => $v) { |
28 | - if (!in_array(strtolower((string) $k), $keys)) { |
|
28 | + if (!in_array(strtolower((string)$k), $keys)) { |
|
29 | 29 | $result[$k] = $v; |
30 | 30 | } |
31 | 31 | } |
@@ -289,7 +289,7 @@ discard block |
||
289 | 289 | if (is_scalar($resource)) { |
290 | 290 | $stream = self::tryFopen('php://temp', 'r+'); |
291 | 291 | if ($resource !== '') { |
292 | - fwrite($stream, (string) $resource); |
|
292 | + fwrite($stream, (string)$resource); |
|
293 | 293 | fseek($stream, 0); |
294 | 294 | } |
295 | 295 | |
@@ -317,7 +317,7 @@ discard block |
||
317 | 317 | if ($resource instanceof StreamInterface) { |
318 | 318 | return $resource; |
319 | 319 | } elseif ($resource instanceof \Iterator) { |
320 | - return new PumpStream(function () use ($resource) { |
|
320 | + return new PumpStream(function() use ($resource) { |
|
321 | 321 | if (!$resource->valid()) { |
322 | 322 | return false; |
323 | 323 | } |
@@ -327,7 +327,7 @@ discard block |
||
327 | 327 | return $result; |
328 | 328 | }, $options); |
329 | 329 | } elseif (method_exists($resource, '__toString')) { |
330 | - return self::streamFor((string) $resource, $options); |
|
330 | + return self::streamFor((string)$resource, $options); |
|
331 | 331 | } |
332 | 332 | break; |
333 | 333 | case 'NULL': |
@@ -357,7 +357,7 @@ discard block |
||
357 | 357 | public static function tryFopen(string $filename, string $mode) |
358 | 358 | { |
359 | 359 | $ex = null; |
360 | - set_error_handler(static function (int $errno, string $errstr) use ($filename, $mode, &$ex): bool { |
|
360 | + set_error_handler(static function(int $errno, string $errstr) use ($filename, $mode, &$ex): bool { |
|
361 | 361 | $ex = new \RuntimeException(sprintf( |
362 | 362 | 'Unable to open "%s" using mode "%s": %s', |
363 | 363 | $filename, |
@@ -404,7 +404,7 @@ discard block |
||
404 | 404 | public static function tryGetContents($stream): string |
405 | 405 | { |
406 | 406 | $ex = null; |
407 | - set_error_handler(static function (int $errno, string $errstr) use (&$ex): bool { |
|
407 | + set_error_handler(static function(int $errno, string $errstr) use (&$ex): bool { |
|
408 | 408 | $ex = new \RuntimeException(sprintf( |
409 | 409 | 'Unable to read stream contents: %s', |
410 | 410 | $errstr |
@@ -9,8 +9,7 @@ |
||
9 | 9 | use OCA\FullTextSearch_Elasticsearch\Vendor\Psr\Http\Message\StreamInterface; |
10 | 10 | use OCA\FullTextSearch_Elasticsearch\Vendor\Psr\Http\Message\UriInterface; |
11 | 11 | |
12 | -final class Utils |
|
13 | -{ |
|
12 | +final class Utils { |
|
14 | 13 | /** |
15 | 14 | * Remove the items given by the keys, case insensitively from the data. |
16 | 15 | * |