4
4
How to Create a custom Data Collector
5
5
=====================================
6
6
7
- :doc: `The Symfony Profiler </cookbook /profiler/index >` delegates data collecting to
7
+ :doc: `The Symfony Profiler </components /profiler/index >` delegates data collecting to
8
8
data collectors. Symfony comes bundled with a few of them, but you can easily
9
9
create your own.
10
10
11
11
Creating a custom Data Collector
12
12
--------------------------------
13
13
14
14
Creating a custom data collector is as simple as implementing the
15
- :class: `Symfony\\ Component\\ HttpKernel \\ DataCollector\\ DataCollectorInterface `::
15
+ :class: `Symfony\\ Component\\ Profiler \\ DataCollector\\ DataCollectorInterface `::
16
16
17
17
interface DataCollectorInterface
18
18
{
19
19
/**
20
- * Collects data for the given Request and Response .
20
+ * Set the Token of the active profile .
21
21
*
22
- * @param Request $request A Request instance
23
- * @param Response $response A Response instance
24
- * @param \Exception $exception An Exception instance
22
+ * @param $token
25
23
*/
26
- function collect(Request $request, Response $response, \Exception $exception = null );
24
+ public function setToken($token );
27
25
28
26
/**
29
27
* Returns the name of the collector.
@@ -37,40 +35,114 @@ The ``getName()`` method must return a unique name. This is used to access the
37
35
information later on (see :doc: `/cookbook/testing/profiling ` for
38
36
instance).
39
37
40
- The ``collect() `` method is responsible for storing the data it wants to give
41
- access to in local properties.
802E
38
+ And implementing either the :class: `Symfony\\ Component\\ Profiler\\ DataCollector\\ RuntimeDataCollectorInterface `::
39
+
40
+ interface RuntimeDataCollectorInterface
41
+ {
42
+ /**
43
+ * Collects data when profiler is triggered.
44
+ *
45
+ * @return ProfileDataInterface
46
+ */
47
+ public function collect();
48
+ }
49
+
50
+ or the :class: `Symfony\\ Component\\ Profiler\\ DataCollector\\ LateDataCollectorInterface `::
51
+
52
+ interface LateDataCollectorInterface
53
+ {
54
+ /**
55
+ * Collects data as late as possible.
56
+ *
57
+ * @return ProfileDataInterface
58
+ */
59
+ public function lateCollect();
60
+ }
61
+
62
+ The ``collect() `` or ``lateCollect() `` method is responsible for storing the data it wants to give
63
+ access to in a :class: `Symfony\\ Component\\ Profiler\\ ProfileData\\ ProfileDataInterface `.
42
64
43
65
.. caution ::
44
66
45
- As the profiler serializes data collector instances, you should not
67
+ As the profiler serializes ProfileData instances, you should not
46
68
store objects that cannot be serialized (like PDO objects), or you need
47
69
to provide your own ``serialize() `` method.
48
70
49
71
Most of the time, it is convenient to extend
50
- :class: `Symfony\\ Component\\ HttpKernel \\ DataCollector\\ DataCollector ` and
51
- populate the `` $this->data `` property (it takes care of serializing the
52
- `` $this-> data`` property) ::
72
+ :class: `Symfony\\ Component\\ Profiler \\ DataCollector\\ AbstractDataCollector ` which already implements
73
+ :class: ` Symfony \\ Component \\ Profiler \\ DataCollector \\ DataCollectorInterface ` and ` setToken($token) ` the only thing
74
+ left to do is to decide when the data is collected ::
53
75
54
- class MemoryDataCollector extends DataCollector
76
+ class MemoryDataCollector extends AbstractDataCollector implements LateDataCollectorInterface
55
77
{
56
- public function collect(Request $request, Response $response, \Exception $exception = null)
78
+ private $memoryLimit;
79
+
80
+ /**
81
+ * Constructor.
82
+ */
83
+ public function __construct()
57
84
{
58
- $this->data = array(
59
- 'memory' => memory_get_peak_usage(true),
60
- );
85
+ $this->memoryLimit = ini_get('memory_limit');
61
86
}
62
87
63
- public function getMemory()
88
+ /**
89
+ * {@inheritdoc}
90
+ */
91
+ public function lateCollect()
64
92
{
65
- return $this->data['memory'] ;
93
+ return new MemoryData(memory_get_peak_usage(true), $this->memoryLimit) ;
66
94
}
67
95
96
+ /**
97
+ * {@inheritdoc}
98
+ */
68
99
public function getName()
69
100
{
70
101
return 'memory';
71
102
}
72
103
}
73
104
105
+ class MemoryData implements ProfileDataInterface
106
+ {
107
+ private $memory;
108
+ private $memoryLimit;
109
+
110
+ /**
111
+ * Constructor.
112
+ *
113
+ * @param int $memory The current used memory.
114
+ * @param int $memoryLimit The memory limit.
115
+ */
116
+ public function __construct($memory, $memoryLimit)
117
+ {
118
+ $this->memory = $memory;
119
+ $this->memoryLimit = $this->convertToBytes($memoryLimit);
120
+ }
121
+
122
+ /**
123
+ * Returns the memory.
124
+ *
125
+ * @return int The memory
126
+ */
127
+ public function getMemory()
128
+ {
129
+ return $this->memory;
130
+ }
131
+
132
+ /**
133
+ * Returns the PHP memory limit.
134
+ *
135
+ * @return int The memory limit
136
+ */
137
+ public function getMemoryLimit()
138
+ {
139
+ return $this->memoryLimit;
140
+ }
141
+
142
+ //...
143
+ }
144
+
145
+
74
146
.. _data_collector_tag :
75
147
76
148
Enabling custom Data Collectors
0 commit comments