1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Http\HttplugBundle\Collector; |
6
|
|
|
|
7
|
|
|
use Http\Client\Common\Plugin; |
8
|
|
|
use Http\Client\Common\PluginClient; |
9
|
|
|
use Http\Client\HttpAsyncClient; |
10
|
|
|
use Http\Client\HttpClient; |
11
|
|
|
use Psr\Http\Client\ClientInterface; |
12
|
|
|
use Symfony\Component\Stopwatch\Stopwatch; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* This factory is used as a replacement for Http\Client\Common\PluginClientFactory when profiling is enabled. It |
16
|
|
|
* creates PluginClient instances with all profiling decorators and extra plugins. |
17
|
|
|
* |
18
|
|
|
* @author Fabien Bourigault <[email protected]> |
19
|
|
|
* |
20
|
|
|
* @internal |
21
|
|
|
*/ |
22
|
|
|
final class PluginClientFactory |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* @var Collector |
26
|
|
|
*/ |
27
|
|
|
private $collector; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var Formatter |
31
|
|
|
*/ |
32
|
|
|
private $formatter; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var Stopwatch |
36
|
|
|
*/ |
37
|
|
|
private $stopwatch; |
38
|
|
|
|
39
|
8 |
|
public function __construct(Collector $collector, Formatter $formatter, Stopwatch $stopwatch) |
40
|
|
|
{ |
41
|
8 |
|
$this->collector = $collector; |
42
|
8 |
|
$this->formatter = $formatter; |
43
|
8 |
|
$this->stopwatch = $stopwatch; |
44
|
8 |
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param HttpClient|ClientInterface|HttpAsyncClient $client |
48
|
|
|
* @param Plugin[] $plugins |
49
|
|
|
* @param array $options { |
50
|
|
|
* |
51
|
|
|
* @var string $client_name to give client a name which may be used when displaying client information like in |
52
|
|
|
* the HTTPlugBundle profiler. |
53
|
|
|
* } |
54
|
|
|
* |
55
|
|
|
* @see PluginClient constructor for PluginClient specific $options. |
56
|
|
|
* |
57
|
|
|
* @return PluginClient |
58
|
|
|
*/ |
59
|
5 |
|
public function createClient($client, array $plugins = [], array $options = []) |
60
|
|
|
{ |
61
|
|
|
$plugins = array_map(function (Plugin $plugin) { |
62
|
5 |
|
return new ProfilePlugin($plugin, $this->collector, $this->formatter); |
63
|
5 |
|
}, $plugins); |
64
|
|
|
|
65
|
5 |
|
$clientName = isset($options['client_name']) ? $options['client_name'] : 'Default'; |
66
|
5 |
|
array_unshift($plugins, new StackPlugin($this->collector, $this->formatter, $clientName)); |
67
|
5 |
|
unset($options['client_name']); |
68
|
|
|
|
69
|
5 |
|
if (!$client instanceof ProfileClient) { |
70
|
2 |
|
$client = new ProfileClient($client, $this->collector, $this->formatter, $this->stopwatch); |
|
|
|
|
71
|
|
|
} |
72
|
|
|
|
73
|
5 |
|
return new PluginClient($client, $plugins, $options); |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.