简体   繁体   English

如何使用自定义数据扩展Symfony2调试工具栏?

[英]How to extend the Symfony2 Debug Toolbar with custom data?

I want to extend the Symfony2 Debug Toolbar with my own custom data. 我想用我自己的自定义数据扩展Symfony2调试工具栏。

I have a service where I want to log specific method calls and then display them in the web debug toolbar. 我有一个服务,我想记录特定的方法调用,然后在Web调试工具栏中显示它们。

I read the cookbook article , but it's not very helpful. 我阅读了食谱文章 ,但它没有多大帮助。

I created my own DataCollector class: 我创建了自己的DataCollector类:

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 . PermissionExtension记录所有调用,然后我想在PermissionDataCollector检索这个调用数组。

And a template just outputting {{ collector.permissionCallsCount }} . 一个模板只输出{{ collector.permissionCallsCount }}

The section gets displayed in the in the toolbar but it just shows a 0 which is wrong. 该部分显示在工具栏中,但它只显示0是错误的。

I'm not sure if I'm even doing this right, because the documentation lacks this section. 我不确定我是否做得对,因为文档缺少此部分。 I'm using Symfony 2.1 我正在使用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. 我基本上需要一直参考$ this-> data。

The reason for this that ->data is used by the Symfony\\Component\\HttpKernel\\DataCollector\\DataCollector and serialized (see DataCollector::serialize). 原因是 - >数据由Symfony\\Component\\HttpKernel\\DataCollector\\DataCollector并序列化(参见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. 如果您使用自己的属性, DataCollector::unserialize只会修剪您的数据。

See https://symfony.com/doc/current/profiler/data_collector.html#creating-a-custom-data-collector 请参阅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. 由于探查器序列化数据收集器实例,因此不应存储无法序列化的对象(如PDO对象),或者需要提供自己的serialize()方法。

Just use $this->data all the time, or implement your own \\Serializable serializing. 只需始终使用$ this-> data,或者实现自己的\\Serializable化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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