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