|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Bilfeldt\LaravelHttpClientLogger; |
| 4 | + |
| 5 | +use GuzzleHttp\Psr7\Utils; |
| 6 | +use Illuminate\Support\Arr; |
| 7 | +use Illuminate\Support\Str; |
| 8 | +use Psr\Http\Message\MessageInterface; |
| 9 | +use Psr\Http\Message\RequestInterface; |
| 10 | +use Psr\Http\Message\UriInterface; |
| 11 | + |
| 12 | +class MessageAccessor |
| 13 | +{ |
| 14 | + private array $values; |
| 15 | + private array $queryFilters; |
| 16 | + private array $headersFilters; |
| 17 | + private array $jsonFilters; |
| 18 | + private string $replace; |
| 19 | + |
| 20 | + public function __construct( |
| 21 | + array $jsonFilers = [], |
| 22 | + array $queryFilters = [], |
| 23 | + array $headersFilters = [], |
| 24 | + array $values = [], |
| 25 | + string $replace = '********' |
| 26 | + ) { |
| 27 | + $this->values = $values; |
| 28 | + $this->queryFilters = $queryFilters; |
| 29 | + $this->headersFilters = $headersFilters; |
| 30 | + $this->jsonFilters = $jsonFilers; |
| 31 | + $this->replace = $replace; |
| 32 | + } |
| 33 | + |
| 34 | + public function getUri(RequestInterface $request): UriInterface |
| 35 | + { |
| 36 | + $uri = $request->getUri(); |
| 37 | + parse_str($uri->getQuery(), $query); |
| 38 | + |
| 39 | + return $uri |
| 40 | + ->withUserInfo($this->replace($this->values, $this->replace, $uri->getUserInfo())) |
| 41 | + ->withHost($this->replace($this->values, $this->replace, $uri->getHost())) |
| 42 | + ->withPath($this->replace($this->values, $this->replace, $uri->getPath())) |
| 43 | + ->withQuery(Arr::query($this->replaceParameters($query, $this->queryFilters, $this->values, $this->replace))); |
| 44 | + } |
| 45 | + |
| 46 | + public function getBase(RequestInterface $request): string |
| 47 | + { |
| 48 | + $uri = $this->getUri($request); |
| 49 | + |
| 50 | + $base = ''; |
| 51 | + if ($uri->getScheme()) { |
| 52 | + $base .= $uri->getScheme().'://'; |
| 53 | + } |
| 54 | + if ($uri->getUserInfo()) { |
| 55 | + $base .= $uri->getUserInfo().'@'; |
| 56 | + } |
| 57 | + if ($uri->getHost()) { |
| 58 | + $base .= $uri->getHost(); |
| 59 | + } |
| 60 | + if ($uri->getPort()) { |
| 61 | + $base .= ':'.$uri->getPort(); |
| 62 | + } |
| 63 | + |
| 64 | + return $base; |
| 65 | + } |
| 66 | + |
| 67 | + public function getQuery(RequestInterface $request): array |
| 68 | + { |
| 69 | + parse_str($this->getUri($request)->getQuery(), $query); |
| 70 | + |
| 71 | + return $query; |
| 72 | + } |
| 73 | + |
| 74 | + public function getHeaders(MessageInterface $message): array |
| 75 | + { |
| 76 | + foreach ($this->headersFilters as $headersFilter) { |
| 77 | + if ($message->hasHeader($headersFilter)) { |
| 78 | + $message = $message->withHeader($headersFilter, $this->replace); |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + // Header filter applied above as this is an array with two layers |
| 83 | + return $this->replaceParameters($message->getHeaders(), [], $this->values, $this->replace, false); |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * Determine if the request is JSON. |
| 88 | + * |
| 89 | + * @see vendor/laravel/framework/src/Illuminate/Http/Client/Request.php |
| 90 | + * |
| 91 | + * @param MessageInterface $message |
| 92 | + * |
| 93 | + * @return bool |
| 94 | + */ |
| 95 | + public function isJson(MessageInterface $message): bool |
| 96 | + { |
| 97 | + return $message->hasHeader('Content-Type') && |
| 98 | + Str::contains($message->getHeaderLine('Content-Type'), 'json'); |
| 99 | + } |
| 100 | + |
| 101 | + public function getJson(MessageInterface $message): ?array |
| 102 | + { |
| 103 | + return $this->replaceParameters( |
| 104 | + json_decode($message->getBody()->__toString(), true), |
| 105 | + $this->jsonFilters, |
| 106 | + $this->values, |
| 107 | + $this->replace |
| 108 | + ); |
| 109 | + } |
| 110 | + |
| 111 | + public function getContent(MessageInterface $message): string |
| 112 | + { |
| 113 | + if ($this->isJson($message)) { |
| 114 | + $body = json_encode($this->getJson($message)); |
| 115 | + } else { |
| 116 | + $body = $message->getBody()->__toString(); |
| 117 | + foreach ($this->values as $value) { |
| 118 | + $body = str_replace($value, $this->replace, $body); |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + return $body; |
| 123 | + } |
| 124 | + |
| 125 | + public function filter(MessageInterface $message): MessageInterface |
| 126 | + { |
| 127 | + $body = $this->getContent($message); |
| 128 | + |
| 129 | + foreach ($this->getHeaders($message) as $header => $values) { |
| 130 | + $message = $message->withHeader($header, $values); |
| 131 | + } |
| 132 | + |
| 133 | + return $message->withBody(Utils::streamFor($body)); |
| 134 | + } |
| 135 | + |
| 136 | + protected function replaceParameters(array $array, array $parameters, array $values, string $replace, $strict = true): array |
| 137 | + { |
| 138 | + foreach ($parameters as $parameter) { |
| 139 | + if (data_get($array, $parameter, null)) { |
| 140 | + data_set($array, $parameter, $replace); |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + array_walk_recursive($array, function (&$item) use ($values, $replace, $strict) { |
| 145 | + foreach ($values as $value) { |
| 146 | + if (!$strict && str_contains($item, $value)) { |
| 147 | + $item = str_replace($value, $replace, $item); |
| 148 | + } elseif ($strict && $value === $item) { |
| 149 | + $item = $replace; |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + return $item; |
| 154 | + }); |
| 155 | + |
| 156 | + return $array; |
| 157 | + } |
| 158 | + |
| 159 | + protected function replace($search, $replace, ?string $subject): ?string |
| 160 | + { |
| 161 | + if (is_null($subject)) { |
| 162 | + return null; |
| 163 | + } |
| 164 | + |
| 165 | + return str_replace($search, $replace, $subject); |
| 166 | + } |
| 167 | +} |
0 commit comments