appbels / phalcon-debugbar
Requires
- php: >=5.5.0
- filp/whoops: ^2
- maximebf/debugbar: ^1
This package is auto-updated.
Last update: 2022-12-12 13:09:43 UTC
README
Phalcon Debugbar
整合 PHP Debug Bar 到 Phalcon 框架。
特性
- 常规请求捕获
- Ajax 请求捕获
- 重定向请求链捕获
- 支持简单的 App、多模块 App 和微 App
- 数据收集持久化:保存到 本地文件 或 MongoDB,或 ElasticSearch
- 通过 sessionid 存储数据,更便于团队开发。
- 您可以关闭注入的 Debugbar,然后在新的浏览器标签页中访问
/_debugbar/open
来查看数据(也可以禁用)。 - Whoops 集成,并与 debugbar 稳定工作。
- 支持 Phalcon 1.3.x、2.x、3.x,PHP5.5~7.1
支持收集器
- MessagesCollector:收集自定义消息,支持标量、数组和对象
- TimeDataCollector:收集自定义时间测量
- ExceptionsCollector:向 debugbar 添加异常对象
- 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\Logger
和 Monolog- 从
log
服务中收集信息。
- 从
- CacheCollectors:显示缓存摘要(保存、获取、增加、减少、失败),以及每个缓存操作的详细信息。
- 从
cache
服务中收集信息。
- 从
快速开始
composer
-
安装
php composer.phar require --dev snowair/phalcon-debugbar
-
更新
php composer.phar update snowair/phalcon-debugbar
修改 index.php
-
将您的应用实例设置为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
-
在处理应用之前,注册并启动调试栏提供者。
(new Snowair\Debugbar\ServiceProvider())->start(); // after start the debugbar, you can do noting but handle your app right now. echo $application->handle()->getContent();
-
可选使用Whoops,修改index.php,在debugbar服务
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
复制到您的配置目录,并更改您想要的任何设置。然后通过以下方式使用您的调试栏配置文件:
(new Snowair\Debugbar\ServiceProvider('your-debugbar-config-file-path'))->start();
向调试栏发送自定义消息
\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 ) }}
多模块
通常,如果您遵循以下规则,您不需要修改其他任何文件
- 所有缓存服务的名称都包含
cache
。 - 所有数据库服务的名称以
db
开头或以db
结尾。 - 访问
/_debugbar/open?m={modulename}
以打开独立的调试栏页面。
如果您的服务名称不匹配这些规则,您需要将其附加到调试栏
// 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文件存储在持久化目录中。您可以通过打开处理程序(右侧的打开图标)将其加载到调试栏中。
-
如果调试栏不工作,最可能的原因是其中一个或多个收集器在运行时触发了错误。您可以修改调试栏配置文件,逐个关闭收集器,重试,直到找到导致问题的收集器。
-
对于任何问题,您可以在Github上创建一个问题。