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