3
votes

I want to extend the Symfony2 Debug Toolbar with my own custom data.

I have a service where I want to log specific method calls and then display them in the web debug toolbar.

I read the cookbook article, but it's not very helpful.

I created my own DataCollector class:

class PermissionDataCollector extends DataCollector
{
    private $permissionCalls = array();

    private $permissionExtension;

    public function __construct(PermissionExtension $permissionExtension)
    {
        $this->permissionExtension = $permissionExtension;
    }

    /**
     * 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
     *
     * @api
     */
    public function collect(Request $request, Response $response, \Exception $exception = null)
    {
        $this->permissionCalls = $this->permissionExtension->getPermissionCalls();

        $this->data = array(
            'calls' => $this->permissionCalls
        );
    }
    public function getPermissionCallsCount()
    {
        return count($this->permissionCalls);
    }

    public function getFailedPermissionCallsCount()
    {
        return count(array_filter($this->permissionCalls, array(&$this, "filterForFailedPermissionCalls")));
    }

    private function filterForFailedPermissionCalls($var)
    {
        return $var['success'];
    }

    /**
     * Returns the name of the collector.
     *
     * @return string The collector name
     *
     * @api
     */
    public function getName()
    {
        return 'permission';
    }
}

The PermissionExtension logs all calls and then I want to retrieve this array of calls in PermissionDataCollector.

And a template just outputting {{ collector.permissionCallsCount }}.

The section gets displayed in the in the toolbar but it just shows a 0 which is wrong.

I'm not sure if I'm even doing this right, because the documentation lacks this section. I'm using Symfony 2.1

Has anybody extended the toolbar with custom data?

1
I don't know how to do this from memory, but I do know that FOQElasticaBundle does it to show a log of ElasticSearch queries, which would be a good place to start.Steve
ah great! It works. I basically need to refer to $this->data all the time. Thanks a lot for the bundle reference!Johannes Klauß
hah, shoots and scores :) glad it helped!Steve
Maybe you could answer your own question with a bit more detail, for the rest of us? :-)Potherca

1 Answers

1
votes

ah great! It works. I basically need to refer to $this->data all the time.

The reason for this that ->data is used by the Symfony\Component\HttpKernel\DataCollector\DataCollector and serialized (see DataCollector::serialize).

This is later stored (somehow, I don't know where, but it is later unserialized). If you use own properties the DataCollector::unserialize just prunes your data.

See https://symfony.com/doc/current/profiler/data_collector.html#creating-a-custom-data-collector

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.

Just use $this->data all the time, or implement your own \Serializable serializing.