Skip to content

Commit 73f17dd

Browse files
authored
Merge pull request #146 from fbourigault/profiler-redesign
profiler redesign
2 parents 19ccef2 + 170ca01 commit 73f17dd

File tree

10 files changed

+430
-216
lines changed

10 files changed

+430
-216
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66

77
- The real request method and target url are now displayed in the profiler.
88

9+
### Changed
10+
11+
- The profiler design has been updated.
12+
913
## 1.4.0 - 2017-02-21
1014

1115
### Changed

Collector/ProfileClient.php

Lines changed: 71 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33
namespace Http\HttplugBundle\Collector;
44

55
use Http\Client\Common\FlexibleHttpClient;
6+
use Http\Client\Exception\HttpException;
67
use Http\Client\HttpAsyncClient;
78
use Http\Client\HttpClient;
89
use Psr\Http\Message\RequestInterface;
10+
use Psr\Http\Message\ResponseInterface;
911

1012
/**
1113
* The ProfileClient decorates any client that implement both HttpClient and HttpAsyncClient interfaces to gather target
@@ -27,12 +29,18 @@ class ProfileClient implements HttpClient, HttpAsyncClient
2729
*/
2830
private $collector;
2931

32+
/**
33+
* @var Formatter
34+
*/
35+
private $formatter;
36+
3037
/**
3138
* @param HttpClient|HttpAsyncClient $client The client to profile. Client must implement both HttpClient and
3239
* HttpAsyncClient interfaces.
3340
* @param Collector $collector
41+
* @param Formatter $formatter
3442
*/
35-
public function __construct($client, Collector $collector)
43+
public function __construct($client, Collector $collector, Formatter $formatter)
3644
{
3745
if (!($client instanceof HttpClient && $client instanceof HttpAsyncClient)) {
3846
throw new \RuntimeException(sprintf(
@@ -45,39 +53,94 @@ public function __construct($client, Collector $collector)
4553
}
4654
$this->client = $client;
4755
$this->collector = $collector;
56+
$this->formatter = $formatter;
4857
}
4958

5059
/**
5160
* {@inheritdoc}
5261
*/
5362
public function sendAsyncRequest(RequestInterface $request)
5463
{
55-
$this->collectRequestInformations($request);
64+
$stack = $this->collector->getCurrentStack();
65+
$this->collectRequestInformations($request, $stack);
5666

57-
return $this->client->sendAsyncRequest($request);
67+
return $this->client->sendAsyncRequest($request)->then(function (ResponseInterface $response) use ($stack) {
68+
$this->collectResponseInformations($response, $stack);
69+
70+
return $response;
71+
}, function (\Exception $exception) use ($stack) {
72+
$this->collectExceptionInformations($exception, $stack);
73+
74+
throw $exception;
75+
});
5876
}
5977

6078
/**
6179
* {@inheritdoc}
6280
*/
6381
public function sendRequest(RequestInterface $request)
6482
{
65-
$this->collectRequestInformations($request);
83+
$stack = $this->collector->getCurrentStack();
84+
$this->collectRequestInformations($request, $stack);
85+
86+
try {
87+
$response = $this->client->sendRequest($request);
6688

67-
return $this->client->sendRequest($request);
89+
$this->collectResponseInformations($response, $stack);
90+
91+
return $response;
92+
} catch (\Exception $e) {
93+
$this->collectExceptionInformations($e, $stack);
94+
95+
throw $e;
96+
}
6897
}
6998

7099
/**
71100
* @param RequestInterface $request
101+
* @param Stack|null $stack
72102
*/
73-
private function collectRequestInformations(RequestInterface $request)
103+
private function collectRequestInformations(RequestInterface $request, Stack $stack = null)
74104
{
75-
if (!$stack = $this->collector->getCurrentStack()) {
105+
if (!$stack) {
76106
return;
77107
}
78108

79-
$stack = $this->collector->getCurrentStack();
80109
$stack->setRequestTarget($request->getRequestTarget());
81110
$stack->setRequestMethod($request->getMethod());
111+
$stack->setRequestScheme($request->getUri()->getScheme());
112+
$stack->setRequestHost($request->getUri()->getHost());
113+
$stack->setClientRequest($this->formatter->formatRequest($request));
114+
}
115+
116+
/**
117+
* @param ResponseInterface $response
118+
* @param Stack|null $stack
119+
*/
120+
private function collectResponseInformations(ResponseInterface $response, Stack $stack = null)
121+
{
122+
if (!$stack) {
123+
return;
124+
}
125+
126+
$stack->setResponseCode($response->getStatusCode());
127+
$stack->setClientResponse($this->formatter->formatResponse($response));
128+
}
129+
130+
/**
131+
* @param \Exception $exception
132+
* @param Stack|null $stack
133+
*/
134+
private function collectExceptionInformations(\Exception $exception, Stack $stack = null)
135+
{
136+
if ($exception instanceof HttpException) {
137+
$this->collectResponseInformations($exception->getResponse(), $stack);
138+
}
139+
140+
if (!$stack) {
141+
return;
142+
}
143+
144+
$stack->setClientException($this->formatter->formatException($exception));
82145
}
83146
}

Collector/ProfileClientFactory.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,24 @@ class ProfileClientFactory implements ClientFactory
2626
*/
2727
private $collector;
2828

29+
/**
30+
* @var Formatter
31+
*/
32+
private $formatter;
33+
2934
/**
3035
* @param ClientFactory|callable $factory
3136
* @param Collector $collector
37+
* @param Formatter $formatter
3238
*/
33-
public function __construct($factory, Collector $collector)
39+
public function __construct($factory, Collector $collector, Formatter $formatter)
3440
{
3541
if (!$factory instanceof ClientFactory && !is_callable($factory)) {
3642
throw new \RuntimeException(sprintf('First argument to ProfileClientFactory::__construct must be a "%s" or a callable.', ClientFactory::class));
3743
}
3844
$this->factory = $factory;
3945
$this->collector = $collector;
46+
$this->formatter = $formatter;
4047
}
4148

4249
/**
@@ -50,6 +57,6 @@ public function createClient(array $config = [])
5057
$client = new FlexibleHttpClient($client);
5158
}
5259

53-
return new ProfileClient($client, $this->collector);
60+
return new ProfileClient($client, $this->collector, $this->formatter);
5461
}
5562
}

Collector/Stack.php

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,36 @@ final class Stack
4646
*/
4747
private $requestMethod;
4848

49+
/**
50+
* @var string
51+
*/
52+
private $requestHost;
53+
54+
/**
55+
* @var string
56+
*/
57+
private $requestScheme;
58+
59+
/**
60+
* @var string
61+
*/
62+
private $clientRequest;
63+
64+
/**
65+
* @var string
66+
*/
67+
private $clientResponse;
68+
69+
/**
70+
* @var string
71+
*/
72+
private $clientException;
73+
74+
/**
75+
* @var int
76+
*/
77+
private $responseCode;
78+
4979
/**
5080
* @param string $client
5181
* @param string $request
@@ -151,4 +181,100 @@ public function setRequestMethod($requestMethod)
151181
{
152182
$this->requestMethod = $requestMethod;
153183
}
184+
185+
/**
186+
* @return string
187+
*/
188+
public function getClientRequest()
189+
{
190+
return $this->clientRequest;
191+
}
192+
193+
/**
194+
* @param string $clientRequest
195+
*/
196+
public function setClientRequest($clientRequest)
197+
{
198+
$this->clientRequest = $clientRequest;
199+
}
200+
201+
/**
202+
* @return mixed
203+
*/
204+
public function getClientResponse()
205+
{
206+
return $this->clientResponse;
207+
}
208+
209+
/**
210+
* @param mixed $clientResponse
211+
*/
212+
public function setClientResponse($clientResponse)
213+
{
214+
$this->clientResponse = $clientResponse;
215+
}
216+
217+
/**
218+
* @return string
219+
*/
220+
public function getClientException()
221+
{
222+
return $this->clientException;
223+
}
224+
225+
/**
226+
* @param string $clientException
227+
*/
228+
public function setClientException($clientException)
229+
{
230+
$this->clientException = $clientException;
231+
}
232+
233+
/**
234+
* @return int
235+
*/
236+
public function getResponseCode()
237+
{
238+
return $this->responseCode;
239+
}
240+
241+
/**
242+
* @param int $responseCode
243+
*/
244+
public function setResponseCode($responseCode)
245+
{
246+
$this->responseCode = $responseCode;
247+
}
248+
249+
/**
250+
* @return string
251+
*/
252+
public function getRequestHost()
253+
{
254+
return $this->requestHost;
255+
}
256+
257+
/**
258+
* @param string $requestHost
259+
*/
260+
public function setRequestHost($requestHost)
261+
{
262+
$this->requestHost = $requestHost;
263+
}
264+
265+
/**
266+
* @return string
267+
*/
268+
public function getRequestScheme()
269+
{
270+
return $this->requestScheme;
271+
}
272+
273+
/**
274+
* @param string $requestScheme
275+
*/
276+
public function setRequestScheme($requestScheme)
277+
{
278+
$this->requestScheme = $requestScheme;
279+
}
154280
}

Collector/Twig/HttpMessageMarkupExtension.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public function markup($message)
3939
// make header names bold
4040
$headers = preg_replace("|\n(.*?): |si", "\n<b>$1</b>: ", $parts[0]);
4141

42-
return sprintf("%s\n\n<div class='httplug-http-body'>%s</div>", $headers, $parts[1]);
42+
return sprintf("%s\n\n<div class='httplug-http-body httplug-hidden'>%s</div>", $headers, $parts[1]);
4343
}
4444

4545
public function getName()

Resources/config/data-collector.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,26 +34,32 @@
3434
<service id="httplug.collector.factory.buzz" class="Http\HttplugBundle\Collector\ProfileClientFactory" decorates="httplug.factory.buzz" public="false">
3535
<argument type="service" id="httplug.collector.factory.buzz.inner"/>
3636
<argument type="service" id="httplug.collector.collector"/>
37+
<argument type="service" id="httplug.collector.formatter"/>
3738
</service>
3839
<service id="httplug.collector.factory.curl" class="Http\HttplugBundle\Collector\ProfileClientFactory" decorates="httplug.factory.curl" public="false">
3940
<argument type="service" id="httplug.collector.factory.curl.inner"/>
4041
<argument type="service" id="httplug.collector.collector"/>
42+
<argument type="service" id="httplug.collector.formatter"/>
4143
</service>
4244
<service id="httplug.collector.factory.guzzle5" class="Http\HttplugBundle\Collector\ProfileClientFactory" decorates="httplug.factory.guzzle5" public="false">
4345
<argument type="service" id="httplug.collector.factory.guzzle5.inner"/>
4446
<argument type="service" id="httplug.collector.collector"/>
47+
<argument type="service" id="httplug.collector.formatter"/>
4548
</service>
4649
<service id="httplug.collector.factory.guzzle6" class="Http\HttplugBundle\Collector\ProfileClientFactory" decorates="httplug.factory.guzzle6" public="false">
4750
<argument type="service" id="httplug.collector.factory.guzzle6.inner"/>
4851
<argument type="service" id="httplug.collector.collector"/>
52+
<argument type="service" id="httplug.collector.formatter"/>
4953
</service>
5054
<service id="httplug.collector.factory.react" class="Http\HttplugBundle\Collector\ProfileClientFactory" decorates="httplug.factory.react" public="false">
5155
<argument type="service" id="httplug.collector.factory.react.inner"/>
5256
<argument type="service" id="httplug.collector.collector"/>
57+
<argument type="service" id="httplug.collector.formatter"/>
5358
</service>
5459
<service id="httplug.collector.factory.socket" class="Http\HttplugBundle\Collector\ProfileClientFactory" decorates="httplug.factory.socket" public="false">
5560
<argument type="service" id="httplug.collector.factory.socket.inner"/>
5661
<argument type="service" id="httplug.collector.collector"/>
62+
<argument type="service" id="httplug.collector.formatter"/>
5763
</service>
5864
</services>
5965
</container>

0 commit comments

Comments
 (0)