纳伊蒂/phalcon-debugbar

将PHP调试栏与Phalcon集成。是snowair/phalcon-debugbar的分支

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. 支持简单的应用程序、多模块应用程序和微服务应用程序
  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服务中收集信息。
  • 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. 将您的应用程序实例设置到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)

Volt 函数

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