8000 Add messages in the profiler · sroze/symfony@3d4d82b · GitHub
[go: up one dir, main page]

Skip to content

Commit 3d4d82b

Browse files
committed
Add messages in the profiler
1 parent 383f5aa commit 3d4d82b

File tree

4 files changed

+161
-0
lines changed

4 files changed

+161
-0
lines changed
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bundle\FrameworkBundle\DataCollector;
13+
14+
use Symfony\Component\HttpFoundation\Request;
15+
use Symfony\Component\HttpFoundation\Response;
16+
use Symfony\Component\HttpKernel\DataCollector\DataCollector;
17+
use Symfony\Component\Message\MessageBusMiddlewareInterface;
18+
19+
/**
20+
* @author Samuel Roze <samuel.roze@gmail.com>
21+
*/
22+
class MessagesDataCollector extends DataCollector implements MessageBusMiddlewareInterface
23+
{
24+
/**
25+
* {@inheritdoc}
26+
*/
27+
public function collect(Request $request, Response $response, \Exception $exception = null)
28+
{
29+
return $this->data;
30+
}
31+
32+
/**
33+
* {@inheritdoc}
34+
*/
35+
public function getName()
36+
{
37+
return 'messages';
38+
}
39+
40+
/**
41+
* {@inheritdoc}
42+
*/
43+
public function handle($message, callable $next)
44+
{
45+
$debugRepresentation = array(
46+
'message' => array(
47+
'type' => get_class($message),
48+
),
49+
);
50+
51+
try {
52+
$result = $next($message);
53+
54+
if (is_object($result)) {
55+
$debugRepresentation['result'] = array(
56+
'type' => get_class($result),
57+
);
58+
} else {
59+
$debugRepresentation['result'] = array(
60+
'type' => gettype($result),
61+
'value' => $result,
62+
);
63+
}
64+
} catch (\Throwable $exception) {
65+
$debugRepresentation['exception'] = array(
66+
'type' => get_class($exception),
67+
'message' => $exception->getMessage(),
68+
);
69+
}
70+
71+
$this->data[] = $debugRepresentation;
72+
73+
if (isset($exception)) {
74+
throw $exception;
75+
}
76+
77+
return $result;
78+
}
79+
80+
/**
81+
* @return array
82+
*/
83+
public function getMessages()
84+
{
85+
return $this->data;
86+
}
87+
}

src/Symfony/Bundle/FrameworkBundle/Resources/config/collectors.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,5 +51,10 @@
5151
<tag name="kernel.event_listener" event="kernel.controller" method="onKernelController"/>
5252
<tag name="data_collector" template="@WebProfiler/Collector/router.html.twig" id="router" priority="285" />
5353
</service>
54+
55+
<service id="data_collector.messages" class="Symfony\Bundle\FrameworkBundle\DataCollector\MessagesDataCollector">
56+
<tag name="data_collector" template="@WebProfiler/Collector/messages.html.twig" id="messages" priority="100" />
57+
<tag name="message_middleware" />
58+
</service>
5459
</services>
5560
</container>
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
{% extends '@WebProfiler/Profiler/layout.html.twig' %}
2+
3+
{% import _self as helper %}
4+
5+
{% block menu %}
6+
<span class="label">
7+
<span class="icon">{{ include('@WebProfiler/Icon/messages.svg') }}</span>
8+
<strong>Messages</strong>
9+
10+
{% if collector.messages | length > 0 %}
11+
<span class="count">
12+
<span>{{ collector.messages | length }}</span>
13+
</span>
14+
{% endif %}
15+
</span>
16+
{% endblock %}
17+
18+
{% block panel %}
19+
<h2>Messages</h2>
20+
21+
{% if collector.messages is empty %}
22+
<p>No messages</p>
23+
{% else %}
24+
<table>
25+
<thead>
26+
<tr>
27+
<th>Message</th>
28+
<th>Result</th>
29+
</tr>
30+
</thead>
31+
<tbody>
32+
{% for message in collector.messages %}
33+
<tr>
34+
<td>{{ message.message.type }}</td>
35+
<td>
36+
{% if message.result.type is defined %}
37+
{{ message.result.type }}
38+
{% endif %}
39+
40+
{% if message.exception.type is defined %}
41+
{{ message.exception.type }}
42+
{% endif %}
43+
</td>
44+
</tr>
45+
{% endfor %}
46+
</tbody>
47+
</table>
48+
{% endif %}
49+
{% endblock %}
50+
51+
{% block toolbar %}
52+
{% set color_code = 'normal' %}
53+
{% set message_count = 0 %}
54+
{% set icon %}
55+
{% if profiler_markup_version == 1 %}
56+
{{ include('@WebProfiler/Icon/messages.svg', { height: 28, color: '#3F3F3F' }) }}
57+
<span class="sf-toolbar-status sf-toolbar-status-{{ color_code }}">{{ message_count }}</span>
58+
{% else %}
59+
{{ include('@WebProfiler/Icon/messages.svg') }}
60+
<span class="sf-toolbar-value">{{ message_count }}</span>
61+
{% endif %}
62+
{% endset %}
63+
64+
{{ include('@WebProfiler/Profiler/toolbar_item.html.twig', { link: 'messages', status: color_code }) }}
65+
{% endblock %}
Lines changed: 4 additions & 0 deletions
Loading

0 commit comments

Comments
 (0)
0