franz-deleon / fdl-debug
一个简单、无依赖、可扩展的PHP调试助手。
Requires
- php: >=5.3.3
This package is not auto-updated.
Last update: 2024-09-24 17:28:32 UTC
README
FdlDebug是一个带有独特功能的简单PHP调试输出器。它类似于var_dump(),但更多!
使用示例
\FdlDebug\Front::i()->pr("Hello Yo!"); // outputs: "Hello Yo!" // procedural style pr("Hello Yo"); // output: "Hello Yo!" // Another example. Imagine you are in a large loop and... for ($i = 0; $i <= 10000; $i++) { // you want to print when the iteration is at 5000 cond_bool($i == 5000)->pr("Yes! its at {$i}"); // you want to print iterations 5006 to 5010 cond_range(5006, 5)->pr($i); // prints: 5006, 5007, 5008, 5009, 5010 // you want to print the end of the loop cond_from('end')->pr($i); // prints: 10000 }
是的,使用它就这么简单!你只需获取Front::i()实例并调用其中一个调试方法。
如何安装
需求
- PHP 5.3
- XDebug(可选)
安装
Composer
如果你的项目使用Composer,只需将其添加到composer.json
"require": { "franz-deleon/fdl-debug": "dev-develop" }
或者使用命令行添加
$> php composer.phar require franz-deleon/fdl-debug
GitHub
没有Composer也没关系。使用git。
$> cd path/to/lib/fdldebug $> git clone git@github.com:franz-deleon/FdlDebug.git .
为过程式项目启用
如果你不使用Composer或使用任何自动加载,包括Bootstrapper.php文件将自动加载库。
include_once 'path/to/fdldebug/Bootstrapper.php'; // call the instance \FdlDebug\Front::i()->pr('wuzzup'); //outputs: "wuzzup"
条件
所谓条件,就是我所说的FdlDebug的精髓。它基本上是在通过Front实例调用打印或var_dumps时添加功能。
1. 布尔条件 - condBoolean(bool $booleanExpression) | cond_bool(bool $booleanExpression)
布尔条件是传递布尔表达式以确定是否打印数据的一种简单方式。
例如,仅当条件评估为true时才打印。
use \FdlDebug\Front as Fdbug; $x = rand(1, 10); // assume $x is 5 Fdbug::i()->condBoolean($x === 5)->pr('Yep its 5'); // outputs: Yep its 5 // function hybrid style cond_bool($x === 5)->pr('Yep its 5');
那么,如果我只想在循环迭代是偶数时打印呢?
for ($x = 1; $x <= 5; ++$x) { Fdbug::i()->condBoolean($x % 2 === 0)->pr("$x is even"); // function hybrid style cond_bool($x % 2 === 0)->pr("$x is even"); } // outputs: // 2 is even // 4 is even
2. 循环范围条件 - loopRange(int $offsetStart [, int $length]) | cond_range(int $offsetStart [, int $length])
循环范围条件在循环内部打印数据范围时非常有用。
例如,仅在循环迭代是第3次到末尾时打印。
use \FdlDebug\Front as Fdbug; for ($x = 1; $x <= 5; ++$x) { Fdbug::i()->loopRange(3)->pr($x); // function hybrid style cond_range(3)->pr($x); } // outputs: // 3 // 4 // 5
loopRange($range [, int $length])还接受长度参数,如果您只想打印特定长度的范围。
for ($x = 1; $x <= 5; ++$x) { Fdbug::i()->loopRange(2, 2)->pr($x); // function hybrid style cond_range(2, 2)->pr($x); } // outputs: // 2 // 3
对于多层循环,您需要在每个嵌套循环的末尾添加rangeNestedEnd()。
for ($x = 1; $x <= 2; $x++) { Fdbug::i()->loopRange(2, 1)->pr("1st:" . $x); // function hybrid style cond_range(2, 1)->pr("1st:" . $x); for ($y = 1; $y <= 3; $y++) { Fdbug::i()->(3, 1)->pr("2nd:" . $y); // function hybrid style cond_range(3, 1)->pr("2nd:" . $y); } // the nested end identifier needs to be place here Fdbug::i()->(3, 1)->rangeNestedEnd(); // function hybrid style cond_range_nested_end(); } // outputs // 2nd:3 // 1st:2 // 2nd:3
3. 循环从条件 - loopFrom(string $expression [, int $length]) | cond_from(string $expression [, int $length])
循环从条件是一个相当动态的条件,设计用于您不知道循环迭代次数的情况。
例如,您想打印mysql资源的末尾迭代。
use \FdlDebug\Front as Fdbug; // asume the end outputs "123" while ($row = mysql_fetch_assoc()) { Fdbug::i()->loopFrom('end')->pr($row['col']); // function hybrid style cond_from('end')->pr($row['col']); } Fdbug::i()->loopFromFlush(); // you need to call loopFromFlush() at the end of the loop // function hybrid style cond_from_flush(); // outputs: // 123
使用loopFrom()时,您需要在循环末尾调用loopFromFlush()以打印数据。
那么,如果我只想从'末尾'打印第3次迭代呢?
for ($x = 1; $x <= 10; ++$x) { // for simplicity, i am using a for loop Fdbug::i()->loopFrom('3rd from end')->pr($x); } Fdbug::i()->loopFromFlush(); // outputs: // 8 // 9 // 10
让我们打印循环的中间部分。
for ($x = 1; $x <= 5; ++$x) { Fdbug::i()->loopFrom('middle', 1)->pr($x); } Fdbug::i()->loopFromFlush(); // outputs: // 3
那么,5个中的第2个中间值之前2次迭代是什么?
for ($x = 1; $x <= 5; ++$x) { Fdbug::i()->loopFrom('2 before median', 1)->pr($x); } Fdbug::i()->loopFromFlush(); // outputs: // 1
loopFrom(string $expression)接受表达式类型语句,因此这些类型的语句是有效的
"first", "beginning", "start", "middle", "median", "2 before middle", "2 after median",
"3rd from start", "4th from last", "5th from end", "end", "last", ...
正如您可能已经注意到的,您还可以将长度变量传递给loopFrom(strin $expression [, int $length])
for ($x = 1; $x <= 10; ++$x) { Fdbug::i()->loopFrom('4th from last', 2)->pr($x); } Fdbug::i()->loopFromFlush(); // outputs: // 7 // 8
在嵌套循环中,您也可以使用多个loopFrom条件(这也适用于其他条件)。
$fdbug = Fdbug::i(); for ($i = 1; $i <= 5; $i++) { $fdbug->loopFrom('3rd from end', 1)->pr("3rd-from-end:" . $i); for ($x = 1; $x <= 5; $x++) { $fdbug->loopFrom('2nd from start', 1)->pr("2nd-from-start:" . $x); } } $fdbug->loopFromFlush(); // now flush everything! // outputs: // 3rd-from-end:3 // 2nd-from-start:2
会话实例条件
开发中
条件链
您可以根据需要链式使用条件。
use \FdlDebug\Front as Fdbug; for ($x = 1; $x <= 10; ++$x) { Fdbug::i() ->condBoolean($x % 2 === 0) ->loopRange(3, 4) ->pr($x) ; // procedural style cond_bool($x % 2 === 0) ->cond_range(3, 4) ->pr($x); } // outputs: // 4 // 6
调试方法
fd_i()- 获取fdldebug实例的函数fd_writer()- 获取写对象实例的函数pr(mixed $value)- 等同于 printNow()prd(mixed $value)- 与 pr() 相同,但会自动终止printObject(object $value)|pr_object- 输出对象信息printNow(mixed $value)|pr_now(mixed $value)- 通过传递参数$value到 Writer 打印内容printBackTrace($showVendor = false)|pr_backtrace($show_vendor = false)- 使用 Writer 打印 PHP 调用栈printFiles($showVendor = false)|pr_files($show_endor = false)- 使用 Writer 打印文件调用栈printGlobal(string $globalType = null)|pr_global(string $globalType)- 打印 PHP 全局变量数据
\\ 'SERVER', 'GET', 'POST', 'FILES', 'REQUEST', 'SESSION', 'ENV', 'COOKIE' \FdlDebug\Front::i()->printGlobal('get'); /** outputs outputs: array (size=28) 'APPLICATION_ENV' => string 'local' (length=5) 'WEB_ENV' => string 'local' (length=5) [...] */
XDebug 方法(扩展插件)
-
printXdebugTracedVar(string $search, bool $showVendor)|prx_trace_var(string $search, bool $showVendor)- 打印目标变量$search的调用栈。此方法通过检查XDEBUG_TRACE=1来利用 XDebug 的跟踪功能示例输出
// in Bootstrap.php line 20 of some mvc framework, pretend below is written: $hello = "hello world"; // Now pretend you are in XController.php of some mvc framework \FdlDebug\Front::i()->printXdebugTracedVar('hello'); /** outputs: array (size=1) 1 => array (size=4) 'file' => string '/someframework/Bootstrap.php' (length=54) 'line' => string '20' (length=2) 'var($hello) assignment' => array (size=1) 0 => string '$hello = 'hello' (length=15) 'initialization' => string '$hello = 'hello world'' (length=22) */
请记住,您需要启用 XDEBUG_TRACE。您可以使用一些 XDebug 浏览器扩展来启用它,或者通过将 XDEBUG_TRACE 传递给
http://domain.com/?XDEBUG_TRACE=1来启用它(不推荐)