1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Http\Client\Plugin; |
4
|
|
|
|
5
|
|
|
use Http\Promise\Promise; |
6
|
|
|
use Psr\Http\Message\RequestInterface; |
7
|
|
|
use Psr\Http\Message\UriInterface; |
8
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolver; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Allow to modify the the schema and the host of an existing request. |
12
|
|
|
* |
13
|
|
|
* @author Tobias Nyholm <[email protected]> |
14
|
|
|
*/ |
15
|
|
|
class BaseHostPlugin implements Plugin |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @var UriInterface |
19
|
|
|
*/ |
20
|
|
|
private $baseUri; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var bool |
24
|
|
|
*/ |
25
|
|
|
private $replace; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Available options for $config are: |
29
|
|
|
* - replace: bool True will replace all hosts, false will only add host when none is specified. |
30
|
|
|
* |
31
|
|
|
* @param UriInterface $baseUri |
32
|
|
|
* @param bool $replace |
|
|
|
|
33
|
|
|
*/ |
34
|
|
|
public function __construct(UriInterface $baseUri, array $config = []) |
35
|
|
|
{ |
36
|
|
|
$this->baseUri = $baseUri; |
37
|
|
|
|
38
|
|
|
$resolver = new OptionsResolver(); |
39
|
|
|
$this->configureOptions($resolver); |
40
|
|
|
$options = $resolver->resolve($config); |
41
|
|
|
|
42
|
|
|
$this->replace = $options['replace']; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* {@inheritdoc} |
47
|
|
|
*/ |
48
|
|
|
public function handleRequest(RequestInterface $request, callable $next, callable $first) |
49
|
|
|
{ |
50
|
|
|
if ($this->replace || $request->getUri()->getHost() === '') { |
51
|
|
|
$uri = $request->getUri()->withHost($this->baseUri->getHost()); |
52
|
|
|
$uri = $uri->withScheme($this->baseUri->getScheme()); |
53
|
|
|
|
54
|
|
|
$request = $request->withUri($uri); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
return $next($request); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param OptionsResolver $resolver |
62
|
|
|
*/ |
63
|
|
|
private function configureOptions(OptionsResolver $resolver) |
64
|
|
|
{ |
65
|
|
|
$resolver->setDefaults([ |
66
|
|
|
'replace' => false, |
67
|
|
|
]); |
68
|
|
|
$resolver->setAllowedTypes('replace', 'bool'); |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.