| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | namespace suda\framework\debug; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use ReflectionProperty; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | class DebugObject implements \JsonSerializable | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |      * @var DebugObject | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |     protected $context; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |      * @var array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |     protected $object = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |      * @var mixed | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     protected $value; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     public function __construct($value, DebugObject $context = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |         $this->context = $context; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |         $this->value = $value; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |      * @param array $value | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |      * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     protected function parseArray(array $value) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |         foreach ($value as $key => $val) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |             $value[$key] = new DebugObject($val, $this); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |         return $value; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |      * @param DebugObject $object | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |      * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |     protected function parseObject($object) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         $objectHash = spl_object_hash($object); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |         if ($this->isObjectExported($objectHash)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |             return ['_type' => get_class($object), '_address' => $objectHash]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |         $this->setObjectIsExported($objectHash); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |         return [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |             '_type' => get_class($object), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |             '_address' => $objectHash, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |             '_properties' => $this->getObjectProp($object) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |      * @param $object | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |      * @return string|array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |     protected function getObjectProp($object) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |             $oR = new \ReflectionClass($object); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |             $props = $oR->getProperties( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |                 ReflectionProperty::IS_PUBLIC | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |                 | ReflectionProperty::IS_PROTECTED | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |                 | ReflectionProperty::IS_PRIVATE | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |             $exported = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |             foreach ($props as $value) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |                 $name = dechex($value->getModifiers()) . '$' . $value->getName(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |                 $value->setAccessible(true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |                 $exported[$name] = new DebugObject($value->getValue($object), $this); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |             return $exported; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |         } catch (\ReflectionException $e) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |             return 'Err:' . $e->getMessage(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |      * @param string $objectHash | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |      * @return bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |     public function isObjectExported(string $objectHash) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |         if ($this->context === null) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |             return in_array($objectHash, $this->object); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |         return $this->context->isObjectExported($objectHash); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |     public function setObjectIsExported(string $objectHash) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |         if ($this->context === null) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |             $this->object [] = $objectHash; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |             $this->context->setObjectIsExported($objectHash); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 106 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 107 |  |  |     public function jsonSerialize() | 
            
                                                                        
                            
            
                                    
            
            
                | 108 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 109 |  |  |         if (is_object($this->value)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 110 |  |  |             return $this->parseObject($this->value); | 
            
                                                                        
                            
            
                                    
            
            
                | 111 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 112 |  |  |         if (is_array($this->value)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 113 |  |  |             return $this->parseArray($this->value); | 
            
                                                                        
                            
            
                                    
            
            
                | 114 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 115 |  |  |         if (is_resource($this->value)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 116 |  |  |             return ['_resource' => get_resource_type($this->value)]; | 
            
                                                                        
                            
            
                                    
            
            
                | 117 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 118 |  |  |         return $this->value; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 120 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 121 |  |  |  |