Skip to content

Commit d40fc1f

Browse files
committed
Updated the "custom data collector" article
1 parent 63d3090 commit d40fc1f

File tree

1 file changed

+63
-53
lines changed

1 file changed

+63
-53
lines changed

profiler/data_collector.rst

Lines changed: 63 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -4,43 +4,22 @@
44
How to Create a custom Data Collector
55
=====================================
66

7-
The :doc:`Symfony Profiler </profiler>` delegates data collection
8-
to some special classes called data collectors. Symfony comes bundled with a few
9-
of them, but you can easily create your own.
7+
The :doc:`Symfony Profiler </profiler>` obtains its profiling and debug
8+
information using some special classes called data collectors. Symfony comes
9+
bundled with a few of them, but you can also create your own.
1010

1111
Creating a custom Data Collector
1212
--------------------------------
1313

14-
Creating a custom data collector is as simple as implementing the
15-
:class:`Symfony\\Component\\HttpKernel\\DataCollector\\DataCollectorInterface`::
14+
A data collector is a PHP class that implements the
15+
:class:`Symfony\\Component\\HttpKernel\\DataCollector\\DataCollectorInterface`.
16+
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.
1620

17-
interface DataCollectorInterface
18-
{
19-
function collect(Request $request, Response $response, \Exception $exception = null);
20-
function getName();
21-
}
22-
23-
The
24-
:method:`Symfony\\Component\\HttpKernel\\DataCollector\\DataCollectorInterface::getName`
25-
method returns the name of the data collector and must be unique in the
26-
application. This value is also used to access the information later on (see
27-
:doc:`/testing/profiling` for instance).
28-
29-
The
30-
:method:`Symfony\\Component\\HttpKernel\\DataCollector\\DataCollectorInterface::collect`
31-
method is responsible for storing the collected data in local properties.
32-
33-
.. caution::
34-
35-
The ``collect()`` method is only called once. It is not used to "gather"
36-
data but is there to "pick up" the data that has been stored by your
37-
service.
38-
39-
Most of the time, it is convenient to extend
40-
:class:`Symfony\\Component\\HttpKernel\\DataCollector\\DataCollector` and
41-
populate the ``$this->data`` property (it takes care of serializing the
42-
``$this->data`` property). Imagine you create a new data collector that
43-
collects the method and accepted content types from the request::
21+
The following example shows a custom collector that stores information about the
22+
request::
4423

4524
// src/AppBundle/DataCollector/RequestCollector.php
4625
namespace AppBundle\DataCollector;
@@ -59,36 +38,46 @@ collects the method and accepted content types from the request::
5938
);
6039
}
6140

62-
public function getMethod()
41+
public function reset()
6342
{
64-
return $this->data['method'];
65-
}
66-
67-
public function getAcceptableContentTypes()
68-
{
69-
return $this->data['acceptable_content_types'];
43+
$this->data = array();
7044
}
7145

7246
public function getName()
7347
{
7448
return 'app.request_collector';
7549
}
50+
51+
// ...
7652
}
7753

78-
The getters are added to give the template access to the collected information.
54+
:method:`Symfony\\Component\\HttpKernel\\DataCollector\\DataCollectorInterface::collect` method:
55+
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.
7959

80-
.. caution::
60+
.. caution::
8161

82-
If the data that is not directly related to the request or response,
83-
you need to make the data accessible to your DataCollector. This can
84-
be achieved by injecting the service that holds the information you intend
85-
to profile into your DataCollector.
62+
The ``collect()`` method is only called once. It is not used to "gather"
63+
data but is there to "pick up" the data that has been stored by your
64+
service.
8665

87-
.. caution::
66+
.. caution::
8867

89-
As the profiler serializes data collector instances, you should not
90-
store objects that cannot be serialized (like PDO objects) or you need
91-
to provide your own ``serialize()`` method.
68+
As the profiler serializes data collector instances, you should not
69+
store objects that cannot be serialized (like PDO objects) or you need
70+
to provide your own ``serialize()`` method.
71+
72+
: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.
75+
76+
:method:`Symfony\\Component\\HttpKernel\\DataCollector\\DataCollectorInterface::getName` method:
77+
Returns the collector identifier, which must be unique in the application.
78+
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, lowercased and without white spaces.
9281

9382
.. _data_collector_tag:
9483

@@ -99,10 +88,8 @@ If you're using the :ref:`default services.yml configuration <service-container-
9988
with ``autoconfigure``, then Symfony will automatically see your new data collector!
10089
Your ``collect()`` method should be called next time your refresh.
10190

102-
.. note::
103-
104-
If you're not using ``autoconfigure``, you can also :ref:`manually wire your service <services-explicitly-configure-wire-services>`
105-
and :doc:`tag </service_container/tags>` it with ``data_collector``.
91+
If you're not using ``autoconfigure``, you can also :ref:`manually wire your service <services-explicitly-configure-wire-services>`
92+
and :doc:`tag </service_container/tags>` it with ``data_collector``.
10693

10794
Adding Web Profiler Templates
10895
-----------------------------
@@ -111,6 +98,29 @@ The information collected by your data collector can be displayed both in the
11198
web debug toolbar and in the web profiler. To do so, you need to create a Twig
11299
template that includes some specific blocks.
113100

101+
However, first you must add some getters in the data collector class to give the
102+
template access to the collected information::
103+
104+
// src/AppBundle/DataCollector/RequestCollector.php
105+
namespace AppBundle\DataCollector;
106+
107+
use Symfony\Component\HttpKernel\DataCollector\DataCollector;
108+
109+
class RequestCollector extends DataCollector
110+
{
111+
// ...
112+
113+
public function getMethod()
114+
{
115+
return $this->data['method'];
116+
}
117+
118+
public function getAcceptableContentTypes()
119+
{
120+
return $this->data['acceptable_content_types'];
121+
}
122+
}
123+
114124
In the simplest case, you just want to display the information in the toolbar
115125
without providing a profiler panel. This requires to define the ``toolbar``
116126
block and set the value of two variables called ``icon`` and ``text``:

0 commit comments

Comments
 (0)