|
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 | + * Returns the collected data. |
21 | 21 | *
|
22 |
| - * @param Request $request A Request instance |
23 |
| - * @param Response $response A Response instance |
24 |
| - * @param \Exception $exception An Exception instance |
25 |
| - */ |
26 |
| - function collect(Request $request, Response $response, \Exception $exception = null); |
27 |
| - |
28 |
| - /** |
29 |
| - * Returns the name of the collector. |
| 22 | + * @return ProfileDataInterface |
30 | 23 | *
|
31 |
| - * @return string The collector name |
| 24 | + * @todo introduce in 3.0 |
32 | 25 | */
|
33 |
| - function getName(); |
| 26 | + public function getCollectedData(); |
| 27 | + } |
| 28 | + |
| 29 | +if the data should be collected just prior to the Profile being saved add the :class:`Symfony\\Component\\Profiler\\DataCollector\\LateDataCollectorInterface`:: |
| 30 | + |
| 31 | + interface LateDataCollectorInterface |
| 32 | + { |
| 33 | + } |
| 34 | + |
| 35 | +The ``getCollectedData()`` method is responsible for storing the data it wants to give |
| 36 | +access to in a :class:`Symfony\\Component\\Profiler\\ProfileData\\ProfileDataInterface`:: |
| 37 | + |
| 38 | + interface ProfileDataInterface extends \Serializable |
| 39 | + { |
| 40 | + public function getName(); |
34 | 41 | }
|
35 | 42 |
|
36 | 43 | The ``getName()`` method must return a unique name. This is used to access the
|
37 | 44 | information later on (see :doc:`/cookbook/testing/profiling` for
|
38 | 45 | instance).
|
39 | 46 |
|
40 |
| -The ``collect()`` method is responsible for storing the data it wants to give |
41 |
| -access to in local properties. |
42 |
| - |
43 | 47 | .. caution::
|
44 | 48 |
|
45 |
| - As the profiler serializes data collector instances, you should not |
| 49 | + As the profiler serializes ProfileData instances, you should not |
46 | 50 | store objects that cannot be serialized (like PDO objects), or you need
|
47 | 51 | to provide your own ``serialize()`` method.
|
48 | 52 |
|
49 |
| -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):: |
| 53 | +Example DataCollector:: |
53 | 54 |
|
54 |
| - class MemoryDataCollector extends DataCollector |
| 55 | + class MemoryDataCollector extends AbstractDataCollector implements LateDataCollectorInterface |
55 | 56 | {
|
56 |
| - public function collect(Request $request, Response $response, \Exception $exception = null) |
| 57 | + private $memoryLimit; |
| 58 | + |
| 59 | + /** |
| 60 | + * Constructor. |
| 61 | + */ |
| 62 | + public function __construct() |
57 | 63 | {
|
58 |
| - $this->data = array( |
59 |
| - 'memory' => memory_get_peak_usage(true), |
60 |
| - ); |
| 64 | + $this->memoryLimit = ini_get('memory_limit'); |
61 | 65 | }
|
62 | 66 |
|
63 |
| - public function getMemory() |
| 67 | + /** |
| 68 | + * {@inheritdoc} |
| 69 | + */ |
| 70 | + public function lateCollect() |
| 71 | + { |
| 72 | + return new MemoryData(memory_get_peak_usage(true), $this->memoryLimit); |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + class MemoryData implements ProfileDataInterface |
| 77 | + { |
| 78 | + private $memory; |
| 79 | + private $memoryLimit; |
| 80 | + |
| 81 | + /** |
| 82 | + * Constructor. |
| 83 | + * |
| 84 | + * @param int $memory The current used memory. |
| 85 | + * @param int $memoryLimit The memory limit. |
| 86 | + */ |
| 87 | + public function __construct($memory, $memoryLimit) |
64 | 88 | {
|
65 |
| - return $this->data['memory']; |
| 89 | + $this->memory = $memory; |
| 90 | + $this->memoryLimit = $this->convertToBytes($memoryLimit); |
66 | 91 | }
|
67 | 92 |
|
| 93 | + /** |
| 94 | + * {@inheritdoc} |
| 95 | + */ |
68 | 96 | public function getName()
|
69 | 97 | {
|
70 | 98 | return 'memory';
|
71 | 99 | }
|
| 100 | + |
| 101 | + /** |
| 102 | + * Returns the memory. |
| 103 | + * |
| 104 | + * @return int The memory |
| 105 | + */ |
| 106 | + public function getMemory() |
| 107 | + { |
| 108 | + return $this->memory; |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * Returns the PHP memory limit. |
| 113 | + * |
| 114 | + * @return int The memory limit |
| 115 | + */ |
| 116 | + public function getMemoryLimit() |
| 117 | + { |
| 118 | + return $this->memoryLimit; |
| 119 | + } |
| 120 | + |
| 121 | + //... |
72 | 122 | }
|
73 | 123 |
|
| 124 | + |
| 125 | + |
| 126 | + |
74 | 127 | .. _data_collector_tag:
|
75 | 128 |
|
76 | 129 | Enabling custom Data Collectors
|
|
0 commit comments