|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Http\HttplugBundle\Collector; |
|
4
|
|
|
|
|
5
|
|
|
use Http\Client\Common\FlexibleHttpClient; |
|
6
|
|
|
use Http\Client\Exception\HttpException; |
|
7
|
|
|
use Http\Client\HttpAsyncClient; |
|
8
|
|
|
use Http\Client\HttpClient; |
|
9
|
|
|
use Psr\Http\Message\RequestInterface; |
|
10
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
11
|
|
|
use Symfony\Component\Stopwatch\Stopwatch; |
|
12
|
|
|
use Symfony\Component\Stopwatch\StopwatchEvent; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* The ProfileClient decorates any client that implement both HttpClient and HttpAsyncClient interfaces to gather target |
|
16
|
|
|
* url and response status code. |
|
17
|
|
|
* |
|
18
|
|
|
* @author Fabien Bourigault <[email protected]> |
|
19
|
|
|
* |
|
20
|
|
|
* @internal |
|
21
|
|
|
*/ |
|
22
|
|
|
class ProfileClient implements HttpClient, HttpAsyncClient |
|
23
|
|
|
{ |
|
24
|
|
|
/** |
|
25
|
|
|
* @var HttpClient|HttpAsyncClient |
|
26
|
|
|
*/ |
|
27
|
|
|
private $client; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var Collector |
|
31
|
|
|
*/ |
|
32
|
|
|
private $collector; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @var Formatter |
|
36
|
|
|
*/ |
|
37
|
|
|
private $formatter; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @var Stopwatch |
|
41
|
|
|
*/ |
|
42
|
|
|
private $stopwatch; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @var array |
|
46
|
|
|
*/ |
|
47
|
|
|
private $eventNames = []; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @param HttpClient|HttpAsyncClient $client The client to profile. Client must implement HttpClient or |
|
51
|
|
|
* HttpAsyncClient interface. |
|
52
|
|
|
* @param Collector $collector |
|
53
|
|
|
* @param Formatter $formatter |
|
54
|
|
|
* @param Stopwatch $stopwatch |
|
55
|
|
|
*/ |
|
56
|
19 |
|
public function __construct($client, Collector $collector, Formatter $formatter, Stopwatch $stopwatch) |
|
57
|
|
|
{ |
|
58
|
19 |
|
if (!($client instanceof HttpClient && $client instanceof HttpAsyncClient)) { |
|
59
|
|
|
$client = new FlexibleHttpClient($client); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
19 |
|
$this->client = $client; |
|
63
|
19 |
|
$this->collector = $collector; |
|
64
|
19 |
|
$this->formatter = $formatter; |
|
65
|
19 |
|
$this->stopwatch = $stopwatch; |
|
66
|
19 |
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* {@inheritdoc} |
|
70
|
|
|
*/ |
|
71
|
3 |
|
public function sendAsyncRequest(RequestInterface $request) |
|
72
|
|
|
{ |
|
73
|
3 |
|
$activateStack = true; |
|
74
|
3 |
|
$stack = $this->collector->getActiveStack(); |
|
75
|
3 |
|
if (null === $stack) { |
|
76
|
|
|
//When using a discovered client not wrapped in a PluginClient, we don't have a stack from StackPlugin. So |
|
77
|
|
|
//we create our own stack and activate it! |
|
78
|
|
|
$stack = new Stack('Default', $this->formatter->formatRequest($request)); |
|
79
|
|
|
$this->collector->addStack($stack); |
|
80
|
|
|
$this->collector->activateStack($stack); |
|
81
|
|
|
$activateStack = false; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
3 |
|
$this->collectRequestInformations($request, $stack); |
|
85
|
3 |
|
$event = $this->stopwatch->start($this->getStopwatchEventName($request)); |
|
86
|
|
|
|
|
87
|
3 |
|
$onFulfilled = function (ResponseInterface $response) use ($event, $stack) { |
|
88
|
2 |
|
$this->collectResponseInformations($response, $event, $stack); |
|
89
|
|
|
|
|
90
|
2 |
|
return $response; |
|
91
|
3 |
|
}; |
|
92
|
|
|
|
|
93
|
3 |
|
$onRejected = function (\Exception $exception) use ($event, $stack) { |
|
94
|
1 |
|
$this->collectExceptionInformations($exception, $event, $stack); |
|
95
|
|
|
|
|
96
|
1 |
|
throw $exception; |
|
97
|
3 |
|
}; |
|
98
|
|
|
|
|
99
|
3 |
|
$this->collector->deactivateStack($stack); |
|
100
|
|
|
|
|
101
|
|
|
try { |
|
102
|
3 |
|
return $this->client->sendAsyncRequest($request)->then($onFulfilled, $onRejected); |
|
|
|
|
|
|
103
|
|
|
} finally { |
|
104
|
3 |
|
$event->stop(); |
|
105
|
3 |
|
if ($activateStack) { |
|
106
|
|
|
//We only activate the stack when created by the StackPlugin. |
|
107
|
3 |
|
$this->collector->activateStack($stack); |
|
108
|
|
|
} |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* {@inheritdoc} |
|
114
|
|
|
*/ |
|
115
|
3 |
|
public function sendRequest(RequestInterface $request) |
|
116
|
|
|
{ |
|
117
|
3 |
|
$stack = $this->collector->getActiveStack(); |
|
118
|
3 |
|
if (null === $stack) { |
|
119
|
|
|
//When using a discovered client not wrapped in a PluginClient, we don't have a stack from StackPlugin. So |
|
120
|
|
|
//we create our own stack but don't activate it. |
|
121
|
|
|
$stack = new Stack('Default', $this->formatter->formatRequest($request)); |
|
122
|
|
|
$this->collector->addStack($stack); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
3 |
|
$this->collectRequestInformations($request, $stack); |
|
126
|
3 |
|
$event = $this->stopwatch->start($this->getStopwatchEventName($request)); |
|
127
|
|
|
|
|
128
|
|
|
try { |
|
129
|
3 |
|
$response = $this->client->sendRequest($request); |
|
|
|
|
|
|
130
|
3 |
|
$this->collectResponseInformations($response, $event, $stack); |
|
131
|
|
|
|
|
132
|
3 |
|
return $response; |
|
133
|
|
|
} catch (\Exception $e) { |
|
134
|
|
|
$this->collectExceptionInformations($e, $event, $stack); |
|
135
|
|
|
|
|
136
|
|
|
throw $e; |
|
137
|
|
|
} catch (\Throwable $e) { |
|
|
|
|
|
|
138
|
|
|
$this->collectExceptionInformations($e, $event, $stack); |
|
139
|
|
|
|
|
140
|
|
|
throw $e; |
|
141
|
|
|
} finally { |
|
142
|
3 |
|
$event->stop(); |
|
143
|
|
|
} |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
/** |
|
147
|
|
|
* @param RequestInterface $request |
|
148
|
|
|
* @param Stack $stack |
|
149
|
|
|
*/ |
|
150
|
6 |
|
private function collectRequestInformations(RequestInterface $request, Stack $stack) |
|
151
|
|
|
{ |
|
152
|
6 |
|
$stack->setRequestTarget($request->getRequestTarget()); |
|
153
|
6 |
|
$stack->setRequestMethod($request->getMethod()); |
|
154
|
6 |
|
$stack->setRequestScheme($request->getUri()->getScheme()); |
|
155
|
6 |
|
$stack->setRequestHost($request->getUri()->getHost()); |
|
156
|
6 |
|
$stack->setClientRequest($this->formatter->formatRequest($request)); |
|
157
|
6 |
|
$stack->setCurlCommand($this->formatter->formatAsCurlCommand($request)); |
|
158
|
6 |
|
} |
|
159
|
|
|
|
|
160
|
|
|
/** |
|
161
|
|
|
* @param ResponseInterface $response |
|
162
|
|
|
* @param StopwatchEvent $event |
|
163
|
|
|
* @param Stack $stack |
|
164
|
|
|
*/ |
|
165
|
5 |
|
private function collectResponseInformations(ResponseInterface $response, StopwatchEvent $event, Stack $stack) |
|
166
|
|
|
{ |
|
167
|
5 |
|
$stack->setDuration($event->getDuration()); |
|
168
|
5 |
|
$stack->setResponseCode($response->getStatusCode()); |
|
169
|
5 |
|
$stack->setClientResponse($this->formatter->formatResponse($response)); |
|
170
|
5 |
|
} |
|
171
|
|
|
|
|
172
|
|
|
/** |
|
173
|
|
|
* @param \Exception $exception |
|
174
|
|
|
* @param StopwatchEvent $event |
|
175
|
|
|
* @param Stack $stack |
|
176
|
|
|
*/ |
|
177
|
1 |
|
private function collectExceptionInformations(\Exception $exception, StopwatchEvent $event, Stack $stack) |
|
178
|
|
|
{ |
|
179
|
1 |
|
if ($exception instanceof HttpException) { |
|
180
|
|
|
$this->collectResponseInformations($exception->getResponse(), $event, $stack); |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
1 |
|
$stack->setDuration($event->getDuration()); |
|
184
|
1 |
|
$stack->setClientException($this->formatter->formatException($exception)); |
|
185
|
1 |
|
} |
|
186
|
|
|
|
|
187
|
|
|
/** |
|
188
|
|
|
* Generates the event name. |
|
189
|
|
|
* |
|
190
|
|
|
* @param RequestInterface $request |
|
191
|
|
|
* |
|
192
|
|
|
* @return string |
|
193
|
|
|
*/ |
|
194
|
6 |
|
private function getStopwatchEventName(RequestInterface $request) |
|
195
|
|
|
{ |
|
196
|
6 |
|
$name = sprintf('%s %s', $request->getMethod(), $request->getUri()); |
|
197
|
|
|
|
|
198
|
6 |
|
if (isset($this->eventNames[$name])) { |
|
199
|
|
|
$name .= sprintf(' [#%d]', ++$this->eventNames[$name]); |
|
200
|
|
|
} else { |
|
201
|
6 |
|
$this->eventNames[$name] = 1; |
|
202
|
|
|
} |
|
203
|
|
|
|
|
204
|
6 |
|
return $name; |
|
205
|
|
|
} |
|
206
|
|
|
} |
|
207
|
|
|
|
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: