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