edemeijer/serialize-debugger

用于调试失败的或危险的 PHP serialize() 调用的工具

v0.1.4 2014-09-07 12:07 UTC

This package is auto-updated.

Last update: 2024-09-27 01:38:53 UTC


README

用于调试失败的或危险的 PHP serialize() 调用的工具。

动机

调试 PHP 的 serialize() 方法的问题可能是一项繁琐的任务,因为它在遇到错误时不会提供很多信息。这可以通过一个例子来最好地说明

<?php

$nestedStruct = [
    'foo' => (object) [
        'validProp' => 42,
        'invalidProp' => function () {},
    ],
    'bar' => [
        'someProp' => 'Hello',
    ],
];

$serialized = serialize($nestedStruct);

上述代码将导致一个致命错误

PHP Fatal error:  Uncaught exception 'Exception' with message 'Serialization of 'Closure' is not allowed' in /path/to/script.php:13
Stack trace:
#0 /path/to/script.php(13): serialize(Array)
#1 {main}
  thrown in /path/to/script.php on line 13

不太有帮助,对吧?这个闭包到底在哪里?想象一下在大型的嵌套结构中发生这种情况,你将面临许多黑暗的调试乐趣。

这就是 serialize-debugger 可以提供帮助的地方。看看下面的 PHP 段落及其输出

Debugger::debugPlaintext($nestedStruct);
Closure - ERROR
    {root}[foo]->invalidProp

现在好多了,我们现在确切地知道问题数据结构中引起麻烦的闭包所在的位置。

要求

serialize-debugger 只支持 PHP 5.4.0 及以上版本。

安装

使用 composer,将其添加到你的 composer.json 中

"edemeijer/serialize-debugger": ">=0.1.1"

然后运行 composer.phar update

API

基本用法

// There are two static convenience methods in the main debugger class:
// Outputting errors and warnings in plain text format
Debugger::debugPlaintext($data);

// Outputting errors and warnings in HTML format
Debugger::debugHTML($data);

// Both methods provide an optional second boolean parameter for verbose output
// This will output not only errors and warnings, but the paths to every element
// in the data set
Debugger::debugPlainText($data, true);

不使用静态便捷方法

$debugger = new Debugger();
$formatter = new PlainTextFormatter();
$result = $debugger->debug($data, $verbose);
echo $formatter->format($result);

许可

MIT - 查看 LICENSE 文件。