|
4 | 4 | use Psr\Http\Message\RequestInterface; |
5 | 5 | use Psr\Log\LoggerInterface; |
6 | 6 |
|
7 | | -function iclogger_guzzle_middleware(LoggerInterface $log, $type = 'raw') |
8 | | -{ |
9 | | - return function (callable $handler) use ($log, $type) { |
10 | | - return function (RequestInterface $request, array $options) use ($handler, $log, $type) { |
11 | | - $method = (string) $request->getMethod(); |
12 | | - $uri = (string) $request->getUri(); |
13 | | - $body = (string) $request->getBody(); |
14 | | - |
15 | | - if (empty($body)) { |
16 | | - $message = "[{$method}] Calling `{$uri}`."; |
17 | | - $context = []; |
18 | | - } else { |
19 | | - $message = "[{$method}] Calling `{$uri}` with body:"; |
20 | | - switch ($type) { |
21 | | - case 'json': |
22 | | - $context = json_decode($body, true); |
23 | | - break; |
24 | | - |
25 | | - case 'raw': |
26 | | - default: |
27 | | - $message .= "\n{$body}"; |
28 | | - $context = []; |
29 | | - break; |
30 | | - } |
31 | | - } |
32 | | - $log->info($message, $context); |
33 | | - |
34 | | - return $handler($request, $options)->then( |
35 | | - function ($response) use ($log, $type) { |
36 | | - $body = (string) $response->getBody(); |
37 | | - $code = $response->getStatusCode(); |
38 | | - |
39 | | - $message = "[{$code}] Response:"; |
| 7 | +if (!function_exists('iclogger_guzzle_middleware')) { |
| 8 | + function iclogger_guzzle_middleware(LoggerInterface $log, $type = 'raw') |
| 9 | + { |
| 10 | + return function (callable $handler) use ($log, $type) { |
| 11 | + return function (RequestInterface $request, array $options) use ($handler, $log, $type) { |
| 12 | + $method = (string) $request->getMethod(); |
| 13 | + $uri = (string) $request->getUri(); |
| 14 | + $body = (string) $request->getBody(); |
| 15 | + |
| 16 | + if (empty($body)) { |
| 17 | + $message = "[{$method}] Calling `{$uri}`."; |
| 18 | + $context = []; |
| 19 | + } else { |
| 20 | + $message = "[{$method}] Calling `{$uri}` with body:"; |
40 | 21 | switch ($type) { |
41 | 22 | case 'json': |
42 | | - $context = is_json($body, true); |
43 | | - if (empty($context)) { |
44 | | - throw new RuntimeException('Bad response, json expected.', ['response' => $body]); |
45 | | - } |
| 23 | + $context = json_decode($body, true); |
46 | 24 | break; |
47 | | - |
| 25 | + |
48 | 26 | case 'raw': |
49 | 27 | default: |
50 | 28 | $message .= "\n{$body}"; |
51 | 29 | $context = []; |
52 | 30 | break; |
53 | 31 | } |
54 | | - if (!empty($context)) { |
55 | | - $response->iclParsedBody = $context; |
56 | | - } |
57 | | - |
58 | | - $log->info($message, $context); |
59 | | - |
60 | | - return $response; |
61 | | - }, |
62 | | - function ($reason) { |
63 | | - return \GuzzleHttp\Promise\rejection_for($reason); |
64 | 32 | } |
65 | | - ); |
| 33 | + $log->info($message, $context); |
| 34 | + |
| 35 | + return $handler($request, $options)->then( |
| 36 | + function ($response) use ($log, $type) { |
| 37 | + $body = (string) $response->getBody(); |
| 38 | + $code = $response->getStatusCode(); |
| 39 | + |
| 40 | + $message = "[{$code}] Response:"; |
| 41 | + switch ($type) { |
| 42 | + case 'json': |
| 43 | + $context = is_json($body, true); |
| 44 | + if (empty($context)) { |
| 45 | + throw new RuntimeException('Bad response, json expected.', ['response' => $body]); |
| 46 | + } |
| 47 | + break; |
| 48 | + |
| 49 | + case 'raw': |
| 50 | + default: |
| 51 | + $message .= "\n{$body}"; |
| 52 | + $context = []; |
| 53 | + break; |
| 54 | + } |
| 55 | + if (!empty($context)) { |
| 56 | + $response->iclParsedBody = $context; |
| 57 | + } |
| 58 | + |
| 59 | + $log->info($message, $context); |
| 60 | + |
| 61 | + return $response; |
| 62 | + }, |
| 63 | + function ($reason) { |
| 64 | + return \GuzzleHttp\Promise\rejection_for($reason); |
| 65 | + } |
| 66 | + ); |
| 67 | + }; |
66 | 68 | }; |
67 | | - }; |
| 69 | + } |
68 | 70 | } |
0 commit comments