4
4
How to Create a custom Data Collector
5
5
=====================================
6
6
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.
10
10
11
11
Creating a custom Data Collector
12
12
--------------------------------
13
13
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.
16
20
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::
44
23
45
24
// src/AppBundle/DataCollector/RequestCollector.php
46
25
namespace AppBundle\DataCollector;
@@ -59,36 +38,46 @@ collects the method and accepted content types from the request::
59
38
);
60
39
}
61
40
62
- public function getMethod ()
41
+ public function reset ()
63
42
{
64
- return $this->data['method'];
65
- }
66
-
67
- public function getAcceptableContentTypes()
68
- {
69
- return $this->data['acceptable_content_types'];
43
+ $this->data = array();
70
44
}
71
45
72
46
public function getName()
73
47
{
74
48
return 'app.request_collector';
75
49
}
50
+
51
+ // ...
76
52
}
77
53
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.
79
59
80
- .. caution ::
60
+ .. caution ::
81
61
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.
86
65
87
- .. caution ::
66
+ .. caution ::
88
67
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.
92
81
93
82
.. _data_collector_tag :
94
83
@@ -99,10 +88,8 @@ If you're using the :ref:`default services.yml configuration <service-container-
99
88
with ``autoconfigure ``, then Symfony will automatically see your new data collector!
100
89
Your ``collect() `` method should be called next time your refresh.
101
90
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 ``.
106
93
107
94
Adding Web Profiler Templates
108
95
-----------------------------
@@ -111,6 +98,29 @@ The information collected by your data collector can be displayed both in the
111
98
web debug toolbar and in the web profiler. To do so, you need to create a Twig
112
99
template that includes some specific blocks.
113
100
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
+
114
124
In the simplest case, you just want to display the information in the toolbar
115
125
without providing a profiler panel. This requires to define the ``toolbar ``
116
126
block and set the value of two variables called ``icon `` and ``text ``:
0 commit comments