定制 assad2008/dump-r 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

assad2008/dump-r

Composer 安装命令:

composer require assad2008/dump-r

包简介

a cleaner, leaner mix of print_r() and var_dump()

README 文档

README

a cleaner, leaner mix of print_r() and var_dump() (MIT Licensed)

screenshot

Demo: http://o-0.me/dump_r/

Installing

Composer

https://packagist.org/packages/leeoniya/dump-r

{
	"require": {
		"leeoniya/dump-r": "dev-master"
	}
}

Require

require 'dump_r.php';

Using & Config

Use dump_r() as a drop-in replacement for print_r() and var_dump(). It has some additional arguments that control output. The full signature of the function is:

function dump_r($value, $return = false, $html = true, $depth = 1e3, $expand = 1e3);
  • $value is the thing you want to dump
  • $return determines whether to return the dump rather than output to screen
  • $html controls whether the output is text or html
  • $depth sets the recursion limit for the dump
  • $expand sets the auto-expanded child node depth

There are also two modifier keys that can be used to control how the node expanding/collapsing works:

  1. Holding Shift while toggling will expand/collapse the full depth of the node.
  2. Hold Ctrl while toggling will expand/collapse all siblings after that node also. This is useful if you have an array of objects/arrays and want to expand all of them to one level simultaneously by clicking just the first one in the group. It works well for deep, complex objects.
  3. Shift and Ctrl can be used together.

Double-clicking binary strings will toggle them between mixed hex/ascii and hex-only representations:

binary_toggle

Some types of strings can be pretty-printed and additonal rendering options can be tweaked (shown with defaults):

dump_r\Rend::$xml_pretty	= false;	// pretty-print xml strings
dump_r\Rend::$json_pretty	= false;	// pretty-print json strings
dump_r\Rend::$sql_pretty	= false;	// pretty-print sql strings (requires https://github.com/jdorn/sql-formatter)
dump_r\Rend::$recset_tbls	= true;		// recordset detection & table-style output
dump_r\Rend::$trace_info	= true;		// show file & line where dump_r was invoked
dump_r\Rend::$val_space		= 4;		// number of spaces between key and value columns (affects text output only, not html)

Circular reference (recursion) detection and duplicate output is indicated like this for arrays, objects, closures and resources respectively: [*],{*},(*),<*>.

You can re-style all aspects of the html output using CSS, everything is class-tagged.

Extending

Adding your own classifiers & parsers is extremely easy. Here are instructions and two concrete examples of how the String base type can be subtyped. First for displaying EXIF data of jpeg and tiff image paths and then showing row data from CSV file paths.

This array

$stuff = [
	'imgs/img_1771.jpg',
	'data/people.csv',
];

Which would normally dump like this:

coretyped

Can be dumped like this with subtyping:

usertyped

To do this, hook the correct core type and provide a function that classifies and processes the raw value, then modifies and returns an instance of Type. Here are the properties that can be modified/augmented:

  1. $type->types - Array of subtype string(s) of your choice. These get appended as CSS classes and are also displayed inline.
  2. $type->nodes - Array of expandable subnodes to display. Provide null if no subnodes are needed or to retain any subnodes extracted by the core type.
  3. $type->length - A string to be displayed at the end of the line, indicating length of subnodes. You can also abuse this param to display other length-esque information (the EXIF example below uses it to display image dimensions inline). Provide null to retain the default length display for the hooked core type.
use dump_r\Type;

// Example 1: dump EXIF data with image filepath strings

Type::hook('_String', function($raw, Type $type, $path) {
	// match path-esque strings (containing '/' or '\') trailed by an
	// EXIF-capable image extension, then verify this file actually exists
	if (preg_match('#[\/]+.+\.(jpe?g|tiff?)$#', $raw) && is_file($raw)) {
		$nodes = $exif = exif_read_data($raw, 0, true);
		$len = $exif['COMPUTED']['Width'] . 'x' . $exif['COMPUTED']['Height'];

		$type->types	= ['image'];
		$type->nodes	= ['EXIF' => $nodes['EXIF']];
		$type->length	= $len;

		return $type;
	}
});

// Example 2: dump CSV records with csv filepath strings

Type::hook('_String', function($raw, Type $type, $path) {
	if (preg_match('#[\/]+.+\.csv$#', $raw) && is_file($raw)) {

		$type->types	= ['csv'];
		$type->nodes	= csv2array($raw);
		$type->length	= count($type->nodes);

		return $type;
	}
});

function csv2array($file) {
	$csv = [];
	$rows = array_map('str_getcsv', file($file));
	$header = array_shift($rows);
	foreach ($rows as $row)
		$csv[] = array_combine($header, $row);
	return $csv;
}

All core types (see src/dump_r/Node dir) can be hooked by their fully namespaced names. For example, if you wanted to further subtype a JSON object string, you would use

Type::hook('_String\\_JSON\\_Object', function($raw, Type $type, $path) {
	// code here
});

Filtering, Marking & Recursion Control

Using the same Type hooks (introduced above) allows you to modify additional aspects of the renderer and iterator.

Skip specific nodes based on their properties or path in the hierarchy

// prevent anything keyd under 'xxx' from dumping
Type::hook('*', function($raw, Type $type, $path) {
	if (end($path) === 'xxx')
		return false;
});

Stop recursion of specific nodes

// prevent arrays keyed under 'c' from dumping sub-nodes
Type::hook('_Array', function($raw, Type $type, $path) {
	if (end($path) === 'c')
		$type->depth = 1;

	return $type;
});

CSS-tag nodes via classes

// tag nodes keyed under `yyy` with addl CSS classes
Type::hook('*', function($raw, Type $type, $path) {
	if (end($path) === 'yyy') {
		$type->classes[] = 'marked';
	}

	return $type;
});

assad2008/dump-r 适用场景与选型建议

assad2008/dump-r 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 156 次下载、GitHub Stars 达 0, 最近一次更新时间为 2024 年 05 月 22 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「debug」 「dump」 「pretty」 「print」 「print_r」 「var_dump」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 assad2008/dump-r 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 assad2008/dump-r 我们能提供哪些服务?
定制开发 / 二次开发

基于 assad2008/dump-r 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 156
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 24
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 18
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-05-22