-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
123 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |