kafoso/tools 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

kafoso/tools

Composer 安装命令:

composer require kafoso/tools

包简介

Various PHP tools.

README 文档

README

Various generic PHP tools.

Will be expanded as necessary.

Kafoso\Tools\Debug\Dumper

An improved yet different version of PHP's fundamental var_dump method. The presentation is more in par with that of the appearance of PHP classes adhering to PSR-1 and PSR-2. However, for readability reasons, and to save precious space and thus prevent vertigo1, an indentation of 2 space characters is used, rather than 4.

Handles recursion gracefully and limits the depth (default: 3) of objects and arrays, omitting content going beyond the maximum desired depth.

Especially useful for dumping very large objects and arrays containing redundant or recursive information. E.g. database contents, which have been mapped to PHP classes with (sometimes) complex relations. Said mapping may be performed through PDO, mysqli, Doctrine, and other DBAL or ORM tools.

Example output

A simple class, with an id, a parent/child relationship, and a static variable $list.

  • Fully quantified class names and SPL hashes
    Each class gets printed with its unique SPL hash identifier. Useful to find recursion, which incidentally is denoted by *RECURSION*.
  • *RECURSION*
    Notice that the Object instance 000000005e85475000007fd972cdb06b under the parent is not reprinted, as it is the same as the very first class.
  • (Array value omitted)
    Notice how the $list variable is visually different between the two classes? While they are indeed identical, the dumper has reached its maximum depth level, and thus the a portion of the parent's $list variable is omitted. An (Object value omitted) equivalent exists for Objects.

Try it: php examples/DebugDumperExample.php

As plain text

The following is produced using Kafoso\Tools\Debug\Dumper::dump($myLittleClass), which presents the contents as plain text (using the Kafoso\Tools\Debug\Dumper\PlainTextFormatter).

My\Little_Class Object #
{
  private $id = int(42);
  protected $parent = My\Little_Class Object #
  {
    private $id = int(41);
    protected $parent = NULL;
    protected $children = array(1) {
      [0] => My\Little_Class Object #
      {
        *RECURSION*
      },
    }
    public static $list = array(3) {
      [0] => string(3) "foo",
      [1] => string(3) "bar",
      [2] => array(1) {
        (Array value omitted)
      }
    }
  }
  protected $children = array(0) {
  }
  public static $list = array(3) {
    [0] => string(3) "foo",
    [1] => string(3) "bar",
    [2] => array(1) {
      [0] => string(3) "baz"
    }
  }
}

As JSON

The same input as the previous plain text example, but this time using Kafoso\Tools\Debug\Dumper::dumpJson($myLittleClass).

JSON_PRETTY_PRINT is on by default, as this is intended for debugging purposes, but may be disabled if desired. E.g. when retrieving JSON through XHR from Javascript where indentations do not matter.

The JSON view contains less information compared to plain text. For instance, there is no class variable exposure information (static, public, protected, private); however, the class variable names and values do appear. Additionally, the following object keys are invasive and change the structure of the resulting Javascript objects.

  • Kafoso\\Tools\\Debug\\Dumper|CLASS
  • Kafoso\\Tools\\Debug\\Dumper|RECURSION
  • Kafoso\\Tools\\Debug\\Dumper|ARRAY_VALUE_OMITTED
  • Kafoso\\Tools\\Debug\\Dumper|OBJECT_VALUE_OMITTED

However, the Dumper is for debugging purposes, after all, and should be used as such.

{
    "Kafoso\\Tools\\Debug\\Dumper|CLASS": "My\\Little_Class Object #",
    "id": 42,
    "parent": {
        "Kafoso\\Tools\\Debug\\Dumper|CLASS": "My\\Little_Class Object #",
        "id": 41,
        "parent": null,
        "children": [
            {
                "Kafoso\\Tools\\Debug\\Dumper|CLASS": "My\\Little_Class Object #",
                "Kafoso\\Tools\\Debug\\Dumper|RECURSION": "*RECURSION*"
            }
        ],
        "list": [
            "foo",
            "bar",
            {
                "Kafoso\\Tools\\Debug\\Dumper|ARRAY_VALUE_OMITTED": "(Array value omitted; array(1))"
            }
        ]
    },
    "children": [],
    "list": [
        "foo",
        "bar",
        [
            "baz"
        ]
    ]
}

As HTML

Usage:

<?php
Kafoso\Tools\Debug\Dumper::dumpHtml($myLittleClass)

The HTML dumper is great for showing variables in a nice, highlighted fashion.

Classes are displayed as psudo-objects with interfaces to inheritance, traits, constants, variables, and methods; "pseudo-object" means the values are displayed with a snapshot of what the object currently contains.

Various options exist, which are stored in a cookie.

Options for showing and hiding sections include:

  • Extended parent classes.
  • Interfaces.
  • Traits.
  • Constants.
  • Class variables.
  • Methods.
  • Method arguments.
  • Method argument type hints.

One option, "Truncate generic objects", allows you to display objects like \DateTime as a string, providing a better overview and saving valuable space.

Do notice, that a maximum depth restriction is enforced, which by default is 3. This number can be changed by passing it as the 2nd variable, e.g.:

<?php
Kafoso\Tools\Debug\Dumper::dumpHtml($myLittleClass, 10)

This behavior is identical to the plain text and JSON rendering methods.

Sample output:

HtmlFormatter-dump-example.jpg

The theme, "dark-one-ui", is courtesy of the Atom Editor.

Footnotes

1 Vertigo: Massive nested indentations in code, usually produced by nested conditions and loops, resulting in lowered readability.

kafoso/tools 适用场景与选型建议

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

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

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

围绕 kafoso/tools 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: BSD-3-Clause
  • 更新时间: 2016-05-26