debugchannel / php-client
Requires
- php: >=5.5
- andyhu/php-ref: dev-master
This package is not auto-updated.
Last update: 2024-09-24 06:31:39 UTC
README
DebugChannel 的 PHP 客户端。
通过 Composer 安装
php-client 可在 Composer 上使用 Packagist。您可以使用以下命令在项目根目录中将其安装为项目依赖项
composer require squareproton/debugchannel-php-client
Composer 将自动将 debugchannel\DebugChannel 添加到您的自动加载器。就这么简单。
通过 Github 安装
php-client 存放在 GitHub 上,可以像任何其他本地库一样使用。
通过检出代码
git clone https://github.com/SquareProtonOrg/debugchannel-php-client
或下载 zip 文件
wget https://github.com/debugchannel/debugchannel-php-client/archive/master.zip
这将创建一个名为 debugchannel-php-client
的目录。您只需要包含一个文件 - 位于 debugchannel-php-client/dist/DebugChannel.php
的文件 - 然后您就完成了。
例如
<?php
require "path/to/debugchannel-php-client/dist/DebugChannel.php";
...
如何使用 php-client
php-client 声明在命名空间 debugchannel
中,客户端类名为 DebugChannel
。还有一个小的全局函数集。
类 DebugChannel 的不同方法决定了信息在 debugchannel 上的显示方式。
- ->explore() 显示对象图的精美表示
- ->table() 尽可能以表格数据形式显示
- ->string() 显示纯文本
- ->image() 显示存储在文件中或以 base64 编码的图像
- ->code() 显示语法高亮文本
- ->chat() 显示像即时消息应用一样带有发送者名称的消息
- ->help() 显示帮助信息
初始化
$d = new debugchannel\DebugChannel("192.168.2.158", "greeting");
创建一个指向位于 192.168.2.158
的 DebugChannel 服务器的客户端 $d
,并在 greeting
通道上显示消息。要查看您的消息,请将浏览器指向 http://192.166.2.158/greeting
。
可以在构造函数的第四个参数中设置各种选项。
$d = new debugchannel\DebugChannel(
"192.168.2.158",
"greeting",
null,
array(
'expLvl' => 2 // Set the number of intially expanded levels with the object graph
'maxDepth' => 3 // Sets the recursion depth of with which to explore a object
'showMethods' => false // switches of display of method signatures on objects
)
);
第三个参数与一个认证功能 $apiKey 相关,但目前未使用。
->explore()
适用于任何东西。传递任何内容,它将尽力以精美、类型感知、易于阅读、可探索(如果需要)和有帮助的方式显示其第一个参数。这是完全神奇的 print_r()
和 var_dump
的替代品。
class Person {
private $name; private $age;
public __construct($name, $age) {
$this->name = $name;
$this->age = $age;
}
}
$d->explore("Hello World"); // 1
$d->explore(23); // 2
$d->explore(new Person("John", 32)); // 3
$d->explore(array(1,"hello", new Person("John", 32))); // 4
->string()
有时您只想有一个对象的字符串表示。传递给它的任何内容都将转换为字符串。
$d->string("Hello, World!");
->clear()
$d->clear()
清除当前在您的通道上显示的任何消息。非常适合在新的调试流程开始时“重置”通道。
->table()
您的数据看起来可以以二维表的形式显示吗?
例如
$data = array(
array(1,2,3),
array(4,5,6),
array(7,8,9)
);
$d->table($data);
我们甚至会尝试处理迭代对象。所以这也可能。
$obj = new stdClass();
$obj->name = 'Peter';
$obj->age = 30;
$obj->likes = 'Php';
$data = array( $obj );
$d->table($data);
->code()
调试一些 SQL?生成一些 JavaScript?想语法高亮一些内容?
$d->code( "SELECT * FROM something", "sql" );
我们使用出色的 highlight.js 语法高亮库。支持的语言列表可以在这里查看 http://highlightjs.org/download/。
->help()
需要帮助吗?
$d->help();
在您的通道上输出有用的消息,其中包含使用说明和文档的链接。也适用于检查一切是否正常。
选项
所有向调试服务器发送输出的函数都可以接受一些选项。这些选项包括:
- die; 在发送消息后立即终止脚本执行。以代码 0 退出。
- expand; 选择在传递给 ->explore() 时是否展开对象。在撰写本文时,这尚未在 webapp 中实现。
调试通道定义了两个常量 - DC_DIE
和 DC_EXPAND
- 来表达这些。您可以以数组的形式传递选项,也可以使用位运算符。以下两个 explore 行是等价的。
$d = new DebugChannel('localhost', 'mychannel');
$d->explore( "...", array(DC_DIE, DC_EXPAND) );
$d->explore( "...", DC_DIE | DC_EXPAND );
还有针对 'die' 功能的快捷方式。在导致发送调试消息的任何方法前添加 !
。以下都是等价的。
!$d->explore("I'm going to exit");
$d->explore("I'm going to exit", [DC_DIE] );
$d->explore("I'm going to exit", DC_DIE );
此操作影响的函数完整列表如下:!dc_explore()
、!dc_table()
、!dc_string()
、!dc_code()
、!dc_image()
、!dc_chat()
、!dc_clear()
、!dc_help()
、!$dc->explore()
、!$dc->table()
、!$dc->string()
、!$dc->code()
、!$dc->image()
、!$dc->chat()
、!$dc->clear()
、!$dc->help()
。
静态方法和默认配置
我们都希望调试尽可能快。如果您觉得实例化 DebugChannel 对象太麻烦,有一些有用的全局快捷方式可以消除这种痛苦。无需添加 uses
语句或担心命名空间,无需处理依赖注入,……。
全局函数只是 DebugChannel 方法名称前加 'dc_';所以您本应键入 $d->explore($mything)
,现在您只需键入 dc_explore($mything)
。以下所有操作都如您预期的那样。
dc_clear();
dc_explore(...);
dc_table(...);
dc_code(...)
在幕后,dc_*
函数在首次调用时创建一个新的 DebugChannel 对象,然后重复使用它。可以设置此全局对象的构造函数参数,方法是通过配置文件或通过调用函数 dc_setup()
。
DebugChannel.php 兼容 PSR-0 自动加载器。全局函数不能/不使用自动加载器,因此您需要手动在一个类似于引导的文件中包含文件 'debugchannel/DebugChannel.php'。
dc_setup()
像调用类 DebugChannel 的构造函数一样调用 dc_setup()。随后的任何 dc_*
函数调用都将使用按这种方式配置的 DebugChannel 实例。
例如:
dc_setup( 'debugchannel.com', 'channel' );
dc_explore( $mysqlResultSet );
通过配置文件
如果您没有调用 dc_setup()
,我们将搜索以下三个目录中的 dc_setup.json
配置文件。
- 与 DebugChannel.php 文件所在的相同目录。
- 当前工作目录。
- 由
$_SERVER['HOME']
确定的服务器主目录。
不在您的项目源代码控制中的设置文件意味着每个开发者都可以根据自己的偏好使用不同的通道并设置不同的选项,而无需修改生成调试输出的代码。
设置文件必须是有效的 JSON,其结构如下。
{
"host": "192.168.2.17",
"channel" : "peter",
"apiKey": null,
"options": {
"showPrivateMembers": true,
"expLvl": 2,
"maxDepth": 2,
"showMethods": false
}
}
想要更多吗?
太棒了。PHP 文档器文档详细说明了所有类方法和更多技术细节,可在存储库中找到。要在您最喜欢的浏览器中查看它,请打开 debugchannel-php-client/doc/index.html。