PluginProbe
Media Cloud Sync / 1.3.11
Media Cloud Sync v1.3.11
1.4.0 1.3.12 1.3.11 1.3.10 trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 1.2.0 1.2.10 1.2.11 1.2.12 1.2.13 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 1.2.9 1.3.0 All 34 releases
media-cloud-sync / includes / sdk / s3 / GuzzleHttp / Middleware.php

Middleware.php in Media Cloud Sync 1.3.11, at includes/sdk/s3/GuzzleHttp/Middleware.php

228 lines 10.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Dudlewebs\WPMCS\s3\GuzzleHttp;
4
5 use Dudlewebs\WPMCS\s3\GuzzleHttp\Cookie\CookieJarInterface;
6 use Dudlewebs\WPMCS\s3\GuzzleHttp\Exception\RequestException;
7 use Dudlewebs\WPMCS\s3\GuzzleHttp\Promise as P;
8 use Dudlewebs\WPMCS\s3\GuzzleHttp\Promise\PromiseInterface;
9 use Dudlewebs\WPMCS\s3\Psr\Http\Message\RequestInterface;
10 use Dudlewebs\WPMCS\s3\Psr\Http\Message\ResponseInterface;
11 use Dudlewebs\WPMCS\s3\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 CookieJarInterface) {
32 throw new \InvalidArgumentException('Dudlewebs\\WPMCS\\s3\\cookies must be an instance of GuzzleHttp\\Cookie\\CookieJarInterface');
33 }
34 $cookieJar = $options['cookies'];
35 $request = $cookieJar->withCookieHeader($request);
36 return $handler($request, $options)->then(static function (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(?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 (ResponseInterface $response) use($request, $bodySummarizer) {
59 $code = $response->getStatusCode();
60 if ($code < 400) {
61 return $response;
62 }
63 throw 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 (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 P\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 (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 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 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(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 MessageFormatter && !$formatter instanceof MessageFormatterInterface) {
170 throw new \LogicException(\sprintf('Argument 2 to %s::log() must be of type %s', self::class, MessageFormatterInterface::class));
171 }
172 return static function (callable $handler) use($logger, $formatter, $logLevel) : callable {
173 return static function (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 RequestException ? $reason->getResponse() : null;
180 $message = $formatter->format($request, $response, P\Create::exceptionFor($reason));
181 $logger->error($message);
182 return P\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 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 (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 (RequestInterface $request, array $options) use($handler, $fn) {
223 return $handler($request, $options)->then($fn);
224 };
225 };
226 }
227 }
228