derry/phalcon-debugbar

将 PHP Debug Bar 与 Phalcon 集成。

v1.0.0 2021-11-03 14:51 UTC

This package is not auto-updated.

Last update: 2024-09-27 15:31:42 UTC


README

Latest Stable Version Total Downloads Latest Unstable Version License PHP Version Require

Phalcon Debugbar

PHP Debug BarPhalcon 框架 集成。

中文说明

特性

  1. 普通请求捕获
  2. Ajax 请求捕获
  3. 重定向请求链捕获
  4. 支持简单应用、多模块应用和微应用
  5. 数据收集持久化:保存到 本地文件MongoDBElasticSearch
  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:显示请求头、Cookie、服务器变量、响应头、查询、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. 将您的 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)

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文件的形式存储在持久目录中。您可以通过打开处理程序(右侧的打开图标)将其加载到debugbar表单中。

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

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

快照

Screenshot

Screenshot

Screenshot

Screenshot

Screenshot

Screenshot

Screenshot

Screenshot

Screenshot

Screenshot

Screenshot