1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the Micro framework package. |
7
|
|
|
* |
8
|
|
|
* (c) Stanislau Komar <[email protected]> |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
11
|
|
|
* file that was distributed with this source code. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Micro\Plugin\Http\Business\Logger\Formatter\Format; |
15
|
|
|
|
16
|
|
|
use Symfony\Component\HttpFoundation\Request; |
17
|
|
|
use Symfony\Component\HttpFoundation\Response; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @author Stanislau Komar <[email protected]> |
21
|
|
|
*/ |
22
|
|
|
class HeadersRequestFormatter extends AbstractHeaderFormatter |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* @var string[] |
26
|
|
|
*/ |
27
|
|
|
private readonly array $securedHeadersList; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @param string[] $securedHeadersList |
31
|
|
|
*/ |
32
|
3 |
|
public function __construct( |
33
|
|
|
array $securedHeadersList |
34
|
|
|
) { |
35
|
3 |
|
$this->securedHeadersList = array_map('mb_strtolower', $securedHeadersList); |
|
|
|
|
36
|
|
|
} |
37
|
|
|
|
38
|
3 |
|
protected function getPrefix(): string |
39
|
|
|
{ |
40
|
3 |
|
return 'request'; |
41
|
|
|
} |
42
|
|
|
|
43
|
3 |
|
protected function getValue(Request $request, ?Response $response, string $matchedVar): string |
44
|
|
|
{ |
45
|
3 |
|
if (!$matchedVar) { |
46
|
1 |
|
return ''; |
47
|
|
|
} |
48
|
2 |
|
$isSingleVar = true; |
49
|
2 |
|
if ('*' === $matchedVar) { |
50
|
1 |
|
$headersArray = $request->headers->all(); |
51
|
1 |
|
$isSingleVar = false; |
52
|
|
|
} else { |
53
|
1 |
|
$tmpHeaderValue = $request->headers->get($matchedVar); |
54
|
|
|
|
55
|
1 |
|
$headersArray = [$matchedVar => $tmpHeaderValue]; |
56
|
|
|
} |
57
|
|
|
|
58
|
2 |
|
$headersArray = array_change_key_case($headersArray); |
59
|
2 |
|
foreach ($this->securedHeadersList as $securedHeader) { |
60
|
2 |
|
if (!\array_key_exists($securedHeader, $headersArray)) { |
61
|
1 |
|
continue; |
62
|
|
|
} |
63
|
|
|
/** @var string[] $securedHeaderValue */ |
64
|
1 |
|
$securedHeaderValue = $headersArray[$securedHeader]; |
65
|
|
|
|
66
|
1 |
|
$headersArray[$securedHeader] = array_map(fn (mixed $value) => '** Secured **', $securedHeaderValue); |
|
|
|
|
67
|
|
|
} |
68
|
|
|
|
69
|
2 |
|
if ($isSingleVar) { |
70
|
1 |
|
$headersArray = array_values($headersArray); |
71
|
1 |
|
if (1 === \count($headersArray)) { |
72
|
1 |
|
$tmpValue = $headersArray[0]; |
73
|
1 |
|
if (!\is_array($tmpValue)) { |
74
|
1 |
|
return (string) $tmpValue; |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|
79
|
1 |
|
return (string) json_encode($headersArray); |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|