Skip to content

Commit

Permalink
#346 Adapting currently existing code to the PublicScopeSimulator c…
Browse files Browse the repository at this point in the history
…ode generator API
  • Loading branch information
Ocramius committed Nov 22, 2016
1 parent 2654b32 commit 7dbb2ef
Show file tree
Hide file tree
Showing 16 changed files with 63 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ public function __construct(

if (! $parent) {
$callParent = PublicScopeSimulator::getPublicAccessSimulationCode(
$parent,
PublicScopeSimulator::OPERATION_GET,
'name',
null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ public function __construct(

if (! $parent) {
$callParent = PublicScopeSimulator::getPublicAccessSimulationCode(
$parent,
PublicScopeSimulator::OPERATION_ISSET,
'name',
null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ public function __construct(

if (! $parent) {
$callParent = PublicScopeSimulator::getPublicAccessSimulationCode(
$parent,
PublicScopeSimulator::OPERATION_SET,
'name',
'value',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ public function __construct(

if (! $parent) {
$callParent = PublicScopeSimulator::getPublicAccessSimulationCode(
$parent,
PublicScopeSimulator::OPERATION_UNSET,
'name',
null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ public function __construct(
$this->setDocBlock(($parent ? "{@inheritDoc}\n" : '') . '@param string $name');

$callParent = PublicScopeSimulator::getPublicAccessSimulationCode(
$parent,
PublicScopeSimulator::OPERATION_GET,
'name',
'value',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ public function __construct(
$this->setDocBlock(($parent ? "{@inheritDoc}\n" : '') . '@param string $name');

$callParent = PublicScopeSimulator::getPublicAccessSimulationCode(
$parent,
PublicScopeSimulator::OPERATION_ISSET,
'name',
'value',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ public function __construct(
$this->setDocBlock(($parent ? "{@inheritDoc}\n" : '') . '@param string $name');

$callParent = PublicScopeSimulator::getPublicAccessSimulationCode(
$parent,
PublicScopeSimulator::OPERATION_SET,
'name',
'value',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ public function __construct(
$this->setDocBlock(($parent ? "{@inheritDoc}\n" : '') . '@param string $name');

$callParent = PublicScopeSimulator::getPublicAccessSimulationCode(
$parent,
PublicScopeSimulator::OPERATION_UNSET,
'name',
'value',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ public function __construct(

if (! $override) {
$parentAccess = PublicScopeSimulator::getPublicAccessSimulationCode(
null,
PublicScopeSimulator::OPERATION_GET,
'name'
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ public function __construct(

if (! $override) {
$parentAccess = PublicScopeSimulator::getPublicAccessSimulationCode(
null,
PublicScopeSimulator::OPERATION_ISSET,
'name'
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ public function __construct(

if (! $override) {
$parentAccess = PublicScopeSimulator::getPublicAccessSimulationCode(
null,
PublicScopeSimulator::OPERATION_SET,
'name',
'value'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
namespace ProxyManager\ProxyGenerator\LazyLoadingGhost\MethodGenerator;

use ProxyManager\Generator\MagicMethodGenerator;
use ProxyManager\Generator\Util\ProxiedMethodReturnExpression;
use Zend\Code\Generator\ParameterGenerator;
use ProxyManager\ProxyGenerator\LazyLoadingGhost\PropertyGenerator\PrivatePropertiesMap;
use ProxyManager\ProxyGenerator\LazyLoadingGhost\PropertyGenerator\ProtectedPropertiesMap;
Expand All @@ -42,67 +43,67 @@ class MagicUnset extends MagicMethodGenerator
* @var string
*/
private $callParentTemplate = <<<'PHP'
%s
%init
if (isset(self::$%s[$name])) {
if (isset(self::$%publicProperties[$name])) {
unset($this->$name);
return;
%voidReturn1
}
if (isset(self::$%s[$name])) {
if (isset(self::$%protectedProperties1[$name])) {
// check protected property access via compatible class
$callers = debug_backtrace(\DEBUG_BACKTRACE_PROVIDE_OBJECT, 2);
$caller = isset($callers[1]) ? $callers[1] : [];
$object = isset($caller['object']) ? $caller['object'] : '';
$expectedType = self::$%s[$name];
$expectedType = self::$%protectedProperties2[$name];
if ($object instanceof $expectedType) {
unset($this->$name);
return;
%voidReturn2
}
$class = isset($caller['class']) ? $caller['class'] : '';
if ($class === $expectedType || is_subclass_of($class, $expectedType) || $class === 'ReflectionProperty') {
unset($this->$name);
return;
%voidReturn3
}
} elseif (isset(self::$%s[$name])) {
} elseif (isset(self::$%privateProperties1[$name])) {
// check private property access via same class
$callers = debug_backtrace(\DEBUG_BACKTRACE_PROVIDE_OBJECT, 2);
$caller = isset($callers[1]) ? $callers[1] : [];
$class = isset($caller['class']) ? $caller['class'] : '';
static $accessorCache = [];
if (isset(self::$%s[$name][$class])) {
if (isset(self::$%privateProperties2[$name][$class])) {
$cacheKey = $class . '#' . $name;
$accessor = isset($accessorCache[$cacheKey])
? $accessorCache[$cacheKey]
: $accessorCache[$cacheKey] = \Closure::bind(function ($instance) use ($name) {
unset($instance->$name);
}, null, $class);
return $accessor($this);
%accessorReturn1
}
if ('ReflectionProperty' === $class) {
$tmpClass = key(self::$%s[$name]);
$tmpClass = key(self::$%privateProperties3[$name]);
$cacheKey = $tmpClass . '#' . $name;
$accessor = isset($accessorCache[$cacheKey])
? $accessorCache[$cacheKey]
: $accessorCache[$cacheKey] = \Closure::bind(function ($instance) use ($name) {
unset($instance->$name);
}, null, $tmpClass);
return $accessor($this);
%accessorReturn2
}
}
%s
%parentAccess
PHP;

/**
Expand All @@ -126,30 +127,40 @@ public function __construct(
) {
parent::__construct($originalClass, '__unset', [new ParameterGenerator('name')]);

$override = $originalClass->hasMethod('__unset');
$existingMethod = $originalClass->hasMethod('__unset') ? $originalClass->getMethod('__unset') : null;

$this->setDocBlock(($override ? "{@inheritDoc}\n" : '') . '@param string $name');
$this->setDocBlock(($existingMethod ? "{@inheritDoc}\n" : '') . '@param string $name');

$parentAccess = 'return parent::__unset($name);';

if (! $override) {
$parentAccess = PublicScopeSimulator::getPublicAccessSimulationCode(
$parentAccess = $existingMethod
? ProxiedMethodReturnExpression::generate('parent::__unset($name)', $existingMethod)
: PublicScopeSimulator::getPublicAccessSimulationCode(
$existingMethod,
PublicScopeSimulator::OPERATION_UNSET,
'name'
);
}

$this->setBody(sprintf(
$this->callParentTemplate,
'$this->' . $initializerProperty->getName() . ' && $this->' . $callInitializer->getName()
. '(\'__unset\', array(\'name\' => $name));',
$publicProperties->getName(),
$protectedProperties->getName(),
$protectedProperties->getName(),
$privateProperties->getName(),
$privateProperties->getName(),
$privateProperties->getName(),
$parentAccess

$symbols = [
'%init' => '$this->' . $initializerProperty->getName()
. ' && $this->' . $callInitializer->getName()
. '(\'__unset\', array(\'name\' => $name));',
'%publicProperties' => $publicProperties->getName(),
'%protectedProperties1' => $protectedProperties->getName(),
'%protectedProperties2' => $protectedProperties->getName(),
'%privateProperties1' => $privateProperties->getName(),
'%privateProperties2' => $privateProperties->getName(),
'%privateProperties3' => $privateProperties->getName(),
'%parentAccess' => $parentAccess,
'%voidReturn1' => ProxiedMethodReturnExpression::generate('true', $existingMethod),
'%voidReturn2' => ProxiedMethodReturnExpression::generate('true', $existingMethod),
'%voidReturn3' => ProxiedMethodReturnExpression::generate('true', $existingMethod),
'%accessorReturn1' => ProxiedMethodReturnExpression::generate('$accessor($this)', $existingMethod),
'%accessorReturn2' => ProxiedMethodReturnExpression::generate('$accessor($this)', $existingMethod),
];

$this->setBody(str_replace(
array_keys($symbols),
array_values($symbols),
$this->callParentTemplate
));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public function __construct(
) {
parent::__construct($originalClass, '__get', [new ParameterGenerator('name')]);

$hasParent = $originalClass->hasMethod('__get');
$hasParent = $originalClass->hasMethod('__get') ? $originalClass->getMethod('__get') : null;

$this->setDocBlock(($hasParent ? "{@inheritDoc}\n" : '') . '@param string $name');

Expand All @@ -78,6 +78,7 @@ public function __construct(
$initializer,
$valueHolder,
$callParent . PublicScopeSimulator::getPublicAccessSimulationCode(
$hasParent,
PublicScopeSimulator::OPERATION_GET,
'name',
null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,9 @@ public function __construct(
$initializer = $initializerProperty->getName();
$valueHolder = $valueHolderProperty->getName();
$callParent = '';
$parent = $originalClass->hasMethod('__isset') ? $originalClass->getMethod('__isset') : null;

$this->setDocBlock(($originalClass->hasMethod('__isset') ? "{@inheritDoc}\n" : '') . '@param string $name');
$this->setDocBlock(($parent ? "{@inheritDoc}\n" : '') . '@param string $name');

if (! $publicProperties->isEmpty()) {
$callParent = 'if (isset(self::$' . $publicProperties->getName() . "[\$name])) {\n"
Expand All @@ -67,6 +68,7 @@ public function __construct(
}

$callParent .= PublicScopeSimulator::getPublicAccessSimulationCode(
$parent,
PublicScopeSimulator::OPERATION_ISSET,
'name',
null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public function __construct(
[new ParameterGenerator('name'), new ParameterGenerator('value')]
);

$hasParent = $originalClass->hasMethod('__set');
$hasParent = $originalClass->hasMethod('__set') ? $originalClass->getMethod('__set') : null;
$initializer = $initializerProperty->getName();
$valueHolder = $valueHolderProperty->getName();
$callParent = '';
Expand All @@ -74,6 +74,7 @@ public function __construct(
$callParent .= $hasParent
? 'return $this->' . $valueHolder . '->__set($name, $value);'
: PublicScopeSimulator::getPublicAccessSimulationCode(
$hasParent,
PublicScopeSimulator::OPERATION_SET,
'name',
'value',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public function __construct(
) {
parent::__construct($originalClass, '__unset', [new ParameterGenerator('name')]);

$hasParent = $originalClass->hasMethod('__unset');
$hasParent = $originalClass->hasMethod('__unset') ? $originalClass->getMethod('__unset') : null;
$initializer = $initializerProperty->getName();
$valueHolder = $valueHolderProperty->getName();
$callParent = '';
Expand All @@ -70,6 +70,7 @@ public function __construct(
$callParent .= $hasParent
? 'return $this->' . $valueHolder . '->__unset($name);'
: PublicScopeSimulator::getPublicAccessSimulationCode(
$hasParent,
PublicScopeSimulator::OPERATION_UNSET,
'name',
null,
Expand Down

0 comments on commit 7dbb2ef

Please sign in to comment.