PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / 18.4
Yoast SEO – Advanced SEO with real-time guidance and built-in AI v18.4
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 18.4, at vendor_prefixed/guzzlehttp/guzzle/src/Middleware.php

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