1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Http\HttplugBundle\Collector; |
4
|
|
|
|
5
|
|
|
use Http\Client\Common\FlexibleHttpClient; |
6
|
|
|
use Http\Client\HttpAsyncClient; |
7
|
|
|
use Http\Client\HttpClient; |
8
|
|
|
use Http\HttplugBundle\ClientFactory\ClientFactory; |
9
|
|
|
use Symfony\Component\Stopwatch\Stopwatch; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* The ProfileClientFactory decorates any ClientFactory and returns the created client decorated by a ProfileClient. |
13
|
|
|
* |
14
|
|
|
* @author Fabien Bourigault <[email protected]> |
15
|
|
|
* |
16
|
|
|
* @internal |
17
|
|
|
*/ |
18
|
|
|
class ProfileClientFactory implements ClientFactory |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @var ClientFactory|callable |
22
|
|
|
*/ |
23
|
|
|
private $factory; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var Collector |
27
|
|
|
*/ |
28
|
|
|
private $collector; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var Formatter |
32
|
|
|
*/ |
33
|
|
|
private $formatter; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var Stopwatch |
37
|
|
|
*/ |
38
|
|
|
private $stopwatch; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param ClientFactory|callable $factory |
42
|
|
|
* @param Collector $collector |
43
|
|
|
* @param Formatter $formatter |
44
|
|
|
* @param Stopwatch $stopwatch |
45
|
|
|
*/ |
46
|
5 |
|
public function __construct($factory, Collector $collector, Formatter $formatter, Stopwatch $stopwatch) |
47
|
|
|
{ |
48
|
5 |
|
if (!$factory instanceof ClientFactory && !is_callable($factory)) { |
49
|
|
|
throw new \RuntimeException(sprintf('First argument to ProfileClientFactory::__construct must be a "%s" or a callable.', ClientFactory::class)); |
50
|
|
|
} |
51
|
5 |
|
$this->factory = $factory; |
52
|
5 |
|
$this->collector = $collector; |
53
|
5 |
|
$this->formatter = $formatter; |
54
|
5 |
|
$this->stopwatch = $stopwatch; |
55
|
5 |
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* {@inheritdoc} |
59
|
|
|
*/ |
60
|
5 |
|
public function createClient(array $config = []) |
61
|
|
|
{ |
62
|
5 |
|
$client = is_callable($this->factory) ? call_user_func($this->factory, $config) : $this->factory->createClient($config); |
63
|
|
|
|
64
|
5 |
|
if (!($client instanceof HttpClient && $client instanceof HttpAsyncClient)) { |
65
|
2 |
|
$client = new FlexibleHttpClient($client); |
66
|
2 |
|
} |
67
|
|
|
|
68
|
5 |
|
return new ProfileClient($client, $this->collector, $this->formatter, $this->stopwatch); |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|