1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Http\Client\Plugin\Vcr\NamingStrategy; |
6
|
|
|
|
7
|
|
|
use Psr\Http\Message\RequestInterface; |
8
|
|
|
use Symfony\Component\OptionsResolver\Options; |
9
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolver; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Will use the request attributes (hostname, path, headers & body) as filename. |
13
|
|
|
* |
14
|
|
|
* @author Gary PEGEOT <[email protected]> |
15
|
|
|
*/ |
16
|
|
|
class PathNamingStrategy implements NamingStrategyInterface |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var array |
20
|
|
|
*/ |
21
|
|
|
private $options; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @param array $options available options: |
25
|
|
|
* - hash_headers: the list of header names to hash, |
26
|
|
|
* - hash_body_methods: Methods for which the body will be hashed (Default: PUT, POST, PATCH) |
27
|
|
|
*/ |
28
|
|
|
public function __construct(array $options = []) |
29
|
|
|
{ |
30
|
|
|
$resolver = new OptionsResolver(); |
31
|
|
|
$this->configureOptions($resolver); |
32
|
|
|
$this->options = $resolver->resolve($options); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public function name(RequestInterface $request): string |
36
|
|
|
{ |
37
|
|
|
$parts = [$request->getUri()->getHost()]; |
38
|
|
|
|
39
|
|
|
$method = strtoupper($request->getMethod()); |
40
|
|
|
|
41
|
|
|
$parts[] = $method; |
42
|
|
|
$parts[] = str_replace('/', '_', trim($request->getUri()->getPath(), '/')); |
43
|
|
|
$parts[] = $this->getHeaderHash($request); |
44
|
|
|
|
45
|
|
|
if ($query = $request->getUri()->getQuery()) { |
46
|
|
|
$parts[] = $this->hash($query); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
if (\in_array($method, $this->options['hash_body_methods'], true)) { |
50
|
|
|
$parts[] = $this->hash((string) $request->getBody()); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
return implode('_', array_filter($parts)); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
private function configureOptions(OptionsResolver $resolver): void |
57
|
|
|
{ |
58
|
|
|
$resolver->setDefaults([ |
59
|
|
|
'hash_headers' => [], |
60
|
|
|
'hash_body_methods' => ['PUT', 'POST', 'PATCH'], |
61
|
|
|
]); |
62
|
|
|
|
63
|
|
|
$resolver->setAllowedTypes('hash_headers', 'string[]'); |
64
|
|
|
$resolver->setAllowedTypes('hash_body_methods', 'string[]'); |
65
|
|
|
|
66
|
|
|
$normalizer = function (Options $options, $value) { |
67
|
|
|
return \is_array($value) ? array_map('strtoupper', $value) : $value; |
68
|
|
|
}; |
69
|
|
|
$resolver->setNormalizer('hash_headers', $normalizer); |
70
|
|
|
$resolver->setNormalizer('hash_body_methods', $normalizer); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
private function hash(string $value): string |
74
|
|
|
{ |
75
|
|
|
return substr(sha1($value), 0, 5); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
private function getHeaderHash(RequestInterface $request): ?string |
79
|
|
|
{ |
80
|
|
|
$headers = []; |
81
|
|
|
|
82
|
|
|
foreach ($this->options['hash_headers'] as $name) { |
83
|
|
|
if ($request->hasHeader($name)) { |
84
|
|
|
$headers[] = "$name:".implode(',', $request->getHeader($name)); |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
return empty($headers) ? null : $this->hash(implode(';', $headers)); |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|