kayalion/vardump

用于调试打印变量的辅助库

4.1.1 2016-11-16 12:18 UTC

This package is not auto-updated.

Last update: 2024-09-29 00:51:58 UTC


README

PHP 辅助库,用于调试目的打印变量。

用法

<?php

// dumps a variable
vd("any variable");

// or multiple variables, as many as your memory can handle
vd("any variable", "any other variable");

// dumps a variable and dies
vdd("any variable");

// or with multiple variables
vdd("any variable", "any other variable");

// dumps one variable with a specific configuration
// set any configuration parameter to null to use the global value
$maxRecursiveDepth = 10;
$maxStringLength = 100;
$includeMethods = true;
$theme = new SpidermanHtmlVarDumpTheme();

vdc("any variable", $maxRecursiveDepth, $maxStringLength, $includeMethods, $theme);

// dumps one variable with a specific configuration and dies
vdcd("any variable", $maxRecursiveDepth, $maxStringLength, $includeMethods, $theme);

// as called for the sample screenshots
vd([
    null,
    true,
    42,
    3.1415,
    "any string",
    new Exception(),
    fopen('php://stdout', 'w'),
]);

截图

HTML 页面上的输出

Screenshot HTML

命令行界面(CLI)中的输出

Screenshot CLI

配置

您可以使用环境变量来配置 vardump。

<?php

// Flag to see if object methods should be included
$_ENV['VAR_DUMP_METHODS'] = true;

// Maximum depth for arrays and objects
$_ENV['VAR_DUMP_RECURSIVE_DEPTH'] = 10;

// Maximum length for the preview of a string
$_ENV['VAR_DUMP_STRING_LENGTH'] = 100;

// Name of the CLI theme class
$_ENV['VAR_DUMP_THEME_CLI'] = 'CliVarDumpTheme';

// Name of the HTML theme class, choose between:
// - HtmlVarDumpTheme
// - BatmanHtmlVarDumpTheme
// - HulkHtmlVarDumpTheme,
// - IronmanHtmlVarDumpTheme,
// - SpidermanHtmlVarDumpTheme,
// - SupermanHtmlVarDumpTheme,
$_ENV['VAR_DUMP_THEME_HTML'] = 'SpidermanHtmlVarDumpTheme';

将输出写入文件

当您调试一个Web应用程序时,输出可能会破坏您的布局或响应,尤其是在开发RESTful API时。

您可以使用文件主题轻松地将 vardump 输出重定向到文件。

<?php

// the file to dump to
$file = __DIR__ . '/vardump.log';

// create a theme to log to the file
$theme = new FileVarDumpTheme($file);

// you can also provide a truncate size in KB, defaults to 1MB
$theme = new FileVarDumpTheme($file, 4096); // 4MB

// set the theme for both environments
$_ENV['VAR_DUMP_THEME_CLI'] = $theme;
$_ENV['VAR_DUMP_THEME_HTML'] = $theme;

一旦创建了日志文件,您可以使用 tail 命令来监视它

tail -f vardump.log

安装

您可以使用 Composer 将此辅助库安装到您的项目中。

composer require kayalion/vardump

对于手动安装,将 src/VarDump.php 文件复制到您的项目中,并像这样包含它

<?php 

include __DIR__ . '/src/VarDump.php';