Skip to content

Commit 3bcdf1d

Browse files
committed
Added improved data collector and web profiler page
1 parent f159e61 commit 3bcdf1d

15 files changed

+781
-156
lines changed

ClientFactory/PluginClientFactory.php

+4-3
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,13 @@ final class PluginClientFactory
1515
/**
1616
* @param Plugin[] $plugins
1717
* @param ClientFactory $factory
18-
* @param array $config
18+
* @param array $config config to the client factory
19+
* @param array $pluginClientOptions config forwarded to the PluginClient
1920
*
2021
* @return PluginClient
2122
*/
22-
public static function createPluginClient(array $plugins, ClientFactory $factory, array $config)
23+
public static function createPluginClient(array $plugins, ClientFactory $factory, array $config, array $pluginClientOptions = [])
2324
{
24-
return new PluginClient($factory->createClient($config), $plugins);
25+
return new PluginClient($factory->createClient($config), $plugins, $pluginClientOptions);
2526
}
2627
}

Collector/DebugPlugin.php

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
3+
namespace Http\HttplugBundle\Collector;
4+
5+
use Http\Client\Common\Plugin;
6+
use Http\Client\Exception;
7+
use Psr\Http\Message\RequestInterface;
8+
use Psr\Http\Message\ResponseInterface;
9+
10+
/**
11+
* A plugin used for log requests and responses. This plugin is executed between each normal plugin.
12+
*
13+
* @author Tobias Nyholm <[email protected]>
14+
*/
15+
final class DebugPlugin implements Plugin
16+
{
17+
/**
18+
* @var DebugPluginCollector
19+
*/
20+
private $collector;
21+
22+
/**
23+
* @var string
24+
*/
25+
private $clientName;
26+
27+
/**
28+
* @var int
29+
*/
30+
private $depth = -1;
31+
32+
/**
33+
* @param DebugPluginCollector $collector
34+
* @param string $clientName
35+
*/
36+
public function __construct(DebugPluginCollector $collector, $clientName)
37+
{
38+
$this->collector = $collector;
39+
$this->clientName = $clientName;
40+
}
41+
42+
/**
43+
* {@inheritdoc}
44+
*/
45+
public function handleRequest(RequestInterface $request, callable $next, callable $first)
46+
{
47+
$collector = $this->collector;
48+
$clientName = $this->clientName;
49+
$depth = &$this->depth;
50+
51+
$collector->addRequest($request, $clientName, ++$depth);
52+
53+
return $next($request)->then(function (ResponseInterface $response) use ($collector, $clientName, &$depth) {
54+
$collector->addResponse($response, $clientName, $depth--);
55+
56+
return $response;
57+
}, function (Exception $exception) use ($collector, $clientName, &$depth) {
58+
$collector->addFailure($exception, $clientName, $depth--);
59+
60+
throw $exception;
61+
});
62+
}
63+
}

Collector/DebugPluginCollector.php

+181
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
1+
<?php
2+
3+
namespace Http\HttplugBundle\Collector;
4+
5+
use Http\Client\Exception;
6+
use Http\Message\Formatter;
7+
use Psr\Http\Message\RequestInterface;
8+
use Psr\Http\Message\ResponseInterface;
9+
use Symfony\Component\HttpFoundation\Request;
10+
use Symfony\Component\HttpFoundation\Response;
11+
use Symfony\Component\HttpKernel\DataCollector\DataCollector;
12+
13+
/**
14+
* A data collector for the debug plugin.
15+
*
16+
* @author Tobias Nyholm <[email protected]>
17+
*/
18+
final class DebugPluginCollector extends DataCollector
19+
{
20+
/**
21+
* @var Formatter
22+
*/
23+
private $formatter;
24+
25+
/**
26+
* @var PluginJournal
27+
*/
28+
private $journal;
29+
30+
/**
31+
* @param Formatter $formatter
32+
* @param PluginJournal $journal
33+
*/
34+
public function __construct(Formatter $formatter, PluginJournal $journal)
35+
{
36+
$this->formatter = $formatter;
37+
$this->journal = $journal;
38+
}
39+
40+
/**
41+
* @param RequestInterface $request
42+
* @param string $clientName
43+
* @param int $depth
44+
*/
45+
public function addRequest(RequestInterface $request, $clientName, $depth)
46+
{
47+
$this->data[$clientName]['request'][$depth][] = $this->formatter->formatRequest($request);
48+
}
49+
50+
/**
51+
* @param ResponseInterface $response
52+
* @param string $clientName
53+
* @param int $depth
54+
*/
55+
public function addResponse(ResponseInterface $response, $clientName, $depth)
56+
{
57+
$this->data[$clientName]['response'][$depth][] = $this->formatter->formatResponse($response);
58+
$this->data[$clientName]['failure'][$depth][] = false;
59+
}
60+
61+
/**
62+
* @param Exception $exception
63+
* @param string $clientName
64+
* @param int $depth
65+
*/
66+
public function addFailure(Exception $exception, $clientName, $depth)
67+
{
68+
if ($exception instanceof Exception\HttpException) {
69+
$formattedResponse = $this->formatter->formatResponse($exception->getResponse());
70+
} elseif ($exception instanceof Exception\TransferException) {
71+
$formattedResponse = $exception->getMessage();
72+
} else {
73+
$formattedResponse = sprintf('Unexpected exception of type "%s"', get_class($exception));
74+
}
75+
76+
$this->data[$clientName]['response'][$depth][] = $formattedResponse;
77+
$this->data[$clientName]['failure'][$depth][] = true;
78+
}
79+
80+
/**
81+
* Returns the successful request-resonse pairs.
82+
*
83+
* @return array
84+
*/
85+
public function getSucessfulRequests()
86+
{
87+
$count = 0;
88+
foreach ($this->data as $client) {
89+
if (isset($client['failure'])) {
90+
foreach ($client['failure'][0] as $failure) {
91+
if (!$failure) {
92+
++$count;
93+
}
94+
}
95+
}
96+
}
97+
98+
return $count;
99+
}
100+
101+
/**
102+
* Returns the failed request-resonse pairs.
103+
*
104+
* @return array
105+
*/
106+
public function getFailedRequests()
107+
{
108+
$count = 0;
109+
foreach ($this->data as $client) {
110+
if (isset($client['failure'])) {
111+
foreach ($client['failure'][0] as $failure) {
112+
if ($failure) {
113+
++$count;
114+
}
115+
}
116+
}
117+
}
118+
119+
return $count;
120+
}
121+
122+
/**
123+
* Returns the total number of request made.
124+
*
125+
* @return int
126+
*/
127+
public function getTotalRequests()
128+
{
129+
return $this->getSucessfulRequests() + $this->getFailedRequests();
130+
}
131+
132+
/**
133+
* Return a RequestStackProvider for each client.
134+
*
135+
* @return RequestStackProvider[]
136+
*/
137+
public function getClients()
138+
{
139+
return RequestStackProvider::createFromCollectedData($this->data);
140+
}
141+
142+
/**
143+
* @return PluginJournal
144+
*/
145+
public function getJournal()
146+
{
147+
return $this->journal;
148+
}
149+
150+
/**
151+
* {@inheritdoc}
152+
*/
153+
public function collect(Request $request, Response $response, \Exception $exception = null)
154+
{
155+
// We do not need to collect any data from the Symfony Request and Response
156+
}
157+
158+
/**
159+
* {@inheritdoc}
160+
*/
161+
public function getName()
162+
{
163+
return 'httplug';
164+
}
165+
166+
/**
167+
* {@inheritdoc}
168+
*/
169+
public function serialize()
170+
{
171+
return serialize([$this->data, $this->journal]);
172+
}
173+
174+
/**
175+
* {@inheritdoc}
176+
*/
177+
public function unserialize($data)
178+
{
179+
list($this->data, $this->journal) = unserialize($data);
180+
}
181+
}

Collector/MessageJournal.php

-109
This file was deleted.

0 commit comments

Comments
 (0)