Skip to content

Commit

Permalink
Access parent's private properties #18
Browse files Browse the repository at this point in the history
  • Loading branch information
msarca committed Jan 21, 2018
1 parent 0574e52 commit 2374cef
Showing 1 changed file with 60 additions and 38 deletions.
98 changes: 60 additions & 38 deletions src/SerializableClosure.php
Original file line number Diff line number Diff line change
Expand Up @@ -336,19 +336,25 @@ public static function wrapClosures(&$data, SplObjectStorage $storage = null)
return;
}
$instance = $data;
$reflection = new ReflectionObject($data);
$reflection = new ReflectionObject($instance);
$storage[$instance] = $data = $reflection->newInstanceWithoutConstructor();
foreach ($reflection->getProperties() as $property){
if($property->isStatic()){
continue;
}
$property->setAccessible(true);
$value = $property->getValue($instance);
if(is_array($value) || is_object($value)){
static::wrapClosures($value, $storage);

do{
if(!$reflection->isUserDefined()){
break;
}
$property->setValue($data, $value);
}
foreach ($reflection->getProperties() as $property){
if($property->isStatic()){
continue;
}
$property->setAccessible(true);
$value = $property->getValue($instance);
if(is_array($value) || is_object($value)){
static::wrapClosures($value, $storage);
}
$property->setValue($data, $value);
};
} while($reflection = $reflection->getParentClass());
}

static::exitContext();
Expand Down Expand Up @@ -394,17 +400,23 @@ public static function unwrapClosures(&$data, SplObjectStorage $storage = null)
}
$storage[$data] = true;
$reflection = new ReflectionObject($data);
foreach ($reflection->getProperties() as $property){
if($property->isStatic()){
continue;
}
$property->setAccessible(true);
$value = $property->getValue($data);
if(is_array($value) || is_object($value)){
static::unwrapClosures($value, $storage);
$property->setValue($data, $value);

do{
if(!$reflection->isUserDefined()){
break;
}
}
foreach ($reflection->getProperties() as $property){
if($property->isStatic()){
continue;
}
$property->setAccessible(true);
$value = $property->getValue($data);
if(is_array($value) || is_object($value)){
static::unwrapClosures($value, $storage);
$property->setValue($data, $value);
}
};
} while($reflection = $reflection->getParentClass());
}
}

Expand Down Expand Up @@ -455,23 +467,28 @@ protected function mapPointers(&$data)
}
$scope[$data] = true;
$reflection = new ReflectionObject($data);
foreach ($reflection->getProperties() as $property){
if($property->isStatic()){
continue;
do{
if(!$reflection->isUserDefined()){
break;
}
$property->setAccessible(true);
$item = $property->getValue($data);
if ($item instanceof SerializableClosure || ($item instanceof SelfReference && $item->hash === $this->code['self'])) {
$this->code['objects'][] = array(
'instance' => $data,
'property' => $property,
'object' => $item instanceof SelfReference ? $this : $item,
);
} elseif (is_array($item) || is_object($item)) {
$this->mapPointers($item);
$property->setValue($data, $item);
foreach ($reflection->getProperties() as $property){
if($property->isStatic()){
continue;
}
$property->setAccessible(true);
$item = $property->getValue($data);
if ($item instanceof SerializableClosure || ($item instanceof SelfReference && $item->hash === $this->code['self'])) {
$this->code['objects'][] = array(
'instance' => $data,
'property' => $property,
'object' => $item instanceof SelfReference ? $this : $item,
);
} elseif (is_array($item) || is_object($item)) {
$this->mapPointers($item);
$property->setValue($data, $item);
}
}
}
} while($reflection = $reflection->getParentClass());
}
}

Expand Down Expand Up @@ -536,7 +553,11 @@ protected function mapByReference(&$data)
$instance = $data;
$reflection = new ReflectionObject($data);
$this->scope[$instance] = $data = $reflection->newInstanceWithoutConstructor();
do {

do{
if(!$reflection->isUserDefined()){
break;
}
foreach ($reflection->getProperties() as $property){
if($property->isStatic()){
continue;
Expand All @@ -548,7 +569,8 @@ protected function mapByReference(&$data)
}
$property->setValue($data, $value);
}
} while ($reflection = $reflection->getParentClass());
} while($reflection = $reflection->getParentClass());
}
}

}

0 comments on commit 2374cef

Please sign in to comment.