@@ -6,35 +6,35 @@ |
||
6 | 6 | |
7 | 7 | final class Is |
8 | 8 | { |
9 | - /** |
|
10 | - * Returns true if a promise is pending. |
|
11 | - */ |
|
12 | - public static function pending(PromiseInterface $promise): bool |
|
13 | - { |
|
14 | - return $promise->getState() === PromiseInterface::PENDING; |
|
15 | - } |
|
9 | + /** |
|
10 | + * Returns true if a promise is pending. |
|
11 | + */ |
|
12 | + public static function pending(PromiseInterface $promise): bool |
|
13 | + { |
|
14 | + return $promise->getState() === PromiseInterface::PENDING; |
|
15 | + } |
|
16 | 16 | |
17 | - /** |
|
18 | - * Returns true if a promise is fulfilled or rejected. |
|
19 | - */ |
|
20 | - public static function settled(PromiseInterface $promise): bool |
|
21 | - { |
|
22 | - return $promise->getState() !== PromiseInterface::PENDING; |
|
23 | - } |
|
17 | + /** |
|
18 | + * Returns true if a promise is fulfilled or rejected. |
|
19 | + */ |
|
20 | + public static function settled(PromiseInterface $promise): bool |
|
21 | + { |
|
22 | + return $promise->getState() !== PromiseInterface::PENDING; |
|
23 | + } |
|
24 | 24 | |
25 | - /** |
|
26 | - * Returns true if a promise is fulfilled. |
|
27 | - */ |
|
28 | - public static function fulfilled(PromiseInterface $promise): bool |
|
29 | - { |
|
30 | - return $promise->getState() === PromiseInterface::FULFILLED; |
|
31 | - } |
|
25 | + /** |
|
26 | + * Returns true if a promise is fulfilled. |
|
27 | + */ |
|
28 | + public static function fulfilled(PromiseInterface $promise): bool |
|
29 | + { |
|
30 | + return $promise->getState() === PromiseInterface::FULFILLED; |
|
31 | + } |
|
32 | 32 | |
33 | - /** |
|
34 | - * Returns true if a promise is rejected. |
|
35 | - */ |
|
36 | - public static function rejected(PromiseInterface $promise): bool |
|
37 | - { |
|
38 | - return $promise->getState() === PromiseInterface::REJECTED; |
|
39 | - } |
|
33 | + /** |
|
34 | + * Returns true if a promise is rejected. |
|
35 | + */ |
|
36 | + public static function rejected(PromiseInterface $promise): bool |
|
37 | + { |
|
38 | + return $promise->getState() === PromiseInterface::REJECTED; |
|
39 | + } |
|
40 | 40 | } |
@@ -4,8 +4,7 @@ |
||
4 | 4 | |
5 | 5 | namespace OCA\FullTextSearch_Elasticsearch\Vendor\GuzzleHttp\Promise; |
6 | 6 | |
7 | -final class Is |
|
8 | -{ |
|
7 | +final class Is { |
|
9 | 8 | /** |
10 | 9 | * Returns true if a promise is pending. |
11 | 10 | */ |
@@ -15,254 +15,254 @@ |
||
15 | 15 | */ |
16 | 16 | final class Middleware |
17 | 17 | { |
18 | - /** |
|
19 | - * Middleware that adds cookies to requests. |
|
20 | - * |
|
21 | - * The options array must be set to a CookieJarInterface in order to use |
|
22 | - * cookies. This is typically handled for you by a client. |
|
23 | - * |
|
24 | - * @return callable Returns a function that accepts the next handler. |
|
25 | - */ |
|
26 | - public static function cookies(): callable |
|
27 | - { |
|
28 | - return static function (callable $handler): callable { |
|
29 | - return static function ($request, array $options) use ($handler) { |
|
30 | - if (empty($options['cookies'])) { |
|
31 | - return $handler($request, $options); |
|
32 | - } elseif (!($options['cookies'] instanceof CookieJarInterface)) { |
|
33 | - throw new \InvalidArgumentException('cookies must be an instance of OCA\FullTextSearch_Elasticsearch\Vendor\GuzzleHttp\Cookie\CookieJarInterface'); |
|
34 | - } |
|
35 | - $cookieJar = $options['cookies']; |
|
36 | - $request = $cookieJar->withCookieHeader($request); |
|
18 | + /** |
|
19 | + * Middleware that adds cookies to requests. |
|
20 | + * |
|
21 | + * The options array must be set to a CookieJarInterface in order to use |
|
22 | + * cookies. This is typically handled for you by a client. |
|
23 | + * |
|
24 | + * @return callable Returns a function that accepts the next handler. |
|
25 | + */ |
|
26 | + public static function cookies(): callable |
|
27 | + { |
|
28 | + return static function (callable $handler): callable { |
|
29 | + return static function ($request, array $options) use ($handler) { |
|
30 | + if (empty($options['cookies'])) { |
|
31 | + return $handler($request, $options); |
|
32 | + } elseif (!($options['cookies'] instanceof CookieJarInterface)) { |
|
33 | + throw new \InvalidArgumentException('cookies must be an instance of OCA\FullTextSearch_Elasticsearch\Vendor\GuzzleHttp\Cookie\CookieJarInterface'); |
|
34 | + } |
|
35 | + $cookieJar = $options['cookies']; |
|
36 | + $request = $cookieJar->withCookieHeader($request); |
|
37 | 37 | |
38 | - return $handler($request, $options) |
|
39 | - ->then( |
|
40 | - static function (ResponseInterface $response) use ($cookieJar, $request): ResponseInterface { |
|
41 | - $cookieJar->extractCookies($request, $response); |
|
38 | + return $handler($request, $options) |
|
39 | + ->then( |
|
40 | + static function (ResponseInterface $response) use ($cookieJar, $request): ResponseInterface { |
|
41 | + $cookieJar->extractCookies($request, $response); |
|
42 | 42 | |
43 | - return $response; |
|
44 | - } |
|
45 | - ); |
|
46 | - }; |
|
47 | - }; |
|
48 | - } |
|
43 | + return $response; |
|
44 | + } |
|
45 | + ); |
|
46 | + }; |
|
47 | + }; |
|
48 | + } |
|
49 | 49 | |
50 | - /** |
|
51 | - * Middleware that throws exceptions for 4xx or 5xx responses when the |
|
52 | - * "http_errors" request option is set to true. |
|
53 | - * |
|
54 | - * @param BodySummarizerInterface|null $bodySummarizer The body summarizer to use in exception messages. |
|
55 | - * |
|
56 | - * @return callable(callable): callable Returns a function that accepts the next handler. |
|
57 | - */ |
|
58 | - public static function httpErrors(BodySummarizerInterface $bodySummarizer = null): callable |
|
59 | - { |
|
60 | - return static function (callable $handler) use ($bodySummarizer): callable { |
|
61 | - return static function ($request, array $options) use ($handler, $bodySummarizer) { |
|
62 | - if (empty($options['http_errors'])) { |
|
63 | - return $handler($request, $options); |
|
64 | - } |
|
50 | + /** |
|
51 | + * Middleware that throws exceptions for 4xx or 5xx responses when the |
|
52 | + * "http_errors" request option is set to true. |
|
53 | + * |
|
54 | + * @param BodySummarizerInterface|null $bodySummarizer The body summarizer to use in exception messages. |
|
55 | + * |
|
56 | + * @return callable(callable): callable Returns a function that accepts the next handler. |
|
57 | + */ |
|
58 | + public static function httpErrors(BodySummarizerInterface $bodySummarizer = null): callable |
|
59 | + { |
|
60 | + return static function (callable $handler) use ($bodySummarizer): callable { |
|
61 | + return static function ($request, array $options) use ($handler, $bodySummarizer) { |
|
62 | + if (empty($options['http_errors'])) { |
|
63 | + return $handler($request, $options); |
|
64 | + } |
|
65 | 65 | |
66 | - return $handler($request, $options)->then( |
|
67 | - static function (ResponseInterface $response) use ($request, $bodySummarizer) { |
|
68 | - $code = $response->getStatusCode(); |
|
69 | - if ($code < 400) { |
|
70 | - return $response; |
|
71 | - } |
|
72 | - throw RequestException::create($request, $response, null, [], $bodySummarizer); |
|
73 | - } |
|
74 | - ); |
|
75 | - }; |
|
76 | - }; |
|
77 | - } |
|
66 | + return $handler($request, $options)->then( |
|
67 | + static function (ResponseInterface $response) use ($request, $bodySummarizer) { |
|
68 | + $code = $response->getStatusCode(); |
|
69 | + if ($code < 400) { |
|
70 | + return $response; |
|
71 | + } |
|
72 | + throw RequestException::create($request, $response, null, [], $bodySummarizer); |
|
73 | + } |
|
74 | + ); |
|
75 | + }; |
|
76 | + }; |
|
77 | + } |
|
78 | 78 | |
79 | - /** |
|
80 | - * Middleware that pushes history data to an ArrayAccess container. |
|
81 | - * |
|
82 | - * @param array|\ArrayAccess<int, array> $container Container to hold the history (by reference). |
|
83 | - * |
|
84 | - * @return callable(callable): callable Returns a function that accepts the next handler. |
|
85 | - * |
|
86 | - * @throws \InvalidArgumentException if container is not an array or ArrayAccess. |
|
87 | - */ |
|
88 | - public static function history(&$container): callable |
|
89 | - { |
|
90 | - if (!\is_array($container) && !$container instanceof \ArrayAccess) { |
|
91 | - throw new \InvalidArgumentException('history container must be an array or object implementing ArrayAccess'); |
|
92 | - } |
|
79 | + /** |
|
80 | + * Middleware that pushes history data to an ArrayAccess container. |
|
81 | + * |
|
82 | + * @param array|\ArrayAccess<int, array> $container Container to hold the history (by reference). |
|
83 | + * |
|
84 | + * @return callable(callable): callable Returns a function that accepts the next handler. |
|
85 | + * |
|
86 | + * @throws \InvalidArgumentException if container is not an array or ArrayAccess. |
|
87 | + */ |
|
88 | + public static function history(&$container): callable |
|
89 | + { |
|
90 | + if (!\is_array($container) && !$container instanceof \ArrayAccess) { |
|
91 | + throw new \InvalidArgumentException('history container must be an array or object implementing ArrayAccess'); |
|
92 | + } |
|
93 | 93 | |
94 | - return static function (callable $handler) use (&$container): callable { |
|
95 | - return static function (RequestInterface $request, array $options) use ($handler, &$container) { |
|
96 | - return $handler($request, $options)->then( |
|
97 | - static function ($value) use ($request, &$container, $options) { |
|
98 | - $container[] = [ |
|
99 | - 'request' => $request, |
|
100 | - 'response' => $value, |
|
101 | - 'error' => null, |
|
102 | - 'options' => $options, |
|
103 | - ]; |
|
94 | + return static function (callable $handler) use (&$container): callable { |
|
95 | + return static function (RequestInterface $request, array $options) use ($handler, &$container) { |
|
96 | + return $handler($request, $options)->then( |
|
97 | + static function ($value) use ($request, &$container, $options) { |
|
98 | + $container[] = [ |
|
99 | + 'request' => $request, |
|
100 | + 'response' => $value, |
|
101 | + 'error' => null, |
|
102 | + 'options' => $options, |
|
103 | + ]; |
|
104 | 104 | |
105 | - return $value; |
|
106 | - }, |
|
107 | - static function ($reason) use ($request, &$container, $options) { |
|
108 | - $container[] = [ |
|
109 | - 'request' => $request, |
|
110 | - 'response' => null, |
|
111 | - 'error' => $reason, |
|
112 | - 'options' => $options, |
|
113 | - ]; |
|
105 | + return $value; |
|
106 | + }, |
|
107 | + static function ($reason) use ($request, &$container, $options) { |
|
108 | + $container[] = [ |
|
109 | + 'request' => $request, |
|
110 | + 'response' => null, |
|
111 | + 'error' => $reason, |
|
112 | + 'options' => $options, |
|
113 | + ]; |
|
114 | 114 | |
115 | - return P\Create::rejectionFor($reason); |
|
116 | - } |
|
117 | - ); |
|
118 | - }; |
|
119 | - }; |
|
120 | - } |
|
115 | + return P\Create::rejectionFor($reason); |
|
116 | + } |
|
117 | + ); |
|
118 | + }; |
|
119 | + }; |
|
120 | + } |
|
121 | 121 | |
122 | - /** |
|
123 | - * Middleware that invokes a callback before and after sending a request. |
|
124 | - * |
|
125 | - * The provided listener cannot modify or alter the response. It simply |
|
126 | - * "taps" into the chain to be notified before returning the promise. The |
|
127 | - * before listener accepts a request and options array, and the after |
|
128 | - * listener accepts a request, options array, and response promise. |
|
129 | - * |
|
130 | - * @param callable $before Function to invoke before forwarding the request. |
|
131 | - * @param callable $after Function invoked after forwarding. |
|
132 | - * |
|
133 | - * @return callable Returns a function that accepts the next handler. |
|
134 | - */ |
|
135 | - public static function tap(callable $before = null, callable $after = null): callable |
|
136 | - { |
|
137 | - return static function (callable $handler) use ($before, $after): callable { |
|
138 | - return static function (RequestInterface $request, array $options) use ($handler, $before, $after) { |
|
139 | - if ($before) { |
|
140 | - $before($request, $options); |
|
141 | - } |
|
142 | - $response = $handler($request, $options); |
|
143 | - if ($after) { |
|
144 | - $after($request, $options, $response); |
|
145 | - } |
|
122 | + /** |
|
123 | + * Middleware that invokes a callback before and after sending a request. |
|
124 | + * |
|
125 | + * The provided listener cannot modify or alter the response. It simply |
|
126 | + * "taps" into the chain to be notified before returning the promise. The |
|
127 | + * before listener accepts a request and options array, and the after |
|
128 | + * listener accepts a request, options array, and response promise. |
|
129 | + * |
|
130 | + * @param callable $before Function to invoke before forwarding the request. |
|
131 | + * @param callable $after Function invoked after forwarding. |
|
132 | + * |
|
133 | + * @return callable Returns a function that accepts the next handler. |
|
134 | + */ |
|
135 | + public static function tap(callable $before = null, callable $after = null): callable |
|
136 | + { |
|
137 | + return static function (callable $handler) use ($before, $after): callable { |
|
138 | + return static function (RequestInterface $request, array $options) use ($handler, $before, $after) { |
|
139 | + if ($before) { |
|
140 | + $before($request, $options); |
|
141 | + } |
|
142 | + $response = $handler($request, $options); |
|
143 | + if ($after) { |
|
144 | + $after($request, $options, $response); |
|
145 | + } |
|
146 | 146 | |
147 | - return $response; |
|
148 | - }; |
|
149 | - }; |
|
150 | - } |
|
147 | + return $response; |
|
148 | + }; |
|
149 | + }; |
|
150 | + } |
|
151 | 151 | |
152 | - /** |
|
153 | - * Middleware that handles request redirects. |
|
154 | - * |
|
155 | - * @return callable Returns a function that accepts the next handler. |
|
156 | - */ |
|
157 | - public static function redirect(): callable |
|
158 | - { |
|
159 | - return static function (callable $handler): RedirectMiddleware { |
|
160 | - return new RedirectMiddleware($handler); |
|
161 | - }; |
|
162 | - } |
|
152 | + /** |
|
153 | + * Middleware that handles request redirects. |
|
154 | + * |
|
155 | + * @return callable Returns a function that accepts the next handler. |
|
156 | + */ |
|
157 | + public static function redirect(): callable |
|
158 | + { |
|
159 | + return static function (callable $handler): RedirectMiddleware { |
|
160 | + return new RedirectMiddleware($handler); |
|
161 | + }; |
|
162 | + } |
|
163 | 163 | |
164 | - /** |
|
165 | - * Middleware that retries requests based on the boolean result of |
|
166 | - * invoking the provided "decider" function. |
|
167 | - * |
|
168 | - * If no delay function is provided, a simple implementation of exponential |
|
169 | - * backoff will be utilized. |
|
170 | - * |
|
171 | - * @param callable $decider Function that accepts the number of retries, |
|
172 | - * a request, [response], and [exception] and |
|
173 | - * returns true if the request is to be retried. |
|
174 | - * @param callable $delay Function that accepts the number of retries and |
|
175 | - * returns the number of milliseconds to delay. |
|
176 | - * |
|
177 | - * @return callable Returns a function that accepts the next handler. |
|
178 | - */ |
|
179 | - public static function retry(callable $decider, callable $delay = null): callable |
|
180 | - { |
|
181 | - return static function (callable $handler) use ($decider, $delay): RetryMiddleware { |
|
182 | - return new RetryMiddleware($decider, $handler, $delay); |
|
183 | - }; |
|
184 | - } |
|
164 | + /** |
|
165 | + * Middleware that retries requests based on the boolean result of |
|
166 | + * invoking the provided "decider" function. |
|
167 | + * |
|
168 | + * If no delay function is provided, a simple implementation of exponential |
|
169 | + * backoff will be utilized. |
|
170 | + * |
|
171 | + * @param callable $decider Function that accepts the number of retries, |
|
172 | + * a request, [response], and [exception] and |
|
173 | + * returns true if the request is to be retried. |
|
174 | + * @param callable $delay Function that accepts the number of retries and |
|
175 | + * returns the number of milliseconds to delay. |
|
176 | + * |
|
177 | + * @return callable Returns a function that accepts the next handler. |
|
178 | + */ |
|
179 | + public static function retry(callable $decider, callable $delay = null): callable |
|
180 | + { |
|
181 | + return static function (callable $handler) use ($decider, $delay): RetryMiddleware { |
|
182 | + return new RetryMiddleware($decider, $handler, $delay); |
|
183 | + }; |
|
184 | + } |
|
185 | 185 | |
186 | - /** |
|
187 | - * Middleware that logs requests, responses, and errors using a message |
|
188 | - * formatter. |
|
189 | - * |
|
190 | - * @phpstan-param \Psr\Log\LogLevel::* $logLevel Level at which to log requests. |
|
191 | - * |
|
192 | - * @param LoggerInterface $logger Logs messages. |
|
193 | - * @param MessageFormatterInterface|MessageFormatter $formatter Formatter used to create message strings. |
|
194 | - * @param string $logLevel Level at which to log requests. |
|
195 | - * |
|
196 | - * @return callable Returns a function that accepts the next handler. |
|
197 | - */ |
|
198 | - public static function log(LoggerInterface $logger, $formatter, string $logLevel = 'info'): callable |
|
199 | - { |
|
200 | - // To be compatible with Guzzle 7.1.x we need to allow users to pass a MessageFormatter |
|
201 | - if (!$formatter instanceof MessageFormatter && !$formatter instanceof MessageFormatterInterface) { |
|
202 | - throw new \LogicException(sprintf('Argument 2 to %s::log() must be of type %s', self::class, MessageFormatterInterface::class)); |
|
203 | - } |
|
186 | + /** |
|
187 | + * Middleware that logs requests, responses, and errors using a message |
|
188 | + * formatter. |
|
189 | + * |
|
190 | + * @phpstan-param \Psr\Log\LogLevel::* $logLevel Level at which to log requests. |
|
191 | + * |
|
192 | + * @param LoggerInterface $logger Logs messages. |
|
193 | + * @param MessageFormatterInterface|MessageFormatter $formatter Formatter used to create message strings. |
|
194 | + * @param string $logLevel Level at which to log requests. |
|
195 | + * |
|
196 | + * @return callable Returns a function that accepts the next handler. |
|
197 | + */ |
|
198 | + public static function log(LoggerInterface $logger, $formatter, string $logLevel = 'info'): callable |
|
199 | + { |
|
200 | + // To be compatible with Guzzle 7.1.x we need to allow users to pass a MessageFormatter |
|
201 | + if (!$formatter instanceof MessageFormatter && !$formatter instanceof MessageFormatterInterface) { |
|
202 | + throw new \LogicException(sprintf('Argument 2 to %s::log() must be of type %s', self::class, MessageFormatterInterface::class)); |
|
203 | + } |
|
204 | 204 | |
205 | - return static function (callable $handler) use ($logger, $formatter, $logLevel): callable { |
|
206 | - return static function (RequestInterface $request, array $options = []) use ($handler, $logger, $formatter, $logLevel) { |
|
207 | - return $handler($request, $options)->then( |
|
208 | - static function ($response) use ($logger, $request, $formatter, $logLevel): ResponseInterface { |
|
209 | - $message = $formatter->format($request, $response); |
|
210 | - $logger->log($logLevel, $message); |
|
205 | + return static function (callable $handler) use ($logger, $formatter, $logLevel): callable { |
|
206 | + return static function (RequestInterface $request, array $options = []) use ($handler, $logger, $formatter, $logLevel) { |
|
207 | + return $handler($request, $options)->then( |
|
208 | + static function ($response) use ($logger, $request, $formatter, $logLevel): ResponseInterface { |
|
209 | + $message = $formatter->format($request, $response); |
|
210 | + $logger->log($logLevel, $message); |
|
211 | 211 | |
212 | - return $response; |
|
213 | - }, |
|
214 | - static function ($reason) use ($logger, $request, $formatter): PromiseInterface { |
|
215 | - $response = $reason instanceof RequestException ? $reason->getResponse() : null; |
|
216 | - $message = $formatter->format($request, $response, P\Create::exceptionFor($reason)); |
|
217 | - $logger->error($message); |
|
212 | + return $response; |
|
213 | + }, |
|
214 | + static function ($reason) use ($logger, $request, $formatter): PromiseInterface { |
|
215 | + $response = $reason instanceof RequestException ? $reason->getResponse() : null; |
|
216 | + $message = $formatter->format($request, $response, P\Create::exceptionFor($reason)); |
|
217 | + $logger->error($message); |
|
218 | 218 | |
219 | - return P\Create::rejectionFor($reason); |
|
220 | - } |
|
221 | - ); |
|
222 | - }; |
|
223 | - }; |
|
224 | - } |
|
219 | + return P\Create::rejectionFor($reason); |
|
220 | + } |
|
221 | + ); |
|
222 | + }; |
|
223 | + }; |
|
224 | + } |
|
225 | 225 | |
226 | - /** |
|
227 | - * This middleware adds a default content-type if possible, a default |
|
228 | - * content-length or transfer-encoding header, and the expect header. |
|
229 | - */ |
|
230 | - public static function prepareBody(): callable |
|
231 | - { |
|
232 | - return static function (callable $handler): PrepareBodyMiddleware { |
|
233 | - return new PrepareBodyMiddleware($handler); |
|
234 | - }; |
|
235 | - } |
|
226 | + /** |
|
227 | + * This middleware adds a default content-type if possible, a default |
|
228 | + * content-length or transfer-encoding header, and the expect header. |
|
229 | + */ |
|
230 | + public static function prepareBody(): callable |
|
231 | + { |
|
232 | + return static function (callable $handler): PrepareBodyMiddleware { |
|
233 | + return new PrepareBodyMiddleware($handler); |
|
234 | + }; |
|
235 | + } |
|
236 | 236 | |
237 | - /** |
|
238 | - * Middleware that applies a map function to the request before passing to |
|
239 | - * the next handler. |
|
240 | - * |
|
241 | - * @param callable $fn Function that accepts a RequestInterface and returns |
|
242 | - * a RequestInterface. |
|
243 | - */ |
|
244 | - public static function mapRequest(callable $fn): callable |
|
245 | - { |
|
246 | - return static function (callable $handler) use ($fn): callable { |
|
247 | - return static function (RequestInterface $request, array $options) use ($handler, $fn) { |
|
248 | - return $handler($fn($request), $options); |
|
249 | - }; |
|
250 | - }; |
|
251 | - } |
|
237 | + /** |
|
238 | + * Middleware that applies a map function to the request before passing to |
|
239 | + * the next handler. |
|
240 | + * |
|
241 | + * @param callable $fn Function that accepts a RequestInterface and returns |
|
242 | + * a RequestInterface. |
|
243 | + */ |
|
244 | + public static function mapRequest(callable $fn): callable |
|
245 | + { |
|
246 | + return static function (callable $handler) use ($fn): callable { |
|
247 | + return static function (RequestInterface $request, array $options) use ($handler, $fn) { |
|
248 | + return $handler($fn($request), $options); |
|
249 | + }; |
|
250 | + }; |
|
251 | + } |
|
252 | 252 | |
253 | - /** |
|
254 | - * Middleware that applies a map function to the resolved promise's |
|
255 | - * response. |
|
256 | - * |
|
257 | - * @param callable $fn Function that accepts a ResponseInterface and |
|
258 | - * returns a ResponseInterface. |
|
259 | - */ |
|
260 | - public static function mapResponse(callable $fn): callable |
|
261 | - { |
|
262 | - return static function (callable $handler) use ($fn): callable { |
|
263 | - return static function (RequestInterface $request, array $options) use ($handler, $fn) { |
|
264 | - return $handler($request, $options)->then($fn); |
|
265 | - }; |
|
266 | - }; |
|
267 | - } |
|
253 | + /** |
|
254 | + * Middleware that applies a map function to the resolved promise's |
|
255 | + * response. |
|
256 | + * |
|
257 | + * @param callable $fn Function that accepts a ResponseInterface and |
|
258 | + * returns a ResponseInterface. |
|
259 | + */ |
|
260 | + public static function mapResponse(callable $fn): callable |
|
261 | + { |
|
262 | + return static function (callable $handler) use ($fn): callable { |
|
263 | + return static function (RequestInterface $request, array $options) use ($handler, $fn) { |
|
264 | + return $handler($request, $options)->then($fn); |
|
265 | + }; |
|
266 | + }; |
|
267 | + } |
|
268 | 268 | } |
@@ -25,8 +25,8 @@ discard block |
||
25 | 25 | */ |
26 | 26 | public static function cookies(): callable |
27 | 27 | { |
28 | - return static function (callable $handler): callable { |
|
29 | - return static function ($request, array $options) use ($handler) { |
|
28 | + return static function(callable $handler): callable { |
|
29 | + return static function($request, array $options) use ($handler) { |
|
30 | 30 | if (empty($options['cookies'])) { |
31 | 31 | return $handler($request, $options); |
32 | 32 | } elseif (!($options['cookies'] instanceof CookieJarInterface)) { |
@@ -37,7 +37,7 @@ discard block |
||
37 | 37 | |
38 | 38 | return $handler($request, $options) |
39 | 39 | ->then( |
40 | - static function (ResponseInterface $response) use ($cookieJar, $request): ResponseInterface { |
|
40 | + static function(ResponseInterface $response) use ($cookieJar, $request): ResponseInterface { |
|
41 | 41 | $cookieJar->extractCookies($request, $response); |
42 | 42 | |
43 | 43 | return $response; |
@@ -57,14 +57,14 @@ discard block |
||
57 | 57 | */ |
58 | 58 | public static function httpErrors(BodySummarizerInterface $bodySummarizer = null): callable |
59 | 59 | { |
60 | - return static function (callable $handler) use ($bodySummarizer): callable { |
|
61 | - return static function ($request, array $options) use ($handler, $bodySummarizer) { |
|
60 | + return static function(callable $handler) use ($bodySummarizer): callable { |
|
61 | + return static function($request, array $options) use ($handler, $bodySummarizer) { |
|
62 | 62 | if (empty($options['http_errors'])) { |
63 | 63 | return $handler($request, $options); |
64 | 64 | } |
65 | 65 | |
66 | 66 | return $handler($request, $options)->then( |
67 | - static function (ResponseInterface $response) use ($request, $bodySummarizer) { |
|
67 | + static function(ResponseInterface $response) use ($request, $bodySummarizer) { |
|
68 | 68 | $code = $response->getStatusCode(); |
69 | 69 | if ($code < 400) { |
70 | 70 | return $response; |
@@ -91,10 +91,10 @@ discard block |
||
91 | 91 | throw new \InvalidArgumentException('history container must be an array or object implementing ArrayAccess'); |
92 | 92 | } |
93 | 93 | |
94 | - return static function (callable $handler) use (&$container): callable { |
|
95 | - return static function (RequestInterface $request, array $options) use ($handler, &$container) { |
|
94 | + return static function(callable $handler) use (&$container): callable { |
|
95 | + return static function(RequestInterface $request, array $options) use ($handler, &$container) { |
|
96 | 96 | return $handler($request, $options)->then( |
97 | - static function ($value) use ($request, &$container, $options) { |
|
97 | + static function($value) use ($request, &$container, $options) { |
|
98 | 98 | $container[] = [ |
99 | 99 | 'request' => $request, |
100 | 100 | 'response' => $value, |
@@ -104,7 +104,7 @@ discard block |
||
104 | 104 | |
105 | 105 | return $value; |
106 | 106 | }, |
107 | - static function ($reason) use ($request, &$container, $options) { |
|
107 | + static function($reason) use ($request, &$container, $options) { |
|
108 | 108 | $container[] = [ |
109 | 109 | 'request' => $request, |
110 | 110 | 'response' => null, |
@@ -134,8 +134,8 @@ discard block |
||
134 | 134 | */ |
135 | 135 | public static function tap(callable $before = null, callable $after = null): callable |
136 | 136 | { |
137 | - return static function (callable $handler) use ($before, $after): callable { |
|
138 | - return static function (RequestInterface $request, array $options) use ($handler, $before, $after) { |
|
137 | + return static function(callable $handler) use ($before, $after): callable { |
|
138 | + return static function(RequestInterface $request, array $options) use ($handler, $before, $after) { |
|
139 | 139 | if ($before) { |
140 | 140 | $before($request, $options); |
141 | 141 | } |
@@ -156,7 +156,7 @@ discard block |
||
156 | 156 | */ |
157 | 157 | public static function redirect(): callable |
158 | 158 | { |
159 | - return static function (callable $handler): RedirectMiddleware { |
|
159 | + return static function(callable $handler): RedirectMiddleware { |
|
160 | 160 | return new RedirectMiddleware($handler); |
161 | 161 | }; |
162 | 162 | } |
@@ -178,7 +178,7 @@ discard block |
||
178 | 178 | */ |
179 | 179 | public static function retry(callable $decider, callable $delay = null): callable |
180 | 180 | { |
181 | - return static function (callable $handler) use ($decider, $delay): RetryMiddleware { |
|
181 | + return static function(callable $handler) use ($decider, $delay): RetryMiddleware { |
|
182 | 182 | return new RetryMiddleware($decider, $handler, $delay); |
183 | 183 | }; |
184 | 184 | } |
@@ -202,16 +202,16 @@ discard block |
||
202 | 202 | throw new \LogicException(sprintf('Argument 2 to %s::log() must be of type %s', self::class, MessageFormatterInterface::class)); |
203 | 203 | } |
204 | 204 | |
205 | - return static function (callable $handler) use ($logger, $formatter, $logLevel): callable { |
|
206 | - return static function (RequestInterface $request, array $options = []) use ($handler, $logger, $formatter, $logLevel) { |
|
205 | + return static function(callable $handler) use ($logger, $formatter, $logLevel): callable { |
|
206 | + return static function(RequestInterface $request, array $options = []) use ($handler, $logger, $formatter, $logLevel) { |
|
207 | 207 | return $handler($request, $options)->then( |
208 | - static function ($response) use ($logger, $request, $formatter, $logLevel): ResponseInterface { |
|
208 | + static function($response) use ($logger, $request, $formatter, $logLevel): ResponseInterface { |
|
209 | 209 | $message = $formatter->format($request, $response); |
210 | 210 | $logger->log($logLevel, $message); |
211 | 211 | |
212 | 212 | return $response; |
213 | 213 | }, |
214 | - static function ($reason) use ($logger, $request, $formatter): PromiseInterface { |
|
214 | + static function($reason) use ($logger, $request, $formatter): PromiseInterface { |
|
215 | 215 | $response = $reason instanceof RequestException ? $reason->getResponse() : null; |
216 | 216 | $message = $formatter->format($request, $response, P\Create::exceptionFor($reason)); |
217 | 217 | $logger->error($message); |
@@ -229,7 +229,7 @@ discard block |
||
229 | 229 | */ |
230 | 230 | public static function prepareBody(): callable |
231 | 231 | { |
232 | - return static function (callable $handler): PrepareBodyMiddleware { |
|
232 | + return static function(callable $handler): PrepareBodyMiddleware { |
|
233 | 233 | return new PrepareBodyMiddleware($handler); |
234 | 234 | }; |
235 | 235 | } |
@@ -243,8 +243,8 @@ discard block |
||
243 | 243 | */ |
244 | 244 | public static function mapRequest(callable $fn): callable |
245 | 245 | { |
246 | - return static function (callable $handler) use ($fn): callable { |
|
247 | - return static function (RequestInterface $request, array $options) use ($handler, $fn) { |
|
246 | + return static function(callable $handler) use ($fn): callable { |
|
247 | + return static function(RequestInterface $request, array $options) use ($handler, $fn) { |
|
248 | 248 | return $handler($fn($request), $options); |
249 | 249 | }; |
250 | 250 | }; |
@@ -259,8 +259,8 @@ discard block |
||
259 | 259 | */ |
260 | 260 | public static function mapResponse(callable $fn): callable |
261 | 261 | { |
262 | - return static function (callable $handler) use ($fn): callable { |
|
263 | - return static function (RequestInterface $request, array $options) use ($handler, $fn) { |
|
262 | + return static function(callable $handler) use ($fn): callable { |
|
263 | + return static function(RequestInterface $request, array $options) use ($handler, $fn) { |
|
264 | 264 | return $handler($request, $options)->then($fn); |
265 | 265 | }; |
266 | 266 | }; |
@@ -13,8 +13,7 @@ |
||
13 | 13 | /** |
14 | 14 | * Functions used to create and wrap handlers with handler middleware. |
15 | 15 | */ |
16 | -final class Middleware |
|
17 | -{ |
|
16 | +final class Middleware { |
|
18 | 17 | /** |
19 | 18 | * Middleware that adds cookies to requests. |
20 | 19 | * |
@@ -2,5 +2,5 @@ |
||
2 | 2 | |
3 | 3 | // Don't redefine the functions if included multiple times. |
4 | 4 | if (!\function_exists('OCA\FullTextSearch_Elasticsearch\Vendor\GuzzleHttp\describe_type')) { |
5 | - require __DIR__.'/functions.php'; |
|
5 | + require __DIR__.'/functions.php'; |
|
6 | 6 | } |
@@ -14,262 +14,262 @@ |
||
14 | 14 | */ |
15 | 15 | class HandlerStack |
16 | 16 | { |
17 | - /** |
|
18 | - * @var (callable(RequestInterface, array): PromiseInterface)|null |
|
19 | - */ |
|
20 | - private $handler; |
|
21 | - |
|
22 | - /** |
|
23 | - * @var array{(callable(callable(RequestInterface, array): PromiseInterface): callable), (string|null)}[] |
|
24 | - */ |
|
25 | - private $stack = []; |
|
26 | - |
|
27 | - /** |
|
28 | - * @var (callable(RequestInterface, array): PromiseInterface)|null |
|
29 | - */ |
|
30 | - private $cached; |
|
31 | - |
|
32 | - /** |
|
33 | - * Creates a default handler stack that can be used by clients. |
|
34 | - * |
|
35 | - * The returned handler will wrap the provided handler or use the most |
|
36 | - * appropriate default handler for your system. The returned HandlerStack has |
|
37 | - * support for cookies, redirects, HTTP error exceptions, and preparing a body |
|
38 | - * before sending. |
|
39 | - * |
|
40 | - * The returned handler stack can be passed to a client in the "handler" |
|
41 | - * option. |
|
42 | - * |
|
43 | - * @param (callable(RequestInterface, array): PromiseInterface)|null $handler HTTP handler function to use with the stack. If no |
|
44 | - * handler is provided, the best handler for your |
|
45 | - * system will be utilized. |
|
46 | - */ |
|
47 | - public static function create(callable $handler = null): self |
|
48 | - { |
|
49 | - $stack = new self($handler ?: Utils::chooseHandler()); |
|
50 | - $stack->push(Middleware::httpErrors(), 'http_errors'); |
|
51 | - $stack->push(Middleware::redirect(), 'allow_redirects'); |
|
52 | - $stack->push(Middleware::cookies(), 'cookies'); |
|
53 | - $stack->push(Middleware::prepareBody(), 'prepare_body'); |
|
54 | - |
|
55 | - return $stack; |
|
56 | - } |
|
57 | - |
|
58 | - /** |
|
59 | - * @param (callable(RequestInterface, array): PromiseInterface)|null $handler Underlying HTTP handler. |
|
60 | - */ |
|
61 | - public function __construct(callable $handler = null) |
|
62 | - { |
|
63 | - $this->handler = $handler; |
|
64 | - } |
|
65 | - |
|
66 | - /** |
|
67 | - * Invokes the handler stack as a composed handler |
|
68 | - * |
|
69 | - * @return ResponseInterface|PromiseInterface |
|
70 | - */ |
|
71 | - public function __invoke(RequestInterface $request, array $options) |
|
72 | - { |
|
73 | - $handler = $this->resolve(); |
|
74 | - |
|
75 | - return $handler($request, $options); |
|
76 | - } |
|
77 | - |
|
78 | - /** |
|
79 | - * Dumps a string representation of the stack. |
|
80 | - * |
|
81 | - * @return string |
|
82 | - */ |
|
83 | - public function __toString() |
|
84 | - { |
|
85 | - $depth = 0; |
|
86 | - $stack = []; |
|
87 | - |
|
88 | - if ($this->handler !== null) { |
|
89 | - $stack[] = '0) Handler: '.$this->debugCallable($this->handler); |
|
90 | - } |
|
91 | - |
|
92 | - $result = ''; |
|
93 | - foreach (\array_reverse($this->stack) as $tuple) { |
|
94 | - ++$depth; |
|
95 | - $str = "{$depth}) Name: '{$tuple[1]}', "; |
|
96 | - $str .= 'Function: '.$this->debugCallable($tuple[0]); |
|
97 | - $result = "> {$str}\n{$result}"; |
|
98 | - $stack[] = $str; |
|
99 | - } |
|
100 | - |
|
101 | - foreach (\array_keys($stack) as $k) { |
|
102 | - $result .= "< {$stack[$k]}\n"; |
|
103 | - } |
|
104 | - |
|
105 | - return $result; |
|
106 | - } |
|
107 | - |
|
108 | - /** |
|
109 | - * Set the HTTP handler that actually returns a promise. |
|
110 | - * |
|
111 | - * @param callable(RequestInterface, array): PromiseInterface $handler Accepts a request and array of options and |
|
112 | - * returns a Promise. |
|
113 | - */ |
|
114 | - public function setHandler(callable $handler): void |
|
115 | - { |
|
116 | - $this->handler = $handler; |
|
117 | - $this->cached = null; |
|
118 | - } |
|
119 | - |
|
120 | - /** |
|
121 | - * Returns true if the builder has a handler. |
|
122 | - */ |
|
123 | - public function hasHandler(): bool |
|
124 | - { |
|
125 | - return $this->handler !== null; |
|
126 | - } |
|
127 | - |
|
128 | - /** |
|
129 | - * Unshift a middleware to the bottom of the stack. |
|
130 | - * |
|
131 | - * @param callable(callable): callable $middleware Middleware function |
|
132 | - * @param string $name Name to register for this middleware. |
|
133 | - */ |
|
134 | - public function unshift(callable $middleware, string $name = null): void |
|
135 | - { |
|
136 | - \array_unshift($this->stack, [$middleware, $name]); |
|
137 | - $this->cached = null; |
|
138 | - } |
|
139 | - |
|
140 | - /** |
|
141 | - * Push a middleware to the top of the stack. |
|
142 | - * |
|
143 | - * @param callable(callable): callable $middleware Middleware function |
|
144 | - * @param string $name Name to register for this middleware. |
|
145 | - */ |
|
146 | - public function push(callable $middleware, string $name = ''): void |
|
147 | - { |
|
148 | - $this->stack[] = [$middleware, $name]; |
|
149 | - $this->cached = null; |
|
150 | - } |
|
151 | - |
|
152 | - /** |
|
153 | - * Add a middleware before another middleware by name. |
|
154 | - * |
|
155 | - * @param string $findName Middleware to find |
|
156 | - * @param callable(callable): callable $middleware Middleware function |
|
157 | - * @param string $withName Name to register for this middleware. |
|
158 | - */ |
|
159 | - public function before(string $findName, callable $middleware, string $withName = ''): void |
|
160 | - { |
|
161 | - $this->splice($findName, $withName, $middleware, true); |
|
162 | - } |
|
163 | - |
|
164 | - /** |
|
165 | - * Add a middleware after another middleware by name. |
|
166 | - * |
|
167 | - * @param string $findName Middleware to find |
|
168 | - * @param callable(callable): callable $middleware Middleware function |
|
169 | - * @param string $withName Name to register for this middleware. |
|
170 | - */ |
|
171 | - public function after(string $findName, callable $middleware, string $withName = ''): void |
|
172 | - { |
|
173 | - $this->splice($findName, $withName, $middleware, false); |
|
174 | - } |
|
175 | - |
|
176 | - /** |
|
177 | - * Remove a middleware by instance or name from the stack. |
|
178 | - * |
|
179 | - * @param callable|string $remove Middleware to remove by instance or name. |
|
180 | - */ |
|
181 | - public function remove($remove): void |
|
182 | - { |
|
183 | - if (!is_string($remove) && !is_callable($remove)) { |
|
184 | - trigger_deprecation('guzzlehttp/guzzle', '7.4', 'Not passing a callable or string to %s::%s() is deprecated and will cause an error in 8.0.', __CLASS__, __FUNCTION__); |
|
185 | - } |
|
186 | - |
|
187 | - $this->cached = null; |
|
188 | - $idx = \is_callable($remove) ? 0 : 1; |
|
189 | - $this->stack = \array_values(\array_filter( |
|
190 | - $this->stack, |
|
191 | - static function ($tuple) use ($idx, $remove) { |
|
192 | - return $tuple[$idx] !== $remove; |
|
193 | - } |
|
194 | - )); |
|
195 | - } |
|
196 | - |
|
197 | - /** |
|
198 | - * Compose the middleware and handler into a single callable function. |
|
199 | - * |
|
200 | - * @return callable(RequestInterface, array): PromiseInterface |
|
201 | - */ |
|
202 | - public function resolve(): callable |
|
203 | - { |
|
204 | - if ($this->cached === null) { |
|
205 | - if (($prev = $this->handler) === null) { |
|
206 | - throw new \LogicException('No handler has been specified'); |
|
207 | - } |
|
208 | - |
|
209 | - foreach (\array_reverse($this->stack) as $fn) { |
|
210 | - /** @var callable(RequestInterface, array): PromiseInterface $prev */ |
|
211 | - $prev = $fn[0]($prev); |
|
212 | - } |
|
213 | - |
|
214 | - $this->cached = $prev; |
|
215 | - } |
|
216 | - |
|
217 | - return $this->cached; |
|
218 | - } |
|
219 | - |
|
220 | - private function findByName(string $name): int |
|
221 | - { |
|
222 | - foreach ($this->stack as $k => $v) { |
|
223 | - if ($v[1] === $name) { |
|
224 | - return $k; |
|
225 | - } |
|
226 | - } |
|
227 | - |
|
228 | - throw new \InvalidArgumentException("Middleware not found: $name"); |
|
229 | - } |
|
230 | - |
|
231 | - /** |
|
232 | - * Splices a function into the middleware list at a specific position. |
|
233 | - */ |
|
234 | - private function splice(string $findName, string $withName, callable $middleware, bool $before): void |
|
235 | - { |
|
236 | - $this->cached = null; |
|
237 | - $idx = $this->findByName($findName); |
|
238 | - $tuple = [$middleware, $withName]; |
|
239 | - |
|
240 | - if ($before) { |
|
241 | - if ($idx === 0) { |
|
242 | - \array_unshift($this->stack, $tuple); |
|
243 | - } else { |
|
244 | - $replacement = [$tuple, $this->stack[$idx]]; |
|
245 | - \array_splice($this->stack, $idx, 1, $replacement); |
|
246 | - } |
|
247 | - } elseif ($idx === \count($this->stack) - 1) { |
|
248 | - $this->stack[] = $tuple; |
|
249 | - } else { |
|
250 | - $replacement = [$this->stack[$idx], $tuple]; |
|
251 | - \array_splice($this->stack, $idx, 1, $replacement); |
|
252 | - } |
|
253 | - } |
|
254 | - |
|
255 | - /** |
|
256 | - * Provides a debug string for a given callable. |
|
257 | - * |
|
258 | - * @param callable|string $fn Function to write as a string. |
|
259 | - */ |
|
260 | - private function debugCallable($fn): string |
|
261 | - { |
|
262 | - if (\is_string($fn)) { |
|
263 | - return "callable({$fn})"; |
|
264 | - } |
|
265 | - |
|
266 | - if (\is_array($fn)) { |
|
267 | - return \is_string($fn[0]) |
|
268 | - ? "callable({$fn[0]}::{$fn[1]})" |
|
269 | - : "callable(['".\get_class($fn[0])."', '{$fn[1]}'])"; |
|
270 | - } |
|
271 | - |
|
272 | - /** @var object $fn */ |
|
273 | - return 'callable('.\spl_object_hash($fn).')'; |
|
274 | - } |
|
17 | + /** |
|
18 | + * @var (callable(RequestInterface, array): PromiseInterface)|null |
|
19 | + */ |
|
20 | + private $handler; |
|
21 | + |
|
22 | + /** |
|
23 | + * @var array{(callable(callable(RequestInterface, array): PromiseInterface): callable), (string|null)}[] |
|
24 | + */ |
|
25 | + private $stack = []; |
|
26 | + |
|
27 | + /** |
|
28 | + * @var (callable(RequestInterface, array): PromiseInterface)|null |
|
29 | + */ |
|
30 | + private $cached; |
|
31 | + |
|
32 | + /** |
|
33 | + * Creates a default handler stack that can be used by clients. |
|
34 | + * |
|
35 | + * The returned handler will wrap the provided handler or use the most |
|
36 | + * appropriate default handler for your system. The returned HandlerStack has |
|
37 | + * support for cookies, redirects, HTTP error exceptions, and preparing a body |
|
38 | + * before sending. |
|
39 | + * |
|
40 | + * The returned handler stack can be passed to a client in the "handler" |
|
41 | + * option. |
|
42 | + * |
|
43 | + * @param (callable(RequestInterface, array): PromiseInterface)|null $handler HTTP handler function to use with the stack. If no |
|
44 | + * handler is provided, the best handler for your |
|
45 | + * system will be utilized. |
|
46 | + */ |
|
47 | + public static function create(callable $handler = null): self |
|
48 | + { |
|
49 | + $stack = new self($handler ?: Utils::chooseHandler()); |
|
50 | + $stack->push(Middleware::httpErrors(), 'http_errors'); |
|
51 | + $stack->push(Middleware::redirect(), 'allow_redirects'); |
|
52 | + $stack->push(Middleware::cookies(), 'cookies'); |
|
53 | + $stack->push(Middleware::prepareBody(), 'prepare_body'); |
|
54 | + |
|
55 | + return $stack; |
|
56 | + } |
|
57 | + |
|
58 | + /** |
|
59 | + * @param (callable(RequestInterface, array): PromiseInterface)|null $handler Underlying HTTP handler. |
|
60 | + */ |
|
61 | + public function __construct(callable $handler = null) |
|
62 | + { |
|
63 | + $this->handler = $handler; |
|
64 | + } |
|
65 | + |
|
66 | + /** |
|
67 | + * Invokes the handler stack as a composed handler |
|
68 | + * |
|
69 | + * @return ResponseInterface|PromiseInterface |
|
70 | + */ |
|
71 | + public function __invoke(RequestInterface $request, array $options) |
|
72 | + { |
|
73 | + $handler = $this->resolve(); |
|
74 | + |
|
75 | + return $handler($request, $options); |
|
76 | + } |
|
77 | + |
|
78 | + /** |
|
79 | + * Dumps a string representation of the stack. |
|
80 | + * |
|
81 | + * @return string |
|
82 | + */ |
|
83 | + public function __toString() |
|
84 | + { |
|
85 | + $depth = 0; |
|
86 | + $stack = []; |
|
87 | + |
|
88 | + if ($this->handler !== null) { |
|
89 | + $stack[] = '0) Handler: '.$this->debugCallable($this->handler); |
|
90 | + } |
|
91 | + |
|
92 | + $result = ''; |
|
93 | + foreach (\array_reverse($this->stack) as $tuple) { |
|
94 | + ++$depth; |
|
95 | + $str = "{$depth}) Name: '{$tuple[1]}', "; |
|
96 | + $str .= 'Function: '.$this->debugCallable($tuple[0]); |
|
97 | + $result = "> {$str}\n{$result}"; |
|
98 | + $stack[] = $str; |
|
99 | + } |
|
100 | + |
|
101 | + foreach (\array_keys($stack) as $k) { |
|
102 | + $result .= "< {$stack[$k]}\n"; |
|
103 | + } |
|
104 | + |
|
105 | + return $result; |
|
106 | + } |
|
107 | + |
|
108 | + /** |
|
109 | + * Set the HTTP handler that actually returns a promise. |
|
110 | + * |
|
111 | + * @param callable(RequestInterface, array): PromiseInterface $handler Accepts a request and array of options and |
|
112 | + * returns a Promise. |
|
113 | + */ |
|
114 | + public function setHandler(callable $handler): void |
|
115 | + { |
|
116 | + $this->handler = $handler; |
|
117 | + $this->cached = null; |
|
118 | + } |
|
119 | + |
|
120 | + /** |
|
121 | + * Returns true if the builder has a handler. |
|
122 | + */ |
|
123 | + public function hasHandler(): bool |
|
124 | + { |
|
125 | + return $this->handler !== null; |
|
126 | + } |
|
127 | + |
|
128 | + /** |
|
129 | + * Unshift a middleware to the bottom of the stack. |
|
130 | + * |
|
131 | + * @param callable(callable): callable $middleware Middleware function |
|
132 | + * @param string $name Name to register for this middleware. |
|
133 | + */ |
|
134 | + public function unshift(callable $middleware, string $name = null): void |
|
135 | + { |
|
136 | + \array_unshift($this->stack, [$middleware, $name]); |
|
137 | + $this->cached = null; |
|
138 | + } |
|
139 | + |
|
140 | + /** |
|
141 | + * Push a middleware to the top of the stack. |
|
142 | + * |
|
143 | + * @param callable(callable): callable $middleware Middleware function |
|
144 | + * @param string $name Name to register for this middleware. |
|
145 | + */ |
|
146 | + public function push(callable $middleware, string $name = ''): void |
|
147 | + { |
|
148 | + $this->stack[] = [$middleware, $name]; |
|
149 | + $this->cached = null; |
|
150 | + } |
|
151 | + |
|
152 | + /** |
|
153 | + * Add a middleware before another middleware by name. |
|
154 | + * |
|
155 | + * @param string $findName Middleware to find |
|
156 | + * @param callable(callable): callable $middleware Middleware function |
|
157 | + * @param string $withName Name to register for this middleware. |
|
158 | + */ |
|
159 | + public function before(string $findName, callable $middleware, string $withName = ''): void |
|
160 | + { |
|
161 | + $this->splice($findName, $withName, $middleware, true); |
|
162 | + } |
|
163 | + |
|
164 | + /** |
|
165 | + * Add a middleware after another middleware by name. |
|
166 | + * |
|
167 | + * @param string $findName Middleware to find |
|
168 | + * @param callable(callable): callable $middleware Middleware function |
|
169 | + * @param string $withName Name to register for this middleware. |
|
170 | + */ |
|
171 | + public function after(string $findName, callable $middleware, string $withName = ''): void |
|
172 | + { |
|
173 | + $this->splice($findName, $withName, $middleware, false); |
|
174 | + } |
|
175 | + |
|
176 | + /** |
|
177 | + * Remove a middleware by instance or name from the stack. |
|
178 | + * |
|
179 | + * @param callable|string $remove Middleware to remove by instance or name. |
|
180 | + */ |
|
181 | + public function remove($remove): void |
|
182 | + { |
|
183 | + if (!is_string($remove) && !is_callable($remove)) { |
|
184 | + trigger_deprecation('guzzlehttp/guzzle', '7.4', 'Not passing a callable or string to %s::%s() is deprecated and will cause an error in 8.0.', __CLASS__, __FUNCTION__); |
|
185 | + } |
|
186 | + |
|
187 | + $this->cached = null; |
|
188 | + $idx = \is_callable($remove) ? 0 : 1; |
|
189 | + $this->stack = \array_values(\array_filter( |
|
190 | + $this->stack, |
|
191 | + static function ($tuple) use ($idx, $remove) { |
|
192 | + return $tuple[$idx] !== $remove; |
|
193 | + } |
|
194 | + )); |
|
195 | + } |
|
196 | + |
|
197 | + /** |
|
198 | + * Compose the middleware and handler into a single callable function. |
|
199 | + * |
|
200 | + * @return callable(RequestInterface, array): PromiseInterface |
|
201 | + */ |
|
202 | + public function resolve(): callable |
|
203 | + { |
|
204 | + if ($this->cached === null) { |
|
205 | + if (($prev = $this->handler) === null) { |
|
206 | + throw new \LogicException('No handler has been specified'); |
|
207 | + } |
|
208 | + |
|
209 | + foreach (\array_reverse($this->stack) as $fn) { |
|
210 | + /** @var callable(RequestInterface, array): PromiseInterface $prev */ |
|
211 | + $prev = $fn[0]($prev); |
|
212 | + } |
|
213 | + |
|
214 | + $this->cached = $prev; |
|
215 | + } |
|
216 | + |
|
217 | + return $this->cached; |
|
218 | + } |
|
219 | + |
|
220 | + private function findByName(string $name): int |
|
221 | + { |
|
222 | + foreach ($this->stack as $k => $v) { |
|
223 | + if ($v[1] === $name) { |
|
224 | + return $k; |
|
225 | + } |
|
226 | + } |
|
227 | + |
|
228 | + throw new \InvalidArgumentException("Middleware not found: $name"); |
|
229 | + } |
|
230 | + |
|
231 | + /** |
|
232 | + * Splices a function into the middleware list at a specific position. |
|
233 | + */ |
|
234 | + private function splice(string $findName, string $withName, callable $middleware, bool $before): void |
|
235 | + { |
|
236 | + $this->cached = null; |
|
237 | + $idx = $this->findByName($findName); |
|
238 | + $tuple = [$middleware, $withName]; |
|
239 | + |
|
240 | + if ($before) { |
|
241 | + if ($idx === 0) { |
|
242 | + \array_unshift($this->stack, $tuple); |
|
243 | + } else { |
|
244 | + $replacement = [$tuple, $this->stack[$idx]]; |
|
245 | + \array_splice($this->stack, $idx, 1, $replacement); |
|
246 | + } |
|
247 | + } elseif ($idx === \count($this->stack) - 1) { |
|
248 | + $this->stack[] = $tuple; |
|
249 | + } else { |
|
250 | + $replacement = [$this->stack[$idx], $tuple]; |
|
251 | + \array_splice($this->stack, $idx, 1, $replacement); |
|
252 | + } |
|
253 | + } |
|
254 | + |
|
255 | + /** |
|
256 | + * Provides a debug string for a given callable. |
|
257 | + * |
|
258 | + * @param callable|string $fn Function to write as a string. |
|
259 | + */ |
|
260 | + private function debugCallable($fn): string |
|
261 | + { |
|
262 | + if (\is_string($fn)) { |
|
263 | + return "callable({$fn})"; |
|
264 | + } |
|
265 | + |
|
266 | + if (\is_array($fn)) { |
|
267 | + return \is_string($fn[0]) |
|
268 | + ? "callable({$fn[0]}::{$fn[1]})" |
|
269 | + : "callable(['".\get_class($fn[0])."', '{$fn[1]}'])"; |
|
270 | + } |
|
271 | + |
|
272 | + /** @var object $fn */ |
|
273 | + return 'callable('.\spl_object_hash($fn).')'; |
|
274 | + } |
|
275 | 275 | } |
@@ -188,7 +188,7 @@ |
||
188 | 188 | $idx = \is_callable($remove) ? 0 : 1; |
189 | 189 | $this->stack = \array_values(\array_filter( |
190 | 190 | $this->stack, |
191 | - static function ($tuple) use ($idx, $remove) { |
|
191 | + static function($tuple) use ($idx, $remove) { |
|
192 | 192 | return $tuple[$idx] !== $remove; |
193 | 193 | } |
194 | 194 | )); |
@@ -12,8 +12,7 @@ |
||
12 | 12 | * |
13 | 13 | * @final |
14 | 14 | */ |
15 | -class HandlerStack |
|
16 | -{ |
|
15 | +class HandlerStack { |
|
17 | 16 | /** |
18 | 17 | * @var (callable(RequestInterface, array): PromiseInterface)|null |
19 | 18 | */ |
@@ -12,122 +12,122 @@ |
||
12 | 12 | */ |
13 | 13 | final class TransferStats |
14 | 14 | { |
15 | - /** |
|
16 | - * @var RequestInterface |
|
17 | - */ |
|
18 | - private $request; |
|
19 | - |
|
20 | - /** |
|
21 | - * @var ResponseInterface|null |
|
22 | - */ |
|
23 | - private $response; |
|
24 | - |
|
25 | - /** |
|
26 | - * @var float|null |
|
27 | - */ |
|
28 | - private $transferTime; |
|
29 | - |
|
30 | - /** |
|
31 | - * @var array |
|
32 | - */ |
|
33 | - private $handlerStats; |
|
34 | - |
|
35 | - /** |
|
36 | - * @var mixed|null |
|
37 | - */ |
|
38 | - private $handlerErrorData; |
|
39 | - |
|
40 | - /** |
|
41 | - * @param RequestInterface $request Request that was sent. |
|
42 | - * @param ResponseInterface|null $response Response received (if any) |
|
43 | - * @param float|null $transferTime Total handler transfer time. |
|
44 | - * @param mixed $handlerErrorData Handler error data. |
|
45 | - * @param array $handlerStats Handler specific stats. |
|
46 | - */ |
|
47 | - public function __construct( |
|
48 | - RequestInterface $request, |
|
49 | - ResponseInterface $response = null, |
|
50 | - float $transferTime = null, |
|
51 | - $handlerErrorData = null, |
|
52 | - array $handlerStats = [] |
|
53 | - ) { |
|
54 | - $this->request = $request; |
|
55 | - $this->response = $response; |
|
56 | - $this->transferTime = $transferTime; |
|
57 | - $this->handlerErrorData = $handlerErrorData; |
|
58 | - $this->handlerStats = $handlerStats; |
|
59 | - } |
|
60 | - |
|
61 | - public function getRequest(): RequestInterface |
|
62 | - { |
|
63 | - return $this->request; |
|
64 | - } |
|
65 | - |
|
66 | - /** |
|
67 | - * Returns the response that was received (if any). |
|
68 | - */ |
|
69 | - public function getResponse(): ?ResponseInterface |
|
70 | - { |
|
71 | - return $this->response; |
|
72 | - } |
|
73 | - |
|
74 | - /** |
|
75 | - * Returns true if a response was received. |
|
76 | - */ |
|
77 | - public function hasResponse(): bool |
|
78 | - { |
|
79 | - return $this->response !== null; |
|
80 | - } |
|
81 | - |
|
82 | - /** |
|
83 | - * Gets handler specific error data. |
|
84 | - * |
|
85 | - * This might be an exception, a integer representing an error code, or |
|
86 | - * anything else. Relying on this value assumes that you know what handler |
|
87 | - * you are using. |
|
88 | - * |
|
89 | - * @return mixed |
|
90 | - */ |
|
91 | - public function getHandlerErrorData() |
|
92 | - { |
|
93 | - return $this->handlerErrorData; |
|
94 | - } |
|
95 | - |
|
96 | - /** |
|
97 | - * Get the effective URI the request was sent to. |
|
98 | - */ |
|
99 | - public function getEffectiveUri(): UriInterface |
|
100 | - { |
|
101 | - return $this->request->getUri(); |
|
102 | - } |
|
103 | - |
|
104 | - /** |
|
105 | - * Get the estimated time the request was being transferred by the handler. |
|
106 | - * |
|
107 | - * @return float|null Time in seconds. |
|
108 | - */ |
|
109 | - public function getTransferTime(): ?float |
|
110 | - { |
|
111 | - return $this->transferTime; |
|
112 | - } |
|
113 | - |
|
114 | - /** |
|
115 | - * Gets an array of all of the handler specific transfer data. |
|
116 | - */ |
|
117 | - public function getHandlerStats(): array |
|
118 | - { |
|
119 | - return $this->handlerStats; |
|
120 | - } |
|
121 | - |
|
122 | - /** |
|
123 | - * Get a specific handler statistic from the handler by name. |
|
124 | - * |
|
125 | - * @param string $stat Handler specific transfer stat to retrieve. |
|
126 | - * |
|
127 | - * @return mixed|null |
|
128 | - */ |
|
129 | - public function getHandlerStat(string $stat) |
|
130 | - { |
|
131 | - return $this->handlerStats[$stat] ?? null; |
|
132 | - } |
|
15 | + /** |
|
16 | + * @var RequestInterface |
|
17 | + */ |
|
18 | + private $request; |
|
19 | + |
|
20 | + /** |
|
21 | + * @var ResponseInterface|null |
|
22 | + */ |
|
23 | + private $response; |
|
24 | + |
|
25 | + /** |
|
26 | + * @var float|null |
|
27 | + */ |
|
28 | + private $transferTime; |
|
29 | + |
|
30 | + /** |
|
31 | + * @var array |
|
32 | + */ |
|
33 | + private $handlerStats; |
|
34 | + |
|
35 | + /** |
|
36 | + * @var mixed|null |
|
37 | + */ |
|
38 | + private $handlerErrorData; |
|
39 | + |
|
40 | + /** |
|
41 | + * @param RequestInterface $request Request that was sent. |
|
42 | + * @param ResponseInterface|null $response Response received (if any) |
|
43 | + * @param float|null $transferTime Total handler transfer time. |
|
44 | + * @param mixed $handlerErrorData Handler error data. |
|
45 | + * @param array $handlerStats Handler specific stats. |
|
46 | + */ |
|
47 | + public function __construct( |
|
48 | + RequestInterface $request, |
|
49 | + ResponseInterface $response = null, |
|
50 | + float $transferTime = null, |
|
51 | + $handlerErrorData = null, |
|
52 | + array $handlerStats = [] |
|
53 | + ) { |
|
54 | + $this->request = $request; |
|
55 | + $this->response = $response; |
|
56 | + $this->transferTime = $transferTime; |
|
57 | + $this->handlerErrorData = $handlerErrorData; |
|
58 | + $this->handlerStats = $handlerStats; |
|
59 | + } |
|
60 | + |
|
61 | + public function getRequest(): RequestInterface |
|
62 | + { |
|
63 | + return $this->request; |
|
64 | + } |
|
65 | + |
|
66 | + /** |
|
67 | + * Returns the response that was received (if any). |
|
68 | + */ |
|
69 | + public function getResponse(): ?ResponseInterface |
|
70 | + { |
|
71 | + return $this->response; |
|
72 | + } |
|
73 | + |
|
74 | + /** |
|
75 | + * Returns true if a response was received. |
|
76 | + */ |
|
77 | + public function hasResponse(): bool |
|
78 | + { |
|
79 | + return $this->response !== null; |
|
80 | + } |
|
81 | + |
|
82 | + /** |
|
83 | + * Gets handler specific error data. |
|
84 | + * |
|
85 | + * This might be an exception, a integer representing an error code, or |
|
86 | + * anything else. Relying on this value assumes that you know what handler |
|
87 | + * you are using. |
|
88 | + * |
|
89 | + * @return mixed |
|
90 | + */ |
|
91 | + public function getHandlerErrorData() |
|
92 | + { |
|
93 | + return $this->handlerErrorData; |
|
94 | + } |
|
95 | + |
|
96 | + /** |
|
97 | + * Get the effective URI the request was sent to. |
|
98 | + */ |
|
99 | + public function getEffectiveUri(): UriInterface |
|
100 | + { |
|
101 | + return $this->request->getUri(); |
|
102 | + } |
|
103 | + |
|
104 | + /** |
|
105 | + * Get the estimated time the request was being transferred by the handler. |
|
106 | + * |
|
107 | + * @return float|null Time in seconds. |
|
108 | + */ |
|
109 | + public function getTransferTime(): ?float |
|
110 | + { |
|
111 | + return $this->transferTime; |
|
112 | + } |
|
113 | + |
|
114 | + /** |
|
115 | + * Gets an array of all of the handler specific transfer data. |
|
116 | + */ |
|
117 | + public function getHandlerStats(): array |
|
118 | + { |
|
119 | + return $this->handlerStats; |
|
120 | + } |
|
121 | + |
|
122 | + /** |
|
123 | + * Get a specific handler statistic from the handler by name. |
|
124 | + * |
|
125 | + * @param string $stat Handler specific transfer stat to retrieve. |
|
126 | + * |
|
127 | + * @return mixed|null |
|
128 | + */ |
|
129 | + public function getHandlerStat(string $stat) |
|
130 | + { |
|
131 | + return $this->handlerStats[$stat] ?? null; |
|
132 | + } |
|
133 | 133 | } |
@@ -10,8 +10,7 @@ |
||
10 | 10 | * Represents data at the point after it was transferred either successfully |
11 | 11 | * or after a network error. |
12 | 12 | */ |
13 | -final class TransferStats |
|
14 | -{ |
|
13 | +final class TransferStats { |
|
15 | 14 | /** |
16 | 15 | * @var RequestInterface |
17 | 16 | */ |