it-al-perugia/phalcon-debugbar

将PHP调试栏与Phalcon集成。

v1.0.0 2022-12-21 08:43 UTC

This package is not auto-updated.

Last update: 2024-09-26 15:04:14 UTC


README

Latest Stable Version Total Downloads Latest Unstable Version License

Phalcon Debugbar

PHP调试栏Phalcon框架集成。

中文说明

功能

  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服务中的数据。
    • 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;
        }
    }

数据持久化

对于文件驱动程序,默认的存储调试栏数据的目录是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