diff --git a/cookbook/profiler/data_collector.rst b/cookbook/profiler/data_collector.rst
index 47229033063..b011bb878d4 100644
--- a/cookbook/profiler/data_collector.rst
+++ b/cookbook/profiler/data_collector.rst
@@ -4,7 +4,7 @@
How to Create a custom Data Collector
=====================================
-:doc:`The Symfony Profiler ` delegates data collection
+The :doc:`Symfony Profiler ` delegates data collection
to some special classes called data collectors. Symfony comes bundled with a few
of them, but you can easily create your own.
@@ -16,70 +16,71 @@ Creating a custom data collector is as simple as implementing the
interface DataCollectorInterface
{
- /**
- * Collects data for the given Request and Response.
- *
- * @param Request $request A Request instance
- * @param Response $response A Response instance
- * @param \Exception $exception An Exception instance
- */
function collect(Request $request, Response $response, \Exception $exception = null);
-
- /**
- * Returns the name of the collector.
- *
- * @return string The collector name
- */
function getName();
}
-The value returned by ``getName()`` must be unique in the application. This value
-is also used to access the information later on (see :doc:`/cookbook/testing/profiling`
-for instance).
-
-The ``collect()`` method is responsible for storing the collected data in local
-properties.
+The
+:method:`Symfony\\Component\\HttpKernel\\DataCollector\\DataCollectorInterface::getName`
+method returns the name of the data collector and must be unique in the
+application. This value is also used to access the information later on (see
+:doc:`/cookbook/testing/profiling` for instance).
-.. caution::
-
- As the profiler serializes data collector instances, you should not
- store objects that cannot be serialized (like PDO objects), or you need
- to provide your own ``serialize()`` method.
+The
+:method:`Symfony\\Component\\HttpKernel\\DataCollector\\DataCollectorInterface::collect`
+method is responsible for storing the collected data in local properties.
Most of the time, it is convenient to extend
:class:`Symfony\\Component\\HttpKernel\\DataCollector\\DataCollector` and
populate the ``$this->data`` property (it takes care of serializing the
-``$this->data`` property)::
+``$this->data`` property). Imagine you create a new data collector that
+collects the method and accepted content types from the request::
+
+ // src/AppBundle/DataCollector/RequestCollector.php
+ namespace AppBundle\DataCollector;
- // src/AppBundle/DataCollector/MyCollector.php
use Symfony\Component\HttpKernel\DataCollector\DataCollector;
- class MyCollector extends DataCollector
+ class RequestCollector extends DataCollector
{
public function collect(Request $request, Response $response, \Exception $exception = null)
{
$this->data = array(
- 'variable' => 'value',
+ 'method' => $request->getMethod(),
+ 'acceptable_content_types' => $request->getAcceptableContentTypes(),
);
}
- public function getVariable()
+ public function getMethod()
{
- return $this->data['variable'];
+ return $this->data['method'];
+ }
+
+ public function getAcceptableContentTypes()
+ {
+ return $this->data['acceptable_content_types'];
}
public function getName()
{
- return 'app.my_collector';
+ return 'app.request_collector';
}
}
+The getters are added to give the template access to the collected information.
+
+.. caution::
+
+ As the profiler serializes data collector instances, you should not
+ store objects that cannot be serialized (like PDO objects) or you need
+ to provide your own ``serialize()`` method.
+
.. _data_collector_tag:
Enabling Custom Data Collectors
-------------------------------
-To enable a data collector, define it as a regular service and tag it with
+To enable a data collector, define it as a regular service and tag it as
``data_collector``:
.. configuration-block::
@@ -88,8 +89,8 @@ To enable a data collector, define it as a regular service and tag it with
# app/config/services.yml
services:
- app.my_collector:
- class: AppBundle\DataCollector\MyCollector
+ app.request_collector:
+ class: AppBundle\DataCollector\RequestCollector
public: false
tags:
- { name: data_collector }
@@ -104,8 +105,10 @@ To enable a data collector, define it as a regular service and tag it with
http://symfony.com/schema/dic/services/services-1.0.xsd"
>
-
+
{% endset %}
{% set text %}
{# this is the content displayed when hovering the mouse over
the toolbar panel #}
- Major information goes here -
+Content Type | +
---|
{{ type }} | +