Skip to content

Commit 171b263

Browse files
committed
Rewords
1 parent b1c6859 commit 171b263

File tree

1 file changed

+59
-58
lines changed

1 file changed

+59
-58
lines changed

profiler/data_collector.rst

Lines changed: 59 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,26 @@ Creating a custom Data Collector
1414
A data collector is a PHP class that implements the
1515
:class:`Symfony\\Component\\HttpKernel\\DataCollector\\DataCollectorInterface`.
1616
For convenience, your data collectors can also extend from the
17-
:class:`Symfony\\Component\\HttpKernel\\DataCollector\\DataCollector` class, which
18-
implements the interface and provides some utilities and the ``$this->data``
19-
property to store the collected information.
17+
:class:`Symfony\\Bundle\\FrameworkBundle\\DataCollector\\AbstractDataCollector`
18+
class, which implements the interface and provides some utilities and the
19+
``$this->data`` property to store the collected information.
20+
21+
.. versionadded:: 5.2
22+
23+
The ``AbstractDataCollector`` class was introduced in Symfony 5.2.
2024

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

2428
// src/DataCollector/RequestCollector.php
2529
namespace App\DataCollector;
2630

31+
use Symfony\Bundle\FrameworkBundle\DataCollector\AbstractDataCollector;
2732
use Symfony\Component\HttpFoundation\Request;
2833
use Symfony\Component\HttpFoundation\Response;
2934
use Symfony\Component\HttpKernel\DataCollector\DataCollector;
3035

31-
class RequestCollector extends DataCollector
36+
class RequestCollector extends AbstractDataCollector
3237
{
3338
public function collect(Request $request, Response $response, \Throwable $exception = null)
3439
{
@@ -37,25 +42,14 @@ request::
3742
'acceptable_content_types' => $request->getAcceptableContentTypes(),
3843
];
3944
}
40-
41-
public function reset()
42-
{
43-
$this->data = [];
44-
}
45-
46-
public function getName()
47-
{
48-
return 'app.request_collector';
49-
}
50-
51-
// ...
5245
}
5346

47+
These are the method that you can define in the data collector class:
48+
5449
:method:`Symfony\\Component\\HttpKernel\\DataCollector\\DataCollectorInterface::collect` method:
5550
Stores the collected data in local properties (``$this->data`` if you extend
56-
from :class:`Symfony\\Component\\HttpKernel\\DataCollector\\DataCollector`).
57-
If the data to collect cannot be obtained through the request or response,
58-
inject the needed services in the data collector.
51+
from ``AbstractDataCollector``). If you need some services to collect the
52+
data, inject those services in the data collector constructor.
5953

6054
.. caution::
6155

@@ -70,32 +64,28 @@ request::
7064
to provide your own ``serialize()`` method.
7165

7266
:method:`Symfony\\Component\\HttpKernel\\DataCollector\\DataCollectorInterface::reset` method:
73-
It's called between requests to reset the state of the profiler. Use it to
74-
remove all the information collected with the ``collect()`` method.
67+
It's called between requests to reset the state of the profiler. By default
68+
it only empties the ``$this->data`` contents, but you can override this method
69+
to do additional cleaning.
7570

7671
:method:`Symfony\\Component\\HttpKernel\\DataCollector\\DataCollectorInterface::getName` method:
7772
Returns the collector identifier, which must be unique in the application.
73+
By default it returns the FQCN of the data collector class, but you can
74+
override this method to return a custom name (e.g. ``app.request_collector``).
7875
This value is used later to access the collector information (see
79-
:doc:`/testing/profiling`) so it's recommended to return a string which is
80-
short, lowercase and without white spaces.
76+
:doc:`/testing/profiling`) so you may prefer using short strings instead of FQCN strings.
8177

8278
The ``collect()`` method is called during the :ref:`kernel.response <component-http-kernel-kernel-response>`
8379
event. If you need to collect data that is only available later, implement
8480
:class:`Symfony\\Component\\HttpKernel\\DataCollector\\LateDataCollectorInterface`
8581
and define the ``lateCollect()`` method, which is invoked right before the profiler
8682
data serialization (during :ref:`kernel.terminate <component-http-kernel-kernel-terminate>` event).
8783

88-
.. _data_collector_tag:
89-
90-
Enabling Custom Data Collectors
91-
-------------------------------
92-
93-
If you're using the :ref:`default services.yaml configuration <service-container-services-load-example>`
94-
with ``autoconfigure``, then Symfony will automatically see your new data collector!
95-
Your ``collect()`` method should be called next time your refresh.
84+
.. note::
9685

97-
If you're not using ``autoconfigure``, you can also :ref:`manually wire your service <services-explicitly-configure-wire-services>`
98-
and :doc:`tag </service_container/tags>` it with ``data_collector``.
86+
If you're using the :ref:`default services.yaml configuration <service-container-services-load-example>`
87+
with ``autoconfigure``, then Symfony will start using your data collector after the
88+
next page refresh. Otherwise, :ref:`enable the data collector by hand <data_collector_tag>`.
9989

10090
Adding Web Profiler Templates
10191
-----------------------------
@@ -104,10 +94,9 @@ The information collected by your data collector can be displayed both in the
10494
web debug toolbar and in the web profiler. To do so, you need to create a Twig
10595
template that includes some specific blocks.
10696

107-
However, first make your DataCollector to extends :class:`Symfony\\Bundle\\FrameworkBundle\\DataCollector\\AbstractDataCollector` instead of :class:`Symfony\\Component\\HttpKernel\\DataCollector\\DataCollector`. When extending :class:`Symfony\\Bundle\\FrameworkBundle\\DataCollector\\AbstractDataCollector`, you don't need to implement `getName` method; your collector FQDN is returned as identifier (you can also override it if needed). Though you need to implement `getTemplate` with the template you're going to use in the profiler (see below).
108-
109-
Then you must add some getters in the data collector class to give the
110-
template access to the collected information::
97+
First, add the ``getTemplate()`` method in your data collector class to return
98+
the path of the Twig template to use. Then, add some *getters* to give the
99+
template access to the collected information::::
111100

112101
// src/DataCollector/RequestCollector.php
113102
namespace App\DataCollector;
@@ -140,6 +129,7 @@ block and set the value of two variables called ``icon`` and ``text``:
140129

141130
.. code-block:: html+twig
142131

132+
{# templates/data_collector/template.html.twig #}
143133
{% extends '@WebProfiler/Profiler/layout.html.twig' %}
144134

145135
{% block toolbar %}
@@ -185,6 +175,7 @@ must also define additional blocks:
185175

186176
.. code-block:: html+twig
187177

178+
{# templates/data_collector/template.html.twig #}
188179
{% extends '@WebProfiler/Profiler/layout.html.twig' %}
189180

190181
{% block toolbar %}
@@ -234,9 +225,25 @@ The ``menu`` and ``panel`` blocks are the only required blocks to define the
234225
contents displayed in the web profiler panel associated with this data collector.
235226
All blocks have access to the ``collector`` object.
236227

237-
That's it ! Your data collector is now accessible in the toolbar.
228+
.. note::
229+
230+
The position of each panel in the toolbar is determined by the collector
231+
priority, which can only be defined when :ref:`configuring the data collector by hand <data_collector_tag>`.
232+
233+
.. note::
234+
235+
If you're using the :ref:`default services.yaml configuration <service-container-services-load-example>`
236+
with ``autoconfigure``, then Symfony will start displaying your collector data
237+
in the toolbar after the next page refresh. Otherwise, :ref:`enable the data collector by hand <data_collector_tag>`.
238+
239+
.. _data_collector_tag:
240+
241+
Enabling Custom Data Collectors
242+
-------------------------------
238243

239-
If you don't use the default configuration with :ref:`autowire and autoconfigure <service-container-services-load-example>`, you'll need to configure the data collector explicitely:
244+
If you don't use Symfony's default configuration with
245+
:ref:`autowire and autoconfigure <service-container-services-load-example>`
246+
you'll need to configure the data collector explicitly:
240247

241248
.. configuration-block::
242249

@@ -247,10 +254,12 @@ If you don't use the default configuration with :ref:`autowire and autoconfigure
247254
App\DataCollector\RequestCollector:
248255
tags:
249256
-
250-
name: data_collector
257+
name: data_collector
251258
# must match the value returned by the getName() method
252-
id: 'App\DataCollector\RequestCollector'
253-
# optional priority
259+
id: 'App\DataCollector\RequestCollector'
260+
# optional template (it has more priority than the value returned by getTemplate())
261+
template: 'data_collector/template.html.twig'
262+
# optional priority (positive or negative integer; default = 0)
254263
# priority: 300
255264
256265
.. code-block:: xml
@@ -264,10 +273,13 @@ If you don't use the default configuration with :ref:`autowire and autoconfigure
264273
265274
<services>
266275
<service id="App\DataCollector\RequestCollector">
267-
<!-- priority="300" -->
276+
<!-- the 'template' attribute has more priority than the value returned by getTemplate() -->
268277
<tag name="data_collector"
269278
id="App\DataCollector\RequestCollector"
279+
template="data_collector/template.html.twig"
270280
/>
281+
<!-- optional 'priority' attribute (positive or negative integer; default = 0) -->
282+
<!-- priority="300" -->
271283
</service>
272284
</services>
273285
</container>
@@ -284,21 +296,10 @@ If you don't use the default configuration with :ref:`autowire and autoconfigure
284296
285297
$services->set(RequestCollector::class)
286298
->tag('data_collector', [
287-
'id' => RequestCollector::class,
299+
'id' => RequestCollector::class,
300+
// optional template (it has more priority than the value returned by getTemplate())
301+
'template' => 'data_collector/template.html.twig',
302+
// optional priority (positive or negative integer; default = 0)
288303
// 'priority' => 300,
289304
]);
290305
};
291-
292-
The position of each panel in the toolbar is determined by the collector priority.
293-
Priorities are defined as positive or negative integers and they default to ``0``.
294-
Most built-in collectors use ``255`` as their priority. If you want your collector
295-
to be displayed before them, use a higher value (like 300).
296-
297-
.. versionadded:: 5.2
298-
299-
:class:`Symfony\\Bundle\\FrameworkBundle\\DataCollector\\AbstractDataCollector` was introduced in Symfony 5.2.
300-
301-
.. note::
302-
303-
Before the introduction of :class:`Symfony\\Bundle\\FrameworkBundle\\DataCollector\\AbstractDataCollector`, template path was defined in the service configuration (`template` key). This is still possible to define the template in the service configuration. In this case **template in service configuration takes precedence over template defined in data collector code**.
304-

0 commit comments

Comments
 (0)