agashe/var-dumper

PHP中var_dump函数的更佳替代方案

0.1.1 2024-09-11 11:05 UTC

This package is auto-updated.

Last update: 2024-09-11 11:06:39 UTC


README

变量转储器,用于调试,旨在成为PHP标准var_dump函数的更好替代方案。

特性

  • 易于使用,无需配置
  • 转储PHP中所有类型的数据,包括(int、float、字符串、bool、对象、数组、资源、枚举、闭包和匿名类)
  • 自动识别已知模式的文本,如电子邮件、URL等
  • 支持将转储输出到CLI、Web内容和文件
  • CLI的漂亮彩色输出
  • 支持折叠/展开对象/数组/长文本的交互式Web调试
  • 支持将转储输出到.json文件
  • 可用于日志,使用转储到文件选项

安装

composer require agashe/var-dumper

文档

要开始使用Var-Dumper,您只需将autoload.php文件包含到您的PHP脚本中,然后调用其中一个辅助函数即可。

<?php

require 'vendor/autoload.php';

d("data 1", "data 2");

前一个示例的输出将是

[ Time : 11-01-2024 07:43:24 / File : index.php / Line : 5 ] 

string (11) => "hello world"
integer => 123

对于每次转储调用,转储器会自动添加时间、文件名和行号,从而使在大项目中调试和日志文件变得更容易。

Var-Dumper提供了4个辅助函数,可以在项目的任何位置使用

d(...$vars)dump(...$vars)函数

这两个函数相同,它们都可以接受任意数量的变量,并将根据您的输出类型(CLI或Web)提供输出。

d(null , 10.59, true);

dump(null , 10.59, true);

以下是输出

[ Time : 11-01-2024 07:59:56 / File : test2.php / Line : 5 ] 

NULL
double => 10.59
boolean => true

[ Time : 11-01-2024 07:59:56 / File : test2.php / Line : 7 ] 

NULL
double => 10.59
boolean => true

命名只是为了兼容性,例如,如果您想替换旧的转储器,它使用ddump作为它们的函数名。

dd(...$vars)函数

dd函数代表“转储并退出”,与之前的两个函数不同,这个函数将在转储完成后终止执行。

如果您将转储用于Web(在浏览器中),转储器将返回“500服务器错误”HTTP状态码。

dump_tp_file($filePath, ...$vars)函数

将转储到文件接受一个必选参数,即您的变量路径之前。该参数是转储文件的完整路径。

class Foo {
    public $name;
    private $age;
}

$foo = new Foo();
$foo->name = "hello world";

dump_to_file(__DIR__ . '/path/to/dump.txt' , $foo);

现在让我们检查dump.txt,我们应该看到以下内容

[ Time : 11-01-2024 08:12:46 / File : index.php / Line : 12 ] 

object (Foo) #2 (2) => { 
    name => "hello world" 
    age (private) => NULL 
} 

到目前为止,转储器支持将转储输出到常规.txt.json文件。在.json文件的情况下,转储器会自动检测扩展。

.txt文件的情况下,新的转储将被附加到原始内容。首先将旧内容解码,然后附加新的json格式化内容,然后将所有内容重新编码并保存。

假设我们有一个名为data.json.json文件,其内容如下

{
    "some_data": {
        "item_1": "data_1",
        "item_2": "data_2",
        "item_3": "data_3"
    }
}

现在让我们使用之前示例中的json文件

// ...

dump_to_file(__DIR__ . '/path/to/data.json' , $foo);

现在如果我们打开data.json,我们会发现内容已被附加

{
    "some_data": {
        "item_1": "data_1",
        "item_2": "data_2",
        "item_3": "data_3"
    },
    "dump_e2bab7e": {
        "timestamp": {
            "time": "11-01-2024 08:23:55",
            "file": "index.php",
            "line": 13
        },
        "data": {
            "object (Foo) #2 (2)": {
                "name": "hello world",
                "age (private)": "NULL"
            }
        }
    }
}

dump_e2bab7e只是一个自动生成的唯一哈希标识符,用于防止转储之间的冲突。

示例

在以下示例中,我们将看到转储器的不同使用案例以及CLI / WEB / 文件 / JSON输出的输出外观。

基本数据类型

d('ahmed', 5.6, false, M_PI, NULL);
dump_to_file(__DIR__ . 'data.txt', 'ahmed', 5.6, false, M_PI, NULL);
dump_to_file(__DIR__ . 'data.json', 'ahmed', 5.6, false, M_PI, NULL);

输出CLI / TXT FILE

[ Time : 11-01-2024 09:51:28 / File : index.php / Line : 5 ] 

string (5) => "ahmed"
double => 5.6
boolean => false
double => 3.1415926535898
NULL

输出JSON

{
    "dump_712db6d": {
        "timestamp": {
            "time": "11-01-2024 09:51:28",
            "file": "index.php",
            "line": 7
        },
        "data": {
            "string (5)": "ahmed",
            "double": 3.1415926535898,
            "boolean": "false",
            "value": null
        }
    }
}

输出WEB

Basic Data Web Output

文本模式

d("/^([A-Za-z1-9])$/");
d(serialize([1, 2, 3]));
d("http://www.helloworld.com");
d("test@example.com");
d('20-5-1995');

dump_to_file(__DIR__ . 'data.txt', "/^([A-Za-z1-9])$/");
dump_to_file(__DIR__ . 'data.txt', serialize([1, 2, 3]));
dump_to_file(__DIR__ . 'data.txt', "http://www.helloworld.com");
dump_to_file(__DIR__ . 'data.txt', "test@example.com");
dump_to_file(__DIR__ . 'data.txt', '20-5-1995');

dump_to_file(__DIR__ . 'data.json', "/^([A-Za-z1-9])$/");
dump_to_file(__DIR__ . 'data.json', serialize([1, 2, 3]));
dump_to_file(__DIR__ . 'data.json', "http://www.helloworld.com");
dump_to_file(__DIR__ . 'data.json', "test@example.com");
dump_to_file(__DIR__ . 'data.json', '20-5-1995');

输出CLI / TXT FILE

[ Time : 11-01-2024 11:05:55 / File : index.php / Line : 5 ] 

RegExp (17) => /^([A-Za-z1-9])$/

[ Time : 11-01-2024 11:05:55 / File : index.php / Line : 6 ] 

Serializable (30) => "a:3:{i:0;i:1;i:1;i:2;i:2;i:3;}"

[ Time : 11-01-2024 11:05:55 / File : index.php / Line : 7 ] 

URL (25) => "http://www.helloworld.com"

[ Time : 11-01-2024 11:05:55 / File : index.php / Line : 8 ] 

Email (16) => "test@example.com"

[ Time : 11-01-2024 11:05:55 / File : index.php / Line : 9 ] 

Date/Time (9) => "20-5-1995"

输出JSON

{
    "dump_82c13d8": {
        "timestamp": {
            "time": "11-01-2024 11:06:49",
            "file": "index.php",
            "line": 17
        },
        "data": {
            "RegExp (17)": "\/^([A-Za-z1-9])$\/"
        }
    },
    "dump_716e868": {
        "timestamp": {
            "time": "11-01-2024 11:06:49",
            "file": "index.php",
            "line": 18
        },
        "data": {
            "Serializable (30)": "a:3:{i:0;i:1;i:1;i:2;i:2;i:3;}"
        }
    },
    "dump_8a02891": {
        "timestamp": {
            "time": "11-01-2024 11:06:49",
            "file": "index.php",
            "line": 19
        },
        "data": {
            "URL (25)": "http:\/\/www.helloworld.com"
        }
    },
    "dump_3d24331": {
        "timestamp": {
            "time": "11-01-2024 11:06:49",
            "file": "index.php",
            "line": 20
        },
        "data": {
            "Email (16)": "test@example.com"
        }
    },
    "dump_47530be": {
        "timestamp": {
            "time": "11-01-2024 11:06:49",
            "file": "index.php",
            "line": 21
        },
        "data": {
            "Date\/Time (9)": "20-5-1995"
        }
    }
}

输出WEB

Text patterns Web Output

对象

class Foo
{
    private $greeting = "hello";
    public $bar;
}

class Bar
{
    public $name;
    private $age;
}


$bar = new Bar();

$bar->name = 'ahmed';

$foo = new Foo();

$foo->bar = $bar;

d($foo);
dump_to_file(__DIR__ . '/data.txt', $foo);
dump_to_file(__DIR__ . '/data.json', $foo);

输出CLI / TXT FILE

[ Time : 11-01-2024 11:22:07 / File : index.php / Line : 26 ] 

object (Foo) #4 (2) => {
    greeting (private) => "hello"
    [bar] object (Bar) #2 (2) => {
        name => "ahmed"
        age (private) => NULL
    }
}

输出JSON

{
    "dump_c5606d6": {
        "timestamp": {
            "time": "11-01-2024 11:22:07",
            "file": "index.php",
            "line": 27
        },
        "data": {
            "object (Foo) #4 (2)": {
                "greeting (private)": "hello",
                "[bar] object (Bar) #2 (2)": {
                    "name": "ahmed",
                    "age (private)": "NULL"
                }
            }
        }
    }
}

输出WEB

Objects Web Output

对象和数组可以折叠/展开

Objects Web Output

匿名类

$class = new class
{
    public $name;

    public function __construct()
    {
        $this->name = 'omar';
    }
};

d($class);
dump_to_file(__DIR__ . '/data.txt', $class);
dump_to_file(__DIR__ . '/data.json', $class);

输出CLI / TXT FILE

[ Time : 11-01-2024 11:26:41 / File : index.php / Line : 15 ] 

object (class@anonymous) #2 (1) => {
    name => "omar"
}

输出JSON

{
    "dump_98cfb27": {
        "timestamp": {
            "time": "11-01-2024 11:26:41",
            "file": "index.php",
            "line": 16
        },
        "data": {
            "object (class@anonymous) #2 (1)": {
                "name": "omar"
            }
        }
    }
}

输出WEB

Anonymous class Web Output

数组

$arr = [
    'a' => 'apple',
    'b' => 'banana',
    'c' => [1, 2, [1, 2, 3]],
    'd' => 'dates',
];

d($arr);
dump_to_file(__DIR__ . '/data.txt', $arr);
dump_to_file(__DIR__ . '/data.json', $arr);

输出CLI / TXT FILE

[ Time : 11-01-2024 11:31:48 / File : index.php / Line : 12 ] 

array (4) => [
    a => "apple"
    b => "banana"
    [c] array (3) => [
        0 => 1
        1 => 2
        [2] array (3) => [
            0 => 1
            1 => 2
            2 => 3
        ]
    ]
    d => "dates"
]

输出JSON

{
    "dump_431b83a": {
        "timestamp": {
            "time": "11-01-2024 11:31:48",
            "file": "index.php",
            "line": 13
        },
        "data": {
            "array (4)": {
                "a": "apple",
                "b": "banana",
                "[c] array (3)": {
                    "0": 1,
                    "1": 2,
                    "[2] array (3)": {
                        "0": 1,
                        "1": 2,
                        "2": 3
                    }
                },
                "d": "dates"
            }
        }
    }
}

输出WEB

Arrays Web Output

资源

$file = fopen(__DIR__ . '/something.txt', 'r');

d($file);
dump_to_file(__DIR__ . '/data.txt', $file);
dump_to_file(__DIR__ . '/data.json', $file);

输出CLI / TXT FILE

[ Time : 11-01-2024 11:37:00 / File : index.php / Line : 7 ] 

Resource id #12 => {
    timed_out => false
    blocked => true
    eof => false
    wrapper_type => "plainfile"
    stream_type => "STDIO"
    mode => "r"
    unread_bytes => 0
    seekable => true
    uri => "/path/to/something.txt"
    [options] array (0) => []
}

输出JSON

{
    "dump_caf8a63": {
        "timestamp": {
            "time": "11-01-2024 11:37:00",
            "file": "index.php",
            "line": 8
        },
        "data": {
            "Resource id #12": {
                "timed_out": "false",
                "blocked": "true",
                "eof": "false",
                "wrapper_type": "plainfile",
                "stream_type": "STDIO",
                "mode": "r",
                "unread_bytes": 0,
                "seekable": "true",
                "uri": "\/path\/to\/something.txt",
                "[options] array (0)": {}
            }
        }
    }
}

输出WEB

Resources Web Output

闭包

$closure = function ($name, $age = '') {
    return $name;
};

d($closure);
dump_to_file(__DIR__ . '/data.txt', $closure);
dump_to_file(__DIR__ . '/data.json', $closure);

输出CLI / TXT FILE

[ Time : 11-01-2024 11:45:15 / File : index.php / Line : 9 ] 

object (Closure) #2 (2) => {
    name => "required"
    age => "optional"
}

输出JSON

{
    "dump_9a1d82e": {
        "timestamp": {
            "time": "11-01-2024 11:45:15",
            "file": "index.php",
            "line": 10
        },
        "data": {
            "object (Closure) #2 (2)": {
                "name": "required",
                "age": "optional"
            }
        }
    }
}

输出WEB

Closures Web Output

长文本

$text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Mauris viverra at est sed vestibulum. Ut ultricies urna et bibendum sollicitudin. Maecenas suscipit bibendum ante convallis dignissim. Nulla facilisi. Sed mollis eget purus eget finibus.Donec convallis risus sit amet dapibus vehicula. Interdum et malesuada fames ac ante ipsum primis in faucibus. Vestibulum vel aliquet ante. Suspendisse vitae neque non nulla viverra blandit at at diam. Phasellus imperdiet quis lacus sed facilisis. Mauris lacinia arcu lorem, quis commodo arcu fringilla in.";


d($text);
dump_to_file(__DIR__ . '/data.txt', $text);
dump_to_file(__DIR__ . '/data.json', $text);

输出CLI / TXT FILE

[ Time : 11-01-2024 11:47:55 / File : index.php / Line : 7 ] 

string (550) => "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Mauris viverra at est sed vestibulum. Ut ultricies urna et bibendum sollicitudin. Maecenas suscipit bibendum ante convallis dignissim. Nulla facilisi. Sed mollis eget purus eget finibus.Donec convallis risus sit amet dapibus vehicula. Interdum et malesuada fames ac ante ipsum primis in faucibus. Vestibulum vel aliquet ante. Suspendisse vitae neque non nulla viverra blandit at at diam. Phasellus imperdiet quis lacus sed facilisis. Mauris lacinia arcu lorem, quis commodo arcu fringilla in. "

输出JSON

{
    "dump_d637a31": {
        "timestamp": {
            "time": "11-01-2024 11:47:55",
            "file": "index.php",
            "line": 8
        },
        "data": {
            "string (550)": "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Mauris viverra at est sed vestibulum. Ut ultricies urna et bibendum sollicitudin. Maecenas suscipit bibendum ante convallis dignissim. Nulla facilisi. Sed mollis eget purus eget finibus.Donec convallis risus sit amet dapibus vehicula. Interdum et malesuada fames ac ante ipsum primis in faucibus. Vestibulum vel aliquet ante. Suspendisse vitae neque non nulla viverra blandit at at diam. Phasellus imperdiet quis lacus sed facilisis. Mauris lacinia arcu lorem, quis commodo arcu fringilla in."
        }
    }
}

输出WEB

Long Text Folded Web Output

并且作为对象和数组,长文本可以通过点击文本进行折叠/展开

Long Text Unfolded Web Output

许可证

(Var-Dumper) 在MIT许可证下发布。