← All changes
|
includes/sdk/google/guzzlehttp/guzzle/src/Middleware.php
+45
-45
1.2.11
→
1.4.2
View file →
| @@ -1,15 +1,15 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | -namespace Dudlewebs\WPMCS\GuzzleHttp; | |
| 3 | +namespace Dudlewebs\WPMCS\GCP\GuzzleHttp; | |
| 4 | 4 | |
| 5 | -use Dudlewebs\WPMCS\GuzzleHttp\Cookie\CookieJarInterface; | |
| 6 | -use Dudlewebs\WPMCS\GuzzleHttp\Exception\RequestException; | |
| 7 | -use Dudlewebs\WPMCS\GuzzleHttp\Promise as P; | |
| 8 | -use Dudlewebs\WPMCS\GuzzleHttp\Promise\PromiseInterface; | |
| 9 | -use Dudlewebs\WPMCS\Psr\Http\Message\RequestInterface; | |
| 10 | -use Dudlewebs\WPMCS\Psr\Http\Message\ResponseInterface; | |
| 11 | -use Dudlewebs\WPMCS\Psr\Log\LoggerInterface; | |
| 5 | +use Dudlewebs\WPMCS\GCP\GuzzleHttp\Cookie\CookieJarInterface; | |
| 6 | +use Dudlewebs\WPMCS\GCP\GuzzleHttp\Exception\RequestException; | |
| 7 | +use Dudlewebs\WPMCS\GCP\GuzzleHttp\Promise as P; | |
| 8 | +use Dudlewebs\WPMCS\GCP\GuzzleHttp\Promise\PromiseInterface; | |
| 9 | +use Dudlewebs\WPMCS\GCP\Psr\Http\Message\RequestInterface; | |
| 10 | +use Dudlewebs\WPMCS\GCP\Psr\Http\Message\ResponseInterface; | |
| 11 | +use Dudlewebs\WPMCS\GCP\Psr\Log\LoggerInterface; | |
| 12 | 12 | /** |
| 13 | 13 | * Functions used to create and wrap handlers with handler middleware. |
| 14 | 14 | */ |
| 15 | 15 | final class Middleware |
| @@ -21,20 +21,20 @@ | ||
| 21 | 21 | * cookies. This is typically handled for you by a client. |
| 22 | 22 | * |
| 23 | 23 | * @return callable Returns a function that accepts the next handler. |
| 24 | 24 | */ |
| 25 | - public static function cookies(): callable | |
| 25 | + public static function cookies() : callable | |
| 26 | 26 | { |
| 27 | - return static function (callable $handler): callable { | |
| 28 | - return static function ($request, array $options) use ($handler) { | |
| 27 | + return static function (callable $handler) : callable { | |
| 28 | + return static function ($request, array $options) use($handler) { | |
| 29 | 29 | if (empty($options['cookies'])) { |
| 30 | 30 | return $handler($request, $options); |
| 31 | 31 | } elseif (!$options['cookies'] instanceof CookieJarInterface) { |
| 32 | - throw new \InvalidArgumentException('cookies must be an instance of GuzzleHttp\Cookie\CookieJarInterface'); | |
| 32 | + throw new \InvalidArgumentException('Dudlewebs\\WPMCS\\GCP\\cookies must be an instance of GuzzleHttp\\Cookie\\CookieJarInterface'); | |
| 33 | 33 | } |
| 34 | 34 | $cookieJar = $options['cookies']; |
| 35 | 35 | $request = $cookieJar->withCookieHeader($request); |
| 36 | - return $handler($request, $options)->then(static function (ResponseInterface $response) use ($cookieJar, $request): ResponseInterface { | |
| 36 | + return $handler($request, $options)->then(static function (ResponseInterface $response) use($cookieJar, $request) : ResponseInterface { | |
| 37 | 37 | $cookieJar->extractCookies($request, $response); |
| 38 | 38 | return $response; |
| 39 | 39 | }); |
| 40 | 40 | }; |
| @@ -47,16 +47,16 @@ | ||
| 47 | 47 | * @param BodySummarizerInterface|null $bodySummarizer The body summarizer to use in exception messages. |
| 48 | 48 | * |
| 49 | 49 | * @return callable(callable): callable Returns a function that accepts the next handler. |
| 50 | 50 | */ |
| 51 | - public static function httpErrors(?BodySummarizerInterface $bodySummarizer = null): callable | |
| 51 | + public static function httpErrors(?BodySummarizerInterface $bodySummarizer = null) : callable | |
| 52 | 52 | { |
| 53 | - return static function (callable $handler) use ($bodySummarizer): callable { | |
| 54 | - return static function ($request, array $options) use ($handler, $bodySummarizer) { | |
| 53 | + return static function (callable $handler) use($bodySummarizer) : callable { | |
| 54 | + return static function ($request, array $options) use($handler, $bodySummarizer) { | |
| 55 | 55 | if (empty($options['http_errors'])) { |
| 56 | 56 | return $handler($request, $options); |
| 57 | 57 | } |
| 58 | - return $handler($request, $options)->then(static function (ResponseInterface $response) use ($request, $bodySummarizer) { | |
| 58 | + return $handler($request, $options)->then(static function (ResponseInterface $response) use($request, $bodySummarizer) { | |
| 59 | 59 | $code = $response->getStatusCode(); |
| 60 | 60 | if ($code < 400) { |
| 61 | 61 | return $response; |
| 62 | 62 | } |
| @@ -73,19 +73,19 @@ | ||
| 73 | 73 | * @return callable(callable): callable Returns a function that accepts the next handler. |
| 74 | 74 | * |
| 75 | 75 | * @throws \InvalidArgumentException if container is not an array or ArrayAccess. |
| 76 | 76 | */ |
| 77 | - public static function history(&$container): callable | |
| 77 | + public static function history(&$container) : callable | |
| 78 | 78 | { |
| 79 | 79 | if (!\is_array($container) && !$container instanceof \ArrayAccess) { |
| 80 | 80 | throw new \InvalidArgumentException('history container must be an array or object implementing ArrayAccess'); |
| 81 | 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) { | |
| 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 | 85 | $container[] = ['request' => $request, 'response' => $value, 'error' => null, 'options' => $options]; |
| 86 | 86 | return $value; |
| 87 | - }, static function ($reason) use ($request, &$container, $options) { | |
| 87 | + }, static function ($reason) use($request, &$container, $options) { | |
| 88 | 88 | $container[] = ['request' => $request, 'response' => null, 'error' => $reason, 'options' => $options]; |
| 89 | 89 | return P\Create::rejectionFor($reason); |
| 90 | 90 | }); |
| 91 | 91 | }; |
| @@ -103,12 +103,12 @@ | ||
| 103 | 103 | * @param callable $after Function invoked after forwarding. |
| 104 | 104 | * |
| 105 | 105 | * @return callable Returns a function that accepts the next handler. |
| 106 | 106 | */ |
| 107 | - public static function tap(?callable $before = null, ?callable $after = null): callable | |
| 107 | + public static function tap(?callable $before = null, ?callable $after = null) : callable | |
| 108 | 108 | { |
| 109 | - return static function (callable $handler) use ($before, $after): callable { | |
| 110 | - return static function (RequestInterface $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) { | |
| 111 | 111 | if ($before) { |
| 112 | 112 | $before($request, $options); |
| 113 | 113 | } |
| 114 | 114 | $response = $handler($request, $options); |
| @@ -123,11 +123,11 @@ | ||
| 123 | 123 | * Middleware that handles request redirects. |
| 124 | 124 | * |
| 125 | 125 | * @return callable Returns a function that accepts the next handler. |
| 126 | 126 | */ |
| 127 | - public static function redirect(): callable | |
| 127 | + public static function redirect() : callable | |
| 128 | 128 | { |
| 129 | - return static function (callable $handler): RedirectMiddleware { | |
| 129 | + return static function (callable $handler) : RedirectMiddleware { | |
| 130 | 130 | return new RedirectMiddleware($handler); |
| 131 | 131 | }; |
| 132 | 132 | } |
| 133 | 133 | /** |
| @@ -144,11 +144,11 @@ | ||
| 144 | 144 | * returns the number of milliseconds to delay. |
| 145 | 145 | * |
| 146 | 146 | * @return callable Returns a function that accepts the next handler. |
| 147 | 147 | */ |
| 148 | - public static function retry(callable $decider, ?callable $delay = null): callable | |
| 148 | + public static function retry(callable $decider, ?callable $delay = null) : callable | |
| 149 | 149 | { |
| 150 | - return static function (callable $handler) use ($decider, $delay): RetryMiddleware { | |
| 150 | + return static function (callable $handler) use($decider, $delay) : RetryMiddleware { | |
| 151 | 151 | return new RetryMiddleware($decider, $handler, $delay); |
| 152 | 152 | }; |
| 153 | 153 | } |
| 154 | 154 | /** |
| @@ -154,29 +154,29 @@ | ||
| 154 | 154 | /** |
| 155 | 155 | * Middleware that logs requests, responses, and errors using a message |
| 156 | 156 | * formatter. |
| 157 | 157 | * |
| 158 | - * @phpstan-param \Psr\Log\LogLevel::* $logLevel Level at which to log requests. | |
| 159 | - * | |
| 160 | 158 | * @param LoggerInterface $logger Logs messages. |
| 161 | 159 | * @param MessageFormatterInterface|MessageFormatter $formatter Formatter used to create message strings. |
| 162 | 160 | * @param string $logLevel Level at which to log requests. |
| 163 | 161 | * |
| 162 | + * @phpstan-param \Psr\Log\LogLevel::* $logLevel Level at which to log requests. | |
| 163 | + * | |
| 164 | 164 | * @return callable Returns a function that accepts the next handler. |
| 165 | 165 | */ |
| 166 | - public static function log(LoggerInterface $logger, $formatter, string $logLevel = 'info'): callable | |
| 166 | + public static function log(LoggerInterface $logger, $formatter, string $logLevel = 'info') : callable | |
| 167 | 167 | { |
| 168 | 168 | // To be compatible with Guzzle 7.1.x we need to allow users to pass a MessageFormatter |
| 169 | 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)); | |
| 170 | + throw new \LogicException(\sprintf('Argument 2 to %s::log() must be of type %s', self::class, MessageFormatterInterface::class)); | |
| 171 | 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 { | |
| 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 | 175 | $message = $formatter->format($request, $response); |
| 176 | 176 | $logger->log($logLevel, $message); |
| 177 | 177 | return $response; |
| 178 | - }, static function ($reason) use ($logger, $request, $formatter): PromiseInterface { | |
| 178 | + }, static function ($reason) use($logger, $request, $formatter) : PromiseInterface { | |
| 179 | 179 | $response = $reason instanceof RequestException ? $reason->getResponse() : null; |
| 180 | 180 | $message = $formatter->format($request, $response, P\Create::exceptionFor($reason)); |
| 181 | 181 | $logger->error($message); |
| 182 | 182 | return P\Create::rejectionFor($reason); |
| @@ -187,11 +187,11 @@ | ||
| 187 | 187 | /** |
| 188 | 188 | * This middleware adds a default content-type if possible, a default |
| 189 | 189 | * content-length or transfer-encoding header, and the expect header. |
| 190 | 190 | */ |
| 191 | - public static function prepareBody(): callable | |
| 191 | + public static function prepareBody() : callable | |
| 192 | 192 | { |
| 193 | - return static function (callable $handler): PrepareBodyMiddleware { | |
| 193 | + return static function (callable $handler) : PrepareBodyMiddleware { | |
| 194 | 194 | return new PrepareBodyMiddleware($handler); |
| 195 | 195 | }; |
| 196 | 196 | } |
| 197 | 197 | /** |
| @@ -200,12 +200,12 @@ | ||
| 200 | 200 | * |
| 201 | 201 | * @param callable $fn Function that accepts a RequestInterface and returns |
| 202 | 202 | * a RequestInterface. |
| 203 | 203 | */ |
| 204 | - public static function mapRequest(callable $fn): callable | |
| 204 | + public static function mapRequest(callable $fn) : callable | |
| 205 | 205 | { |
| 206 | - return static function (callable $handler) use ($fn): callable { | |
| 207 | - return static function (RequestInterface $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) { | |
| 208 | 208 | return $handler($fn($request), $options); |
| 209 | 209 | }; |
| 210 | 210 | }; |
| 211 | 211 | } |
| @@ -215,12 +215,12 @@ | ||
| 215 | 215 | * |
| 216 | 216 | * @param callable $fn Function that accepts a ResponseInterface and |
| 217 | 217 | * returns a ResponseInterface. |
| 218 | 218 | */ |
| 219 | - public static function mapResponse(callable $fn): callable | |
| 219 | + public static function mapResponse(callable $fn) : callable | |
| 220 | 220 | { |
| 221 | - return static function (callable $handler) use ($fn): callable { | |
| 222 | - return static function (RequestInterface $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) { | |
| 223 | 223 | return $handler($request, $options)->then($fn); |
| 224 | 224 | }; |
| 225 | 225 | }; |
| 226 | 226 | } |