1
0
mirror of https://github.com/maximebf/php-debugbar.git synced 2025-01-17 21:38:14 +01:00
php-debugbar/docs/data_collectors.md

111 lines
3.7 KiB
Markdown
Raw Normal View History

2013-06-13 18:48:23 +08:00
# Collecting Data
## Using collectors
2014-01-16 21:41:41 +00:00
Collectors can be added to your debug bar using `addCollector()`.
2013-06-13 18:48:23 +08:00
$debugbar = new DebugBar();
$debugbar->addCollector(new DataCollector\RequestDataCollector());
2014-01-16 21:41:41 +00:00
Each collector as a unique name as defined by its `getName()` method. You can
2013-06-13 18:48:23 +08:00
access collectors using `getCollector($name)`.
$debugbar->addCollector(new DataCollector\MessagesCollector());
$debugbar->getCollector('messages')->addMessage("foobar");
// or:
$debugbar['messages']->addMessage("foobar");
Data will be collected from them when the debug bar is rendered. You can however
collect the data earlier using `collect()`.
$debugbar->collect();
## Creating collectors
Collectors must implement the `DebugBar\DataCollector\DataCollectorInterface`. They
may subclass `DebugBar\DataCollector\DataCollector` which provides utility methods.
Collectors must provide a `getName()` function returning their unique name and a
`collect()` function returning some json-encodable data. The latter will be called at the
same time the `DebugBar::collect()` method is called.
class MyDataCollector extends DebugBar\DataCollector\DataCollector
{
public function collect()
{
return array("uniqid" => uniqid());
}
public function getName()
{
return 'mycollector';
}
}
$debugbar->addCollector(new MyDataCollector());
This however won't show anything in the debug bar as no information are provided
on how to display these data. You could do that manually as you'll see in later chapter
2014-01-16 21:41:41 +00:00
or implement the `DebugBar\DataSource\Renderable` interface.
2013-06-13 18:48:23 +08:00
To implement it, you must define a `getWidgets()` function which returns an array
of key/value pairs where key are control names and values control options as defined
in `JavascriptRenderer::addControl($name, $options)` (see Rendering chapter).
class MyDataCollector extends DebugBar\DataCollector\DataCollector implements DebugBar\DataCollector\Renderable
{
// ...
public function getWidgets()
{
return array(
"mycollector" => array(
"icon" => "cogs",
"tooltip" => "uniqid()",
"map" => "uniqid",
"default" => "''"
)
);
}
}
This will have the result of adding a new indicator to the debug bar.
2014-03-22 16:12:18 -04:00
When implementing the Renderable interface, you may use widgets which are not provided
with the default install. You can add new assets by implementing the `DebugBar\DataCollector\AssetProvider` interface.
to implement it, you must define the `getAssets()` method. It must return an array with the
following keys:
- base\_path: base path of assets (optional, if omitted or null, will use the base path of the JavascriptRenderer)
- base\_url: base url of assets (optional, same as base\_path)
- css: an array of css filenames
- js: an array of javascript filenames
Example:
class MyDbCollector extends DebugBar\DataCollector\DataCollector implements DebugBar\DataCollector\Renderable, DebugBar\DataCollector\AssetProvider
{
// ...
public function getWidgets()
{
return array(
"database" => array(
"icon" => "inbox",
"widget" => "PhpDebugBar.Widgets.SQLQueriesWidget",
"map" => "pdo",
"default" => "[]"
)
);
}
public function getAssets()
{
return array(
'css' => 'widgets/sqlqueries/widget.css',
'js' => 'widgets/sqlqueries/widget.js'
);
}
2014-07-26 12:00:45 +01:00
}