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