|
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
|
8 |
|
public function __construct(array $options = []) |
|
29
|
|
|
{ |
|
30
|
8 |
|
$resolver = new OptionsResolver(); |
|
31
|
8 |
|
$this->configureOptions($resolver); |
|
32
|
8 |
|
$this->options = $resolver->resolve($options); |
|
33
|
8 |
|
} |
|
34
|
|
|
|
|
35
|
8 |
|
public function name(RequestInterface $request): string |
|
36
|
|
|
{ |
|
37
|
8 |
|
$parts = [$request->getUri()->getHost()]; |
|
38
|
|
|
|
|
39
|
8 |
|
$method = strtoupper($request->getMethod()); |
|
40
|
|
|
|
|
41
|
8 |
|
$parts[] = $method; |
|
42
|
8 |
|
$parts[] = str_replace('/', '_', trim($request->getUri()->getPath(), '/')); |
|
43
|
8 |
|
$parts[] = $this->getHeaderHash($request); |
|
44
|
|
|
|
|
45
|
8 |
|
if ($query = $request->getUri()->getQuery()) { |
|
46
|
3 |
|
$parts[] = $this->hash($query); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
8 |
|
if (\in_array($method, $this->options['hash_body_methods'], true)) { |
|
50
|
2 |
|
$parts[] = $this->hash((string) $request->getBody()); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
8 |
|
return implode('_', array_filter($parts)); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
8 |
|
private function configureOptions(OptionsResolver $resolver): void |
|
57
|
|
|
{ |
|
58
|
8 |
|
$resolver->setDefaults([ |
|
59
|
8 |
|
'hash_headers' => [], |
|
60
|
|
|
'hash_body_methods' => ['PUT', 'POST', 'PATCH'], |
|
61
|
|
|
]); |
|
62
|
|
|
|
|
63
|
8 |
|
$resolver->setAllowedTypes('hash_headers', 'string[]'); |
|
64
|
8 |
|
$resolver->setAllowedTypes('hash_body_methods', 'string[]'); |
|
65
|
|
|
|
|
66
|
8 |
|
$normalizer = function (Options $options, $value) { |
|
67
|
8 |
|
return \is_array($value) ? array_map('strtoupper', $value) : $value; |
|
68
|
8 |
|
}; |
|
69
|
8 |
|
$resolver->setNormalizer('hash_headers', $normalizer); |
|
70
|
8 |
|
$resolver->setNormalizer('hash_body_methods', $normalizer); |
|
71
|
8 |
|
} |
|
72
|
|
|
|
|
73
|
5 |
|
private function hash(string $value): string |
|
74
|
|
|
{ |
|
75
|
5 |
|
return substr(sha1($value), 0, 5); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
8 |
|
private function getHeaderHash(RequestInterface $request): ?string |
|
79
|
|
|
{ |
|
80
|
8 |
|
$headers = []; |
|
81
|
|
|
|
|
82
|
8 |
|
foreach ($this->options['hash_headers'] as $name) { |
|
83
|
2 |
|
if ($request->hasHeader($name)) { |
|
84
|
2 |
|
$headers[] = "$name:".implode(',', $request->getHeader($name)); |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
8 |
|
return empty($headers) ? null : $this->hash(implode(';', $headers)); |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
|