Skip to content

Commit b1c6859

Browse files
committed
minor #14085 Documentation about custom data collectors with autowire/autoconfigure (l-vo)
This PR was merged into the master branch. Discussion ---------- Documentation about custom data collectors with autowire/autoconfigure Feature integrated in symfony/symfony#37332 Close #14060 Commits ------- 54eafd3 Documentation about custom data collectors with autowire/autoconfigure
2 parents a533c35 + 54eafd3 commit b1c6859

File tree

1 file changed

+25
-12
lines changed

1 file changed

+25
-12
lines changed

profiler/data_collector.rst

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -104,18 +104,25 @@ The information collected by your data collector can be displayed both in the
104104
web debug toolbar and in the web profiler. To do so, you need to create a Twig
105105
template that includes some specific blocks.
106106

107-
However, first you must add some getters in the data collector class to give the
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
108110
template access to the collected information::
109111

110112
// src/DataCollector/RequestCollector.php
111113
namespace App\DataCollector;
112114

113-
use Symfony\Component\HttpKernel\DataCollector\DataCollector;
115+
use Symfony\Bundle\FrameworkBundle\DataCollector\AbstractDataCollector;
114116

115-
class RequestCollector extends DataCollector
117+
class RequestCollector extends AbstractDataCollector
116118
{
117119
// ...
118120

121+
public static function getTemplate(): ?string
122+
{
123+
return 'data_collector/template.html.twig';
124+
}
125+
119126
public function getMethod()
120127
{
121128
return $this->data['method'];
@@ -227,8 +234,9 @@ The ``menu`` and ``panel`` blocks are the only required blocks to define the
227234
contents displayed in the web profiler panel associated with this data collector.
228235
All blocks have access to the ``collector`` object.
229236

230-
Finally, to enable the data collector template, override your service configuration
231-
to specify a tag that contains the template:
237+
That's it ! Your data collector is now accessible in the toolbar.
238+
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:
232240

233241
.. configuration-block::
234242

@@ -240,9 +248,8 @@ to specify a tag that contains the template:
240248
tags:
241249
-
242250
name: data_collector
243-
template: 'data_collector/template.html.twig'
244251
# must match the value returned by the getName() method
245-
id: 'app.request_collector'
252+
id: 'App\DataCollector\RequestCollector'
246253
# optional priority
247254
# priority: 300
248255
@@ -259,8 +266,7 @@ to specify a tag that contains the template:
259266
<service id="App\DataCollector\RequestCollector">
260267
<!-- priority="300" -->
261268
<tag name="data_collector"
262-
template="data_collector/template.html.twig"
263-
id="app.request_collector"
269+
id="App\DataCollector\RequestCollector"
264270
/>
265271
</service>
266272
</services>
@@ -277,10 +283,8 @@ to specify a tag that contains the template:
277283
$services = $configurator->services();
278284
279285
$services->set(RequestCollector::class)
280-
->autowire()
281286
->tag('data_collector', [
282-
'template' => 'data_collector/template.html.twig',
283-
'id' => 'app.request_collector',
287+
'id' => RequestCollector::class,
284288
// 'priority' => 300,
285289
]);
286290
};
@@ -289,3 +293,12 @@ The position of each panel in the toolbar is determined by the collector priorit
289293
Priorities are defined as positive or negative integers and they default to ``0``.
290294
Most built-in collectors use ``255`` as their priority. If you want your collector
291295
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)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy