如何将东西记录到Zend Developer Tools工具栏?

时间:2013-12-02 10:20:33

标签: zend-framework2 developer-tools

可以在Zend Developer Tools工具栏中记录ZF2控制器中的一些变量吗?

3 个答案:

答案 0 :(得分:7)

好吧,我创建了一个收集器来在ZendDevelopersTool上添加个人信息。 您需要来自Controller的信息,而这是我从未尝试过的。 :)

我将告诉你我是如何创建收藏家的,也许你可以找到一些从控制器获取信息的方法。

MyModule\module.config.php

/* ZendDeveloperTools Configuration */
'invokables' => array(
    'MyModule\ConfigCollector'   => 'MyModule\Collector\ConfigCollector',
),

'view_manager' => array(
    'template_map' => array(
        'zend-developer-tools/toolbar/mymodule-configs' => __DIR__ . '/../view/zend-developer-tools/toolbar/mymodule-configs.phtml',
    ),
),

'zenddevelopertools' => array(
    'profiler' => array(
        'collectors' => array(
            'mymodule_configs' => 'MyModule\ConfigCollector',
        ),
    ),
    'toolbar' => array(
        'entries' => array(
            'mymodule_configs' => 'zend-developer-tools/toolbar/mymodule-configs',
        ),
    ),
),

MyModule\Module.php上,你需要加载你的invokables,我这样做是通过添加以下方法来实现的:

public function getServiceConfig()
{
    return include __DIR__ . '/config/module.config.php';
}

在您的观看次数上添加.phtml文件:

MyModule/view/zend-developer-tools/toolbar/mymodule-configs.phtml

并添加,例如:

<?php /* @var $collector \DoctrineORMModule\Collector\MappingCollector */ ?>
<div class="zdt-toolbar-entry">
    <div class="zdt-toolbar-preview">
        <img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABoAAAAaCAYAAACpSkzOAAAHMUlEQVRIiYWWW2wU1xnHf2dm71fv2uv1GszaFLDTEIJcMJe0AiJuJYYEBbWkJKKq0qql9Kmq+tCq6kVtQ+OqUVDJW9qXNlKE1DQRAQLiJlqT1OFmOxjWXOwFX9b22rs7s9eZOX1Y45CUku/lHM0c/X/zfeecb/6CLw4b4ANqAD/gmH1eBjRgBsgBxqNExCPeOYF6hGiZ1xTf0hhfuCbesjBeEwp5LSlFenJSHx8bSSZvDV64N3T7qJTyFp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kP8FZsQ6v1dQpnEAAAAASUVORK5CYII=" alt="VnsDesenvolvimento">
        <span class="zdt-toolbar-info">
            Configurações Gerais
        </span>
    </div>
    <div class="zdt-toolbar-detail">
        <span class="zdt-toolbar-info">
            <span class="zdt-detail-label">Environment</span>
            <span class="zdt-detail-value">
                <?php echo $this->collector->getEnvironment(); ?>
            </span>
        </span>

        <span class="zdt-toolbar-info">
            <span class="zdt-detail-label">Default Locale</span>
            <span class="zdt-detail-value">
                <?php echo $this->collector->getDefaultLocale(); ?>
            </span>
        </span>

        <span class="zdt-toolbar-info">
            <span class="zdt-detail-label">Time Zone</span>
            <span class="zdt-detail-value">
                <?php echo $this->collector->getTimeZone(); ?>
            </span>
        </span>

    </div>
</div>

现在,最后一部分! 我们需要创建一个收集器,它将获取我们想要显示的所有信息:

<?php
namespace MyModule\Collector;

use ZendDeveloperTools\Collector;

use Zend\Mvc\MvcEvent;

/**
 * Collector to be used in ZendDeveloperTools to record and display personal information
 *
 * @license MIT
 * @author  Vinicius Garcia <vinigar[...].com>
 */
class ConfigCollector extends \ZendDeveloperTools\Collector\AbstractCollector
{

    /**
     * {@inheritDoc}
     */
    public function getName()
    {
        return 'mymodule_configs';
    }

    /**
     * {@inheritDoc}
     */
    public function getPriority()
    {
        return 10;
    }

    /**
     * {@inheritDoc}
     */
    public function collect(MvcEvent $mvcEvent)
    {
        $date = new \DateTime();

        $this->data = array(
            'environment' => ucfirst(getenv('APPLICATION_ENV') ?: 'production'),
            'timezone' => $date->getTimezone()->getName(),
            'defaultlocale' => \Locale::getDefault(),
        );
    }

    /**
     * Returns the environment
     *
     * @return string
     */
    public function getEnvironment()
    {
        return $this->data['environment'];
    }

    /**
     * Returns the timezone
     *
     * @return string
     */
    public function getTimeZone()
    {
        return $this->data['timezone'];
    }

    /**
     * Returns the default locale
     *
     * @return string
     */
    public function getDefaultLocale()
    {
        return $this->data['defaultlocale'];
    }
}

此类有方法,例如getDefaultLocale(),我们在zend-developer-tools/toolbar/mymodule-configs.phtml上使用它们。

它应该是这样的:

My Configs on ZendDeveloperTool

您可以尝试调整这段代码,以满足您的需求。 如果您发现如何从控制器获取信息,请与我们分享!

修改

ConfigCollector课程中,请注意方法collect(MvcEvent $mvcEvent)

如您所见,那里有一个MvcEvent对象。也许您可以使用它来传递您的个人信息。

答案 1 :(得分:2)

更简单地说,它是可能的:) 我对自己这样做的方式并不感到自豪,但是 - 众所周知的借口 - 它有效。 如果你有任何建议,我会很高兴但很高兴听到他们的意见。

如何创建上面解释的自定义收藏家@ vinigarcia87。

要在Controller(以及任何其他地方)中记录某些内容,然后在收集器中获取该数据,我在模块中创建了一个新类,其中包含静态字段和静态getter / setter,我将其用作代理。这是一些代码:

namespace MyModule\Log;

class MyLogger {
    private static $messages = array();

    public static function log($message)
    {
        self::$messages[] = $message;
    }
    public static function getMessages()
    {
        return self::$messages;
    }
}

在控制器的动作中:

MyModule\Log\Logger::log('Informational message');

在收集器收集方法中:

public function collect(MvcEvent $mvcEvent)
{
    $this->data = array(
        'messages'     => MyModule\Log\Logger::getMessages(),
    );
}
public function getMessages()
{
    return $this->data['messages'];
}

现在可以通过以下方式在工具栏条目.pthml文件中访问此消息:  <?php \Zend\Debug\Debug::dump($this->collector->getMessages()); ?>

你的意见......

答案 2 :(得分:2)

您还可以使用我的包来记录任何数据,并使用Tracy / Debugger突出显示并向您的转储数据添加树。

renat-magadiev/zf3-bar-logger

所以你可以简单地写一下:

<?php

\BarLogger\Collector\DebugCollector::barDump(['testing dump' => ['asd', 'asdad', 'fdgfdsg', 'gfgsd']], 'testing dump');

?>

或简单地在快捷方式中使用全局函数:

<?php

barDump(['testing dump' => ['asd', 'asdad', 'fdgfdsg', 'gfgsd']], 'testing dump');

?>

preview