44How to Create a custom Data Collector
55=====================================
66
7- :doc: `The Symfony Profiler </cookbook /profiler/index >` delegates data collecting to
7+ :doc: `The Symfony Profiler </component /profiler/index >` delegates data collecting to
88data collectors. Symfony comes bundled with a few of them, but you can easily
99create your own.
1010
1111Creating a custom Data Collector
1212--------------------------------
1313
1414Creating a custom data collector is as simple as implementing the
15- :class: `Symfony\\ Component\\ HttpKernel \\ DataCollector\\ DataCollectorInterface `::
15+ :class: `Symfony\\ Component\\ Profiler \\ DataCollector\\ DataCollectorInterface `::
1616
1717 interface DataCollectorInterface
1818 {
1919 /**
20- * Collects data for the given Request and Response .
20+ * Set the Token of the active profile .
2121 *
22- * @param Request $request A Request instance
23- * @param Response $response A Response instance
24- * @param \Exception $exception An Exception instance
22+ * @param $token
2523 */
26- function collect(Request $request, Response $response, \Exception $exception = null );
24+ public function setToken($token );
2725
2826 /**
2927 * Returns the name of the collector.
@@ -37,40 +35,114 @@ The ``getName()`` method must return a unique name. This is used to access the
3735information later on (see :doc: `/cookbook/testing/profiling ` for
3836instance).
3937
40- The ``collect() `` method is responsible for storing the data it wants to give
41- access to in local properties.
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 `.
4264
4365.. caution ::
4466
45- As the profiler serializes data collector instances, you should not
67+ As the profiler serializes ProfileData instances, you should not
4668 store objects that cannot be serialized (like PDO objects), or you need
4769 to provide your own ``serialize() `` method.
4870
4971Most 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 ::
5375
54- class MemoryDataCollector extends DataCollector
76+ class MemoryDataCollector extends AbstractDataCollector implements LateDataCollectorInterface
5577 {
56- public function collect(Request $request, Response $response, \Exception $exception = null)
78+ private $memoryLimit;
79+
80+ /**
81+ * Constructor.
82+ */
83+ public function __construct()
5784 {
58- $this->data = array(
59- 'memory' => memory_get_peak_usage(true),
60- );
85+ $this->memoryLimit = ini_get('memory_limit');
6186 }
6287
63- public function getMemory()
88+ /**
89+ * {@inheritdoc}
90+ */
91+ public function lateCollect()
6492 {
65- return $this->data['memory'] ;
93+ return new MemoryData(memory_get_peak_usage(true), $this->memoryLimit) ;
6694 }
6795
96+ /**
97+ * {@inheritdoc}
98+ */
6899 public function getName()
69100 {
70101 return 'memory';
71102 }
72103 }
73104
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+
74146.. _data_collector_tag :
75147
76148Enabling custom Data Collectors
0 commit comments