PageRenderTime 43ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/cookbook/profiler/data_collector.rst

https://github.com/sebio/symfony-docs
ReStructuredText | 169 lines | 125 code | 44 blank | 0 comment | 0 complexity | 742bd5f53f440e1aa95eff4f1296774c MD5 | raw file
  1. .. index::
  2. single: Profiling; Data Collector
  3. How to create a custom Data Collector
  4. =====================================
  5. The Symfony2 :doc:`Profiler </book/internals/profiler>` delegates data
  6. collecting to data collectors. Symfony2 comes bundles with a few of them, but
  7. you can easily create your own.
  8. Creating a Custom Data Collector
  9. --------------------------------
  10. Creating a custom data collector is as simple as implementing the
  11. :class:`Symfony\\Component\\HttpKernel\\DataCollector\\DataCollectorInterface`::
  12. interface DataCollectorInterface
  13. {
  14. /**
  15. * Collects data for the given Request and Response.
  16. *
  17. * @param Request $request A Request instance
  18. * @param Response $response A Response instance
  19. * @param \Exception $exception An Exception instance
  20. */
  21. function collect(Request $request, Response $response, \Exception $exception = null);
  22. /**
  23. * Returns the name of the collector.
  24. *
  25. * @return string The collector name
  26. */
  27. function getName();
  28. }
  29. The ``getName()`` method must return a unique name. This is used to access the
  30. information later on (see the section about functional tests above for
  31. instance).
  32. The ``collect()`` method is responsible for storing the data it wants to give
  33. access to in local properties.
  34. .. caution::
  35. As the profiler serializes data collector instances, you should not
  36. store objects that cannot be serialized (like PDO objects), or you need
  37. to provide your own ``serialize()`` method.
  38. Most of the time, it is convenient to extend
  39. :class:`Symfony\\Component\\HttpKernel\\DataCollector\\DataCollector` and
  40. populate the ``$this->data`` property (it takes care of serializing the
  41. ``$this->data`` property)::
  42. class MemoryDataCollector extends DataCollector
  43. {
  44. public function collect(Request $request, Response $response, \Exception $exception = null)
  45. {
  46. $this->data = array(
  47. 'memory' => memory_get_peak_usage(true),
  48. );
  49. }
  50. public function getMemory()
  51. {
  52. return $this->data['memory'];
  53. }
  54. public function getName()
  55. {
  56. return 'memory';
  57. }
  58. }
  59. .. _data_collector_tag:
  60. Enabling Custom Data Collectors
  61. -------------------------------
  62. To enable a data collector, add it as a regular service in one of your
  63. configuration, and tag it with ``data_collector``:
  64. .. configuration-block::
  65. .. code-block:: yaml
  66. services:
  67. data_collector.your_collector_name:
  68. class: Fully\Qualified\Collector\Class\Name
  69. tags:
  70. - { name: data_collector }
  71. .. code-block:: xml
  72. <service id="data_collector.your_collector_name" class="Fully\Qualified\Collector\Class\Name">
  73. <tag name="data_collector" />
  74. </service>
  75. .. code-block:: php
  76. $container
  77. ->register('data_collector.your_collector_name', 'Fully\Qualified\Collector\Class\Name')
  78. ->addTag('data_collector')
  79. ;
  80. Adding Web Profiler Templates
  81. -----------------------------
  82. When you want to display the data collected by your Data Collector in the web
  83. debug toolbar or the web profiler, create a Twig template following this
  84. skeleton:
  85. .. code-block:: jinja
  86. {% extends 'WebProfilerBundle:Profiler:layout.html.twig' %}
  87. {% block toolbar %}
  88. {# the web debug toolbar content #}
  89. {% endblock %}
  90. {% block head %}
  91. {# if the web profiler panel needs some specific JS or CSS files #}
  92. {% endblock %}
  93. {% block menu %}
  94. {# the menu content #}
  95. {% endblock %}
  96. {% block panel %}
  97. {# the panel content #}
  98. {% endblock %}
  99. Each block is optional. The ``toolbar`` block is used for the web debug
  100. toolbar and ``menu`` and ``panel`` are used to add a panel to the web
  101. profiler.
  102. All blocks have access to the ``collector`` object.
  103. .. tip::
  104. Built-in templates use a base64 encoded image for the toolbar (``<img
  105. src="src="data:image/png;base64,..."``). You can easily calculate the
  106. base64 value for an image with this little script: ``echo
  107. base64_encode(file_get_contents($_SERVER['argv'][1]));``.
  108. To enable the template, add a ``template`` attribute to the ``data_collector``
  109. tag in your configuration:
  110. .. configuration-block::
  111. .. code-block:: yaml
  112. services:
  113. data_collector.your_collector_name:
  114. class: Fully\Qualified\Collector\Class\Name
  115. tags:
  116. - { name: data_collector, template: "YourBundle:Collector:templatename", id: "your_collector_name" }
  117. .. code-block:: xml
  118. <service id="data_collector.your_collector_name" class="Fully\Qualified\Collector\Class\Name">
  119. <tag name="data_collector" template="YourBundle:Collector:templatename" id="your_collector_name" />
  120. </service>
  121. .. code-block:: php
  122. $container
  123. ->register('data_collector.your_collector_name', 'Fully\Qualified\Collector\Class\Name')
  124. ->addTag('data_collector', array('template' => 'YourBundle:Collector:templatename', 'id' => 'your_collector_name'))
  125. ;