PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / 28.5
Yoast SEO – Advanced SEO with real-time guidance and built-in AI v28.5
28.5 28.4 28.3 28.2 28.1 28.0 27.9 27.8 27.7 27.6 27.5 trunk 18.0 18.1 18.2 18.3 18.4 18.4.1 18.5 18.5.1 18.6 18.7 18.8 18.9 19.0 All 129 releases
wordpress-seo / vendor_prefixed / guzzlehttp / guzzle / src / Middleware.php

Middleware.php in Yoast SEO – Advanced SEO with real-time guidance and built-in AI 28.5, at vendor_prefixed/guzzlehttp/guzzle/src/Middleware.php

228 lines 11.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace YoastSEO_Vendor\GuzzleHttp;
4
5 use YoastSEO_Vendor\GuzzleHttp\Cookie\CookieJarInterface;
6 use YoastSEO_Vendor\GuzzleHttp\Exception\RequestException;
7 use YoastSEO_Vendor\GuzzleHttp\Promise as P;
8 use YoastSEO_Vendor\GuzzleHttp\Promise\PromiseInterface;
9 use YoastSEO_Vendor\Psr\Http\Message\RequestInterface;
10 use YoastSEO_Vendor\Psr\Http\Message\ResponseInterface;
11 use YoastSEO_Vendor\Psr\Log\LoggerInterface;
12 /**
13 * Functions used to create and wrap handlers with handler middleware.
14 */
15 final class Middleware
16 {
17 /**
18 * Middleware that adds cookies to requests.
19 *
20 * The options array must be set to a CookieJarInterface in order to use
21 * cookies. This is typically handled for you by a client.
22 *
23 * @return callable Returns a function that accepts the next handler.
24 */
25 public static function cookies() : callable
26 {
27 return static function (callable $handler) : callable {
28 return static function ($request, array $options) use($handler) {
29 if (empty($options['cookies'])) {
30 return $handler($request, $options);
31 } elseif (!$options['cookies'] instanceof \YoastSEO_Vendor\GuzzleHttp\Cookie\CookieJarInterface) {
32 throw new \InvalidArgumentException('cookies must be an instance of YoastSEO_Vendor\\GuzzleHttp\\Cookie\\CookieJarInterface');
33 }
34 $cookieJar = $options['cookies'];
35 $request = $cookieJar->withCookieHeader($request);
36 return $handler($request, $options)->then(static function (\YoastSEO_Vendor\Psr\Http\Message\ResponseInterface $response) use($cookieJar, $request) : ResponseInterface {
37 $cookieJar->extractCookies($request, $response);
38 return $response;
39 });
40 };
41 };
42 }
43 /**
44 * Middleware that throws exceptions for 4xx or 5xx responses when the
45 * "http_errors" request option is set to true.
46 *
47 * @param BodySummarizerInterface|null $bodySummarizer The body summarizer to use in exception messages.
48 *
49 * @return callable(callable): callable Returns a function that accepts the next handler.
50 */
51 public static function httpErrors(?\YoastSEO_Vendor\GuzzleHttp\BodySummarizerInterface $bodySummarizer = null) : callable
52 {
53 return static function (callable $handler) use($bodySummarizer) : callable {
54 return static function ($request, array $options) use($handler, $bodySummarizer) {
55 if (empty($options['http_errors'])) {
56 return $handler($request, $options);
57 }
58 return $handler($request, $options)->then(static function (\YoastSEO_Vendor\Psr\Http\Message\ResponseInterface $response) use($request, $bodySummarizer) {
59 $code = $response->getStatusCode();
60 if ($code < 400) {
61 return $response;
62 }
63 throw \YoastSEO_Vendor\GuzzleHttp\Exception\RequestException::create($request, $response, null, [], $bodySummarizer);
64 });
65 };
66 };
67 }
68 /**
69 * Middleware that pushes history data to an ArrayAccess container.
70 *
71 * @param array|\ArrayAccess<int, array> $container Container to hold the history (by reference).
72 *
73 * @return callable(callable): callable Returns a function that accepts the next handler.
74 *
75 * @throws \InvalidArgumentException if container is not an array or ArrayAccess.
76 */
77 public static function history(&$container) : callable
78 {
79 if (!\is_array($container) && !$container instanceof \ArrayAccess) {
80 throw new \InvalidArgumentException('history container must be an array or object implementing ArrayAccess');
81 }
82 return static function (callable $handler) use(&$container) : callable {
83 return static function (\YoastSEO_Vendor\Psr\Http\Message\RequestInterface $request, array $options) use($handler, &$container) {
84 return $handler($request, $options)->then(static function ($value) use($request, &$container, $options) {
85 $container[] = ['request' => $request, 'response' => $value, 'error' => null, 'options' => $options];
86 return $value;
87 }, static function ($reason) use($request, &$container, $options) {
88 $container[] = ['request' => $request, 'response' => null, 'error' => $reason, 'options' => $options];
89 return \YoastSEO_Vendor\GuzzleHttp\Promise\Create::rejectionFor($reason);
90 });
91 };
92 };
93 }
94 /**
95 * Middleware that invokes a callback before and after sending a request.
96 *
97 * The provided listener cannot modify or alter the response. It simply
98 * "taps" into the chain to be notified before returning the promise. The
99 * before listener accepts a request and options array, and the after
100 * listener accepts a request, options array, and response promise.
101 *
102 * @param callable $before Function to invoke before forwarding the request.
103 * @param callable $after Function invoked after forwarding.
104 *
105 * @return callable Returns a function that accepts the next handler.
106 */
107 public static function tap(?callable $before = null, ?callable $after = null) : callable
108 {
109 return static function (callable $handler) use($before, $after) : callable {
110 return static function (\YoastSEO_Vendor\Psr\Http\Message\RequestInterface $request, array $options) use($handler, $before, $after) {
111 if ($before) {
112 $before($request, $options);
113 }
114 $response = $handler($request, $options);
115 if ($after) {
116 $after($request, $options, $response);
117 }
118 return $response;
119 };
120 };
121 }
122 /**
123 * Middleware that handles request redirects.
124 *
125 * @return callable Returns a function that accepts the next handler.
126 */
127 public static function redirect() : callable
128 {
129 return static function (callable $handler) : RedirectMiddleware {
130 return new \YoastSEO_Vendor\GuzzleHttp\RedirectMiddleware($handler);
131 };
132 }
133 /**
134 * Middleware that retries requests based on the boolean result of
135 * invoking the provided "decider" function.
136 *
137 * If no delay function is provided, a simple implementation of exponential
138 * backoff will be utilized.
139 *
140 * @param callable $decider Function that accepts the number of retries,
141 * a request, [response], and [exception] and
142 * returns true if the request is to be retried.
143 * @param callable $delay Function that accepts the number of retries and
144 * returns the number of milliseconds to delay.
145 *
146 * @return callable Returns a function that accepts the next handler.
147 */
148 public static function retry(callable $decider, ?callable $delay = null) : callable
149 {
150 return static function (callable $handler) use($decider, $delay) : RetryMiddleware {
151 return new \YoastSEO_Vendor\GuzzleHttp\RetryMiddleware($decider, $handler, $delay);
152 };
153 }
154 /**
155 * Middleware that logs requests, responses, and errors using a message
156 * formatter.
157 *
158 * @param LoggerInterface $logger Logs messages.
159 * @param MessageFormatterInterface|MessageFormatter $formatter Formatter used to create message strings.
160 * @param string $logLevel Level at which to log requests.
161 *
162 * @phpstan-param \Psr\Log\LogLevel::* $logLevel Level at which to log requests.
163 *
164 * @return callable Returns a function that accepts the next handler.
165 */
166 public static function log(\YoastSEO_Vendor\Psr\Log\LoggerInterface $logger, $formatter, string $logLevel = 'info') : callable
167 {
168 // To be compatible with Guzzle 7.1.x we need to allow users to pass a MessageFormatter
169 if (!$formatter instanceof \YoastSEO_Vendor\GuzzleHttp\MessageFormatter && !$formatter instanceof \YoastSEO_Vendor\GuzzleHttp\MessageFormatterInterface) {
170 throw new \LogicException(\sprintf('Argument 2 to %s::log() must be of type %s', self::class, \YoastSEO_Vendor\GuzzleHttp\MessageFormatterInterface::class));
171 }
172 return static function (callable $handler) use($logger, $formatter, $logLevel) : callable {
173 return static function (\YoastSEO_Vendor\Psr\Http\Message\RequestInterface $request, array $options = []) use($handler, $logger, $formatter, $logLevel) {
174 return $handler($request, $options)->then(static function ($response) use($logger, $request, $formatter, $logLevel) : ResponseInterface {
175 $message = $formatter->format($request, $response);
176 $logger->log($logLevel, $message);
177 return $response;
178 }, static function ($reason) use($logger, $request, $formatter) : PromiseInterface {
179 $response = $reason instanceof \YoastSEO_Vendor\GuzzleHttp\Exception\RequestException ? $reason->getResponse() : null;
180 $message = $formatter->format($request, $response, \YoastSEO_Vendor\GuzzleHttp\Promise\Create::exceptionFor($reason));
181 $logger->error($message);
182 return \YoastSEO_Vendor\GuzzleHttp\Promise\Create::rejectionFor($reason);
183 });
184 };
185 };
186 }
187 /**
188 * This middleware adds a default content-type if possible, a default
189 * content-length or transfer-encoding header, and the expect header.
190 */
191 public static function prepareBody() : callable
192 {
193 return static function (callable $handler) : PrepareBodyMiddleware {
194 return new \YoastSEO_Vendor\GuzzleHttp\PrepareBodyMiddleware($handler);
195 };
196 }
197 /**
198 * Middleware that applies a map function to the request before passing to
199 * the next handler.
200 *
201 * @param callable $fn Function that accepts a RequestInterface and returns
202 * a RequestInterface.
203 */
204 public static function mapRequest(callable $fn) : callable
205 {
206 return static function (callable $handler) use($fn) : callable {
207 return static function (\YoastSEO_Vendor\Psr\Http\Message\RequestInterface $request, array $options) use($handler, $fn) {
208 return $handler($fn($request), $options);
209 };
210 };
211 }
212 /**
213 * Middleware that applies a map function to the resolved promise's
214 * response.
215 *
216 * @param callable $fn Function that accepts a ResponseInterface and
217 * returns a ResponseInterface.
218 */
219 public static function mapResponse(callable $fn) : callable
220 {
221 return static function (callable $handler) use($fn) : callable {
222 return static function (\YoastSEO_Vendor\Psr\Http\Message\RequestInterface $request, array $options) use($handler, $fn) {
223 return $handler($request, $options)->then($fn);
224 };
225 };
226 }
227 }
228