简体   繁体   中英

How to extend the Symfony2 Debug Toolbar with custom data?

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM