雪域/phalcon-debugbar

将PHP调试条与Phalcon集成。

v1.2.6 2017-10-15 16:32 UTC

README

Latest Stable Version Total Downloads Latest Unstable Version License

Phalcon调试条

PHP调试条Phalcon框架集成。

中文说明

功能

  1. 普通请求捕获
  2. Ajax请求捕获
  3. 重定向请求链捕获
  4. 支持简单的App、多模块App和微服务App
  5. 收集的数据持久化:保存到本地文件MongoDB,或ElasticSearch
  6. 使用sessionid存储数据,更适合团队开发。
  7. 您可以关闭注入调试条,并在新的浏览器标签页中访问/_debugbar/open来查看数据(它也可以被禁用)。
  8. Whoops集成,调试条与它配合良好。
  9. 支持Phalcon 1.3.x、2.x、3.x,PHP5.5~7.1

支持收集器

  • MessagesCollector:收集自定义消息,支持标量、数组和对象
  • TimeDataCollector:收集自定义时间度量。
  • ExceptionsCollector:向调试条添加异常对象。
  • MemoryCollector:收集内存使用情况。
  • QueryCollector:捕获每个SQL语句,测量每个SQL的耗时,显示每个SELECT语句的EXPLAIN结果
    • db服务收集信息。仅适用于Phalcon ORM。
  • DoctrineCollector:捕获Doctrine中的每个SQL语句,测量每个SQL的耗时。
    • entityManager服务收集信息。仅适用于Doctrine ORM。
  • RouteCollector:显示当前请求的路由信息。
    • router服务收集信息。
  • ViewCollector:显示所有已渲染的模板,测量每个模板的耗时,显示所有模板变量。
    • view服务收集信息。
  • PhalconRequestCollector:显示请求头、Cookies、服务器变量、响应头、查询、POST数据、原始体
    • request服务收集信息。
  • ConfigCollector:显示config服务中的数据。
    • config服务收集信息。
  • SessionCollectior:显示会话数据
    • session服务收集信息。
  • SwiftMailCollector:邮件发送器信息
    • mail服务收集信息。
  • LogsCollectors:显示当前请求的日志。支持Phalcon\LoggerMonolog
    • log服务收集信息。
  • CacheCollectors:显示缓存摘要(保存、获取、增、减、失败),以及每个缓存操作的详细信息。
    • cache服务收集信息。

快速开始

composer

  • 安装

    php composer.phar require --dev snowair/phalcon-debugbar
    
  • 更新

    php composer.phar update snowair/phalcon-debugbar
    

修改index.php

  1. 将您的App实例设置为DI

    $application = new Phalcon\Mvc\Application( $di ); // Important: mustn't ignore $di param . The Same Micro APP: new Phalcon\Mvc\Micro($di);
    $di['app'] = $application; //  Important
  2. 在处理应用程序之前,注册和启动调试条提供者。

    (new Snowair\Debugbar\ServiceProvider())->start();
    // after start the debugbar, you can do noting but handle your app right now.
    echo $application->handle()->getContent();
  3. 可选使用Whoops,修改index.php,在调试条服务start()方法下方添加以下代码。

    (new \Snowair\Debugbar\Whoops\WhoopsServiceProvider($di));
    

修改ACL代码

以下是一个INVO的示例

public function beforeDispatch(Event $event, Dispatcher $dispatcher)
    {
        $auth = $this->session->get('auth');
        if (!$auth){
            $role = 'Guests';
        } else {
            $role = 'Users';
        }
        
        $controller = $dispatcher->getControllerName();
        $action = $dispatcher->getActionName();
        
        /* Debugbar start */
        $ns = $dispatcher->getNamespaceName();
        if ($ns=='Snowair\Debugbar\Controllers') {
            return true;
        }
        /* Debugbar end */
        
        $acl = $this->getAcl();
        $allowed = $acl->isAllowed($role, $controller, $action);
        if ($allowed != Acl::ALLOW) {
            $dispatcher->forward(array(
                'controller' => 'errors',
                'action'     => 'show401'
            ));
                        $this->session->destroy();
            return false;
        }
    }

数据持久化

对于file驱动程序,默认的存储调试条数据的目录是Runtime/phalcon。如果不存在,它将自动创建。您可以通过重新配置来更改它。

对于 mongodb 驱动程序,您必须安装 mongodb 扩展并安装 phplib:composer require mongodb/mongodb

对于 elastic 驱动程序,您必须安装 phplib:composer require elasticsearch/elasticsearch:some-version

关于 baseUri

请注意您项目的 baseUri 配置,您必须为您的 uri 服务设置正确的 baseUri。

如果您使用的是 apache,您应该启用 Rewrite 模块,并在 baseUri 目录下创建一个 .htaccess 文件。

如果您使用的是 nginx,您应该启用 Rewrite 模块,并像这样编辑服务器配置中的 location 块

    location @rewrite {
        # replace 'baseuri' to your real baseuri
        rewrite ^/baseuri/(.*)$ /baseuri/index.php?_url=/$1;
    }

更多

使用您的配置

config/debugbar.php 复制到您的配置目录,并根据需要更改任何设置。然后通过以下方式使用您的 debugbar 配置文件:

(new Snowair\Debugbar\ServiceProvider('your-debugbar-config-file-path'))->start();

向 debugbar 发送自定义消息

\PhalconDebug::startMeasure('start-1','how long');        // startMeasure($internal_sign_use_to_stop_measure, $label)
\PhalconDebug::addMeasurePoint('start');                  // measure the spent time from latest measurepoint to now.
\PhalconDebug::addMessage('this is a message', 'label');  // add a message using a custom label.
\PhalconDebug::info($var1,$var2, $var3, ...);  // add many messages once a time. See PSR-3 for other methods name.(debug,notice,warning,error,...)
\PhalconDebug::addMessageIfTrue('1 == "1"', 1=='1','custom_label'); // add message only when the second parameter is true
\PhalconDebug::addMessageIfTrue('will not show', 1=='0');
\PhalconDebug::addMessageIfFalse('1 != "0" ', 1=='0');       // add message only when the second parameter is false
\PhalconDebug::addMessageIfNull('condition is null', Null ); // add message only when the second parameter is NULL
\PhalconDebug::addMessageIfEmpty('condition is emtpy', $condition ); // add message only when the second parameter is empty
\PhalconDebug::addMessageIfNotEmpty('condition is not emtpy', $condition=[1] ); // add message only when the second parameter is not empty
\PhalconDebug::addException(new \Exception('oh , error'));
\PhalconDebug::addMeasurePoint('stop');
\PhalconDebug::stopMeasure('start-1');                    // stopMeasure($internal_sign_use_to_stop_measure)

Voltp 函数

addMessage
addMessageIfTrue
addMessageIfFalse
addMessageIfNull
addMessageIfEmpty
addMessageIfNotEmpty
addException
addMeasurePoint
startMeasure
stopMeasure
debug/info/notice/warning/error/emergency/critical

示例

{{ debug( var1, var2 )}}
{{ info( var1, var2 )}}
{{ addMessageIfTrue('$var === true', var ) }}

多模块

通常,如果您遵循以下规则,您不需要修改其他任何文件

  1. 所有缓存服务名称都包含 cache
  2. 所有数据库服务名称以 db 开头或以 db 结尾。
  3. 访问 /_debugbar/open?m={modulename} 以打开独立的 debugbar 页面。

如果您的服务名称不符合这些规则,您需要将其附加到 debugbar

// service.php
$di->set('read-db-test',function(...)); // db service
$di->set('redis',function(...)); // cache service
if ( $di->has('debugbar') ) {
    $debugbar = $di['debugbar'];
    $debugbar->attachDb('read-db-test');
    $debugbar->attachCache('redis');
}

故障排除

  • 我强烈建议您为您的项目分配一个 主机域名,并将 uri 服务的 baseUri 设置为 /

  • 对于 ajax/json 请求,调试数据仅以 json 文件的形式存储在持久目录中。您可以通过 Openhandler(右侧的打开图标)将其加载到 debugbar 中。

  • 如果 debugbar 无法正常工作,最可能的原因是一个或多个收集器在运行时触发了错误。您可以修改 debugbar 配置文件,逐个关闭收集器,重试直到找到导致问题的收集器。

  • 对于任何问题,您可以在 Github 上提交一个 Issue。

快照

Screenshot

Screenshot

Screenshot

Screenshot

Screenshot

Screenshot

Screenshot

Screenshot

Screenshot

Screenshot

Screenshot