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