A data collector is a PHP class that implements the
DataCollectorInterface.
For convenience, your data collectors can also extend from the
DataCollector class, which
implements the interface and provides some utilities and the $this->data
property to store the collected information.

The following example shows a custom collector that stores information about the
request:

Stores the collected data in local properties ($this->data if you extend
from DataCollector).
If the data to collect cannot be obtained through the request or response,
inject the needed services in the data collector.

Caution

The collect() method is only called once. It is not used to "gather"
data but is there to "pick up" the data that has been stored by your
service.

Caution

As the profiler serializes data collector instances, you should not
store objects that cannot be serialized (like PDO objects) or you need
to provide your own serialize() method.

Returns the collector identifier, which must be unique in the application.
This value is used later to access the collector information (see
How to Use the Profiler in a Functional Test) so it's recommended to return a string which is
short, lowercased and without white spaces.

The information collected by your data collector can be displayed both in the
web debug toolbar and in the web profiler. To do so, you need to create a Twig
template that includes some specific blocks.

However, first you must add some getters in the data collector class to give the
template access to the collected information:

In the simplest case, you just want to display the information in the toolbar
without providing a profiler panel. This requires to define the toolbar
block and set the value of two variables called icon and text:

{%extends'@WebProfiler/Profiler/layout.html.twig'%}{%blocktoolbar%}{%seticon%}{# this is the content displayed as a panel in the toolbar #}<svgxmlns="http://www.w3.org/2000/svg"> ... </svg><spanclass="sf-toolbar-value">Request</span>{%endset%}{%settext%}{# this is the content displayed when hovering the mouse over the toolbar panel #}<divclass="sf-toolbar-info-piece"><b>Method</b><span>{{collector.method}}</span></div><divclass="sf-toolbar-info-piece"><b>Accepted content type</b><span>{{collector.acceptableContentTypes|join(', ')}}</span></div>{%endset%}{# the 'link' value set to 'false' means that this panel doesn't show a section in the web profiler #}{{include('@WebProfiler/Profiler/toolbar_item.html.twig',{link:false})}}{%endblock%}

Tip

Built-in collector templates define all their images as embedded SVG files.
This makes them work everywhere without having to mess with web assets links:

1
2
3
4

{%seticon%}{{include('data_collector/icon.svg')}}{# ... #}{%endset%}

If the toolbar panel includes extended web profiler information, the Twig template
must also define additional blocks:

{%extends'@WebProfiler/Profiler/layout.html.twig'%}{%blocktoolbar%}{%seticon%}{# ... #}{%endset%}{%settext%}<divclass="sf-toolbar-info-piece">{# ... #}</div>{%endset%}{{include('@WebProfiler/Profiler/toolbar_item.html.twig',{'link':true})}}{%endblock%}{%blockhead%}{# Optional. Here you can link to or define your own CSS and JS contents. #}{# Use {{ parent() }} to extend the default styles instead of overriding them. #}{%endblock%}{%blockmenu%}{# This left-hand menu appears when using the full-screen profiler. #}<spanclass="label"><spanclass="icon"><imgsrc="..."alt=""/></span><strong>Request</strong></span>{%endblock%}{%blockpanel%}{# Optional, for showing the most details. #}<h2>Acceptable Content Types</h2><table><tr><th>Content Type</th></tr>{%fortypeincollector.acceptableContentTypes%}<tr><td>{{type}}</td></tr>{%endfor%}</table>{%endblock%}

The menu and panel blocks are the only required blocks to define the
contents displayed in the web profiler panel associated with this data collector.
All blocks have access to the collector object.

Finally, to enable the data collector template, override your service configuration
to specify a tag that contains the template:

YAML

1
2
3
4
5
6
7
8
9
10
11
12

# config/services.yamlservices:App\DataCollector\RequestCollector:tags:-name:data_collectortemplate:'data_collector/template.html.twig'# must match the value returned by the getName() methodid:'app.request_collector'# optional priority# priority: 300public:false