Skip to content

Commit 9767042

Browse files
committed
Added web profiler and toolbar
1 parent 8e3a96e commit 9767042

File tree

6 files changed

+210
-2
lines changed

6 files changed

+210
-2
lines changed

Collector/MessageJournal.php

+108
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
<?php
2+
3+
namespace Http\HttplugBundle\Collector;
4+
5+
use Http\Client\Exception;
6+
use Http\Client\Plugin\Journal;
7+
use Http\Message\Formatter\SimpleFormatter;
8+
use Http\Message\Formatter;
9+
use Psr\Http\Message\RequestInterface;
10+
use Psr\Http\Message\ResponseInterface;
11+
use Symfony\Component\HttpFoundation\Request;
12+
use Symfony\Component\HttpFoundation\Response;
13+
use Symfony\Component\HttpKernel\DataCollector\DataCollector;
14+
15+
/**
16+
* @author Tobias Nyholm <[email protected]>
17+
*/
18+
class MessageJournal extends DataCollector implements Journal
19+
{
20+
/**
21+
* @var Formatter
22+
*/
23+
private $formatter;
24+
25+
/**
26+
* @param Formatter $formatter
27+
*/
28+
public function __construct(Formatter $formatter = null)
29+
{
30+
$this->formatter = $formatter ?: new SimpleFormatter();
31+
$this->data=['success'=>[], 'failure'=>[]];
32+
}
33+
34+
/**
35+
* {@inheritdoc}
36+
*/
37+
public function addSuccess(RequestInterface $request, ResponseInterface $response)
38+
{
39+
$this->data['success'][] = [
40+
'request' => $this->formatter->formatRequest($request),
41+
'response' => $this->formatter->formatResponse($response),
42+
];
43+
}
44+
45+
/**
46+
* {@inheritdoc}
47+
*/
48+
public function addFailure(RequestInterface $request, Exception $exception)
49+
{
50+
if ($exception instanceof Exception\HttpException) {
51+
$formattedResponse = $this->formatter->formatResponse($exception->getResponse());
52+
} elseif ($exception instanceof Exception\TransferException) {
53+
$formattedResponse = $exception->getMessage();
54+
} else {
55+
$formattedResponse = sprintf('Unexpected exception of type "%s"', get_class($exception));
56+
}
57+
58+
$this->data['failure'][] = [
59+
'request' => $this->formatter->formatRequest($request),
60+
'response' => $formattedResponse,
61+
];
62+
}
63+
64+
/**
65+
* Get the successful request-resonse pairs.
66+
*
67+
* @return array
68+
*/
69+
public function getSucessfulRequests()
70+
{
71+
return $this->data['success'];
72+
}
73+
74+
/**
75+
* Get the failed request-resonse pairs.
76+
*
77+
* @return array
78+
*/
79+
public function getFailedRequests()
80+
{
81+
return $this->data['failure'];
82+
}
83+
84+
/**
85+
* Get the total number of request made.
86+
*
87+
* @return int
88+
*/
89+
public function getTotalRequests()
90+
{
91+
return count($this->data['success'])+count($this->data['failure']);
92+
}
93+
94+
/**
95+
* {@inheritdoc}
96+
*/
97+
public function collect(Request $request, Response $response, \Exception $exception = null)
98+
{
99+
}
100+
101+
/**
102+
* {@inheritdoc}
103+
*/
104+
public function getName()
105+
{
106+
return 'httplug';
107+
}
108+
}

DependencyInjection/Configuration.php

+1
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ public function getConfigTreeBuilder()
7272
->scalarNode('stream_factory')->defaultNull()->end()
7373
->end()
7474
->end()
75+
->booleanNode('toolbar')->defaultTrue()->end()
7576
->end()
7677
;
7778

DependencyInjection/HttplugExtension.php

+9
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@ public function load(array $configs, ContainerBuilder $container)
2727
$loader->load('services.xml');
2828
$loader->load('plugins.xml');
2929
$loader->load('discovery.xml');
30+
if ($config['toolbar'] && $container->getParameter('kernel.debug')) {
31+
$loader->load('data-collector.xml');
32+
$config['_inject_journal_plugin'] = true;
33+
}
34+
3035
foreach ($config['classes'] as $service => $class) {
3136
if (!empty($class)) {
3237
$container->removeDefinition(sprintf('httplug.%s.default', $service));
@@ -56,6 +61,10 @@ protected function configureClients(ContainerBuilder $container, array $config)
5661
$first = $name;
5762
}
5863

64+
if (isset($config['_inject_journal_plugin'])) {
65+
array_unshift($arguments['plugins'], 'httplug.plugin.history');
66+
}
67+
5968
$def = $container->register('httplug.client.'.$name, DummyClient::class);
6069

6170
if (empty($arguments['plugins'])) {

Resources/config/data-collector.xml

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<container xmlns="http://symfony.com/schema/dic/services"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
5+
6+
<services>
7+
<service id="httplug.message_journal" class="Http\HttplugBundle\Collector\MessageJournal" public="false">
8+
<tag name="data_collector" template="HttplugBundle::webprofiler.html.twig" priority="200"
9+
id="httplug"/>
10+
</service>
11+
12+
<service id="httplug.plugin.history" class="Http\Client\Plugin\HistoryPlugin" public="false">
13+
<argument type="service" id="httplug.message_journal"/>
14+
</service>
15+
</services>
16+
</container>

Resources/config/plugins.xml

+1-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@
55

66
<services>
77
<service id="httplug.plugin.content_length" class="Http\Client\Plugin\ContentLengthPlugin" public="false" />
8-
<service id="httplug.plugin.decoder" class="Http\Client\Plugin\DecoderPlugin" public="false">
9-
</service>
8+
<service id="httplug.plugin.decoder" class="Http\Client\Plugin\DecoderPlugin" public="false" />
109
<service id="httplug.plugin.error" class="Http\Client\Plugin\ErrorPlugin" public="false" />
1110
<service id="httplug.plugin.logger" class="Http\Client\Plugin\LoggerPlugin" public="false">
1211
<argument type="service" id="logger"/>

Resources/views/webprofiler.html.twig

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
{% extends '@WebProfiler/Profiler/layout.html.twig' %}
2+
3+
{% import _self as macro %}
4+
5+
{% block toolbar %}
6+
{% if collector.totalRequests > 0 %}
7+
{% set icon %}
8+
{{ include('@WebProfiler/Icon/ajax.svg') }}
9+
<span class="sf-toolbar-status">{{ collector.totalRequests }}</span>
10+
{% endset %}
11+
12+
{% set text %}
13+
<div class="sf-toolbar-info-piece">
14+
<b>Successful requests</b>
15+
<span>{{ collector.sucessfulRequests|length }}</span>
16+
</div>
17+
<div class="sf-toolbar-info-piece">
18+
<b>Faild requests</b>
19+
<span>{{ collector.failedRequests|length }}</span>
20+
</div>
21+
22+
{% endset %}
23+
{% include 'WebProfilerBundle:Profiler:toolbar_item.html.twig' with { 'link': profiler_url } %}
24+
{% endif %}
25+
{% endblock %}
26+
27+
{% block head %}
28+
{# Optional. Here you can link to or define your own CSS and JS contents. #}
29+
{{ parent() }}
30+
{% endblock %}
31+
32+
{% block menu %}
33+
{# This left-hand menu appears when using the full-screen profiler. #}
34+
<span class="label {{ collector.totalRequests == 0 ? 'disabled' }}">
35+
<span class="icon">{{ include('@WebProfiler/Icon/ajax.svg') }}</span>
36+
<strong>Httplug</strong>
37+
</span>
38+
{% endblock %}
39+
40+
{% block panel %}
41+
<h2>Httplug</h2>
42+
{% if (collector.failedRequests|length > 0) %}
43+
<h3>Failed requests</h3>
44+
{{ macro.printMessages(collector.failedRequests) }}
45+
{% endif %}
46+
47+
{% if (collector.sucessfulRequests|length > 0) %}
48+
<h3>Successful requests</h3>
49+
{{ macro.printMessages(collector.sucessfulRequests) }}
50+
{% endif %}
51+
52+
{% if collector.totalRequests == 0 %}
53+
54+
<div class="empty">
55+
<p>No request were sent.</p>
56+
</div>
57+
{% endif %}
58+
59+
{% endblock %}
60+
61+
{% macro printMessages(messages) %}
62+
<table>
63+
<tr>
64+
<th>Request</th>
65+
<th>Response</th>
66+
</tr>
67+
68+
{% for message in messages %}
69+
<tr>
70+
<td>{{ message['request'] }}</td>
71+
<td>{{ message['response'] }}</td>
72+
</tr>
73+
{% endfor %}
74+
</table>
75+
{% endmacro %}

0 commit comments

Comments
 (0)