Skip to content

Commit

Permalink
添加:调试输出对象
Browse files Browse the repository at this point in the history
  • Loading branch information
dxkite committed Jul 1, 2019
1 parent a4d92bf commit 2d2b569
Show file tree
Hide file tree
Showing 2 changed files with 123 additions and 3 deletions.
6 changes: 3 additions & 3 deletions suda/src/framework/Debugger.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,9 @@ public function uncaughtFatalError()
if ($e = error_get_last()) {
$isFatalError = E_ERROR | E_PARSE | E_CORE_ERROR | E_CORE_WARNING | E_COMPILE_ERROR | E_COMPILE_WARNING;
if ($e['type'] === ($e['type'] & $isFatalError)) {
$errorHander = set_error_handler(null);
if ($errorHander !== null) {
$errorHander($e['type'], $e['message'], $e['file'], $e['line']);
$handler = set_error_handler(null);
if ($handler !== null) {
$handler($e['type'], $e['message'], $e['file'], $e['line']);
}
restore_error_handler();
}
Expand Down
120 changes: 120 additions & 0 deletions suda/src/framework/debug/DebugObject.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
<?php


namespace suda\framework\debug;

use ReflectionProperty;

class DebugObject implements \JsonSerializable
{
/**
* @var DebugObject
*/
protected $context;

/**
* @var array
*/
protected $object = [];

/**
* @var mixed
*/
protected $value;

public function __construct($value, DebugObject $context = null)
{
$this->context = $context;
$this->value = $value;
}

/**
* @param array $value
* @return array
*/
protected function parseArray(array $value)
{
foreach ($value as $key => $val) {
$value[$key] = new DebugObject($val, $this);
}
return $value;
}

/**
* @param DebugObject $object
* @return array
*/
protected function parseObject($object)
{
$objectHash = spl_object_hash($object);
if ($this->isObjectExported($objectHash)) {
return ['_type' => get_class($object), '_address' => $objectHash];
}
$this->setObjectIsExported($objectHash);
return [
'_type' => get_class($object),
'_address' => $objectHash,
'_properties' => $this->getObjectProp($object)
];
}

/**
* @param $object
* @return string|array
*/
protected function getObjectProp($object)
{
try {
$oR = new \ReflectionClass($object);
$props = $oR->getProperties(
ReflectionProperty::IS_PUBLIC
| ReflectionProperty::IS_PROTECTED
| ReflectionProperty::IS_PRIVATE
);
$exported = [];
foreach ($props as $value) {
$name = dechex($value->getModifiers()) . '$' . $value->getName();
$value->setAccessible(true);
$exported[$name] = new DebugObject($value->getValue($object), $this);
}
return $exported;
} catch (\ReflectionException $e) {
return 'Err:' . $e->getMessage();
}
}

/**
* @param string $objectHash
* @return bool
*/
public function isObjectExported(string $objectHash)
{
if ($this->context === null) {
return in_array($objectHash, $this->object);
}
return $this->context->isObjectExported($objectHash);
}

public function setObjectIsExported(string $objectHash)
{
if ($this->context === null) {
$this->object [] = $objectHash;
} else {
$this->context->setObjectIsExported($objectHash);
}
}

public function jsonSerialize()
{
if (is_object($this->value)) {
return $this->parseObject($this->value);
}
if (is_array($this->value)) {
return $this->parseArray($this->value);
}
if (is_resource($this->value)) {
return ['_resource' => get_resource_type($this->value)];
}
return $this->value;
}
}

0 comments on commit 2d2b569

Please sign in to comment.