Skip to content

Commit

Permalink
Merge pull request #225 from Ocramius/hotfix/#224-disable-on-initiali…
Browse files Browse the repository at this point in the history
…zation-ghost-object-checks

#224 - GhostObjects can simply ignore scope checks during lazy-loading (temporarily fixes #210)
  • Loading branch information
Ocramius committed Jan 9, 2015
2 parents dc430d2 + 1fad9fe commit e1c4f81
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

use ProxyManager\Generator\MagicMethodGenerator;
use ProxyManager\Generator\ParameterGenerator;
use ProxyManager\ProxyGenerator\LazyLoadingGhost\PropertyGenerator\InitializationTracker;
use ProxyManager\ProxyGenerator\LazyLoadingGhost\PropertyGenerator\PrivatePropertiesMap;
use ProxyManager\ProxyGenerator\LazyLoadingGhost\PropertyGenerator\ProtectedPropertiesMap;
use ProxyManager\ProxyGenerator\PropertyGenerator\PublicPropertiesMap;
Expand All @@ -40,13 +41,17 @@ class MagicGet extends MagicMethodGenerator
* @var string
*/
private $callParentTemplate = <<<'PHP'
%s
$this->%s && ! $this->%s && $this->%s('__get', array('name' => $name));
if (isset(self::$%s[$name])) {
return $this->$name;
}
if (isset(self::$%s[$name])) {
if ($this->%s) {
return $this->$name;
}
// check protected property access via compatible class
$callers = debug_backtrace(\DEBUG_BACKTRACE_PROVIDE_OBJECT, 2);
$caller = isset($callers[1]) ? $callers[1] : [];
Expand Down Expand Up @@ -81,7 +86,7 @@ class MagicGet extends MagicMethodGenerator
return $accessor($this);
}
if ('ReflectionProperty' === $class) {
if ($this->%s || 'ReflectionProperty' === $class) {
$tmpClass = key(self::$%s[$name]);
$cacheKey = $tmpClass . '#' . $name;
$accessor = isset($accessorCache[$cacheKey])
Expand All @@ -104,14 +109,16 @@ class MagicGet extends MagicMethodGenerator
* @param PublicPropertiesMap $publicProperties
* @param ProtectedPropertiesMap $protectedProperties
* @param PrivatePropertiesMap $privateProperties
* @param InitializationTracker $initializationTracker
*/
public function __construct(
ReflectionClass $originalClass,
PropertyGenerator $initializerProperty,
MethodGenerator $callInitializer,
PublicPropertiesMap $publicProperties,
ProtectedPropertiesMap $protectedProperties,
PrivatePropertiesMap $privateProperties
PrivatePropertiesMap $privateProperties,
InitializationTracker $initializationTracker
) {
parent::__construct($originalClass, '__get', [new ParameterGenerator('name')]);

Expand All @@ -130,13 +137,16 @@ public function __construct(

$this->setBody(sprintf(
$this->callParentTemplate,
'$this->' . $initializerProperty->getName() . ' && $this->' . $callInitializer->getName()
. '(\'__get\', array(\'name\' => $name));',
$initializerProperty->getName(),
$initializationTracker->getName(),
$callInitializer->getName(),
$publicProperties->getName(),
$protectedProperties->getName(),
$initializationTracker->getName(),
$protectedProperties->getName(),
$privateProperties->getName(),
$privateProperties->getName(),
$initializationTracker->getName(),
$privateProperties->getName(),
$parentAccess
));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,8 @@ function (MethodGenerator $generatedMethod) use ($originalClass, $classGenerator
$init,
$publicProperties,
$protectedProperties,
$privateProperties
$privateProperties,
$initializationTracker
),
new MagicSet(
$originalClass,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

use PHPUnit_Framework_TestCase;
use ProxyManager\ProxyGenerator\LazyLoadingGhost\MethodGenerator\MagicGet;
use ProxyManager\ProxyGenerator\LazyLoadingGhost\PropertyGenerator\InitializationTracker;
use ProxyManager\ProxyGenerator\LazyLoadingGhost\PropertyGenerator\PrivatePropertiesMap;
use ProxyManager\ProxyGenerator\LazyLoadingGhost\PropertyGenerator\ProtectedPropertiesMap;
use ProxyManager\ProxyGenerator\PropertyGenerator\PublicPropertiesMap;
Expand Down Expand Up @@ -64,17 +65,26 @@ class MagicGetTest extends PHPUnit_Framework_TestCase
*/
protected $privateProperties;

/**
* @var InitializationTracker|\PHPUnit_Framework_MockObject_MockObject
*/
protected $initializationTracker;

/**
* @var string
*/
private $expectedCode = <<<'PHP'
$this->foo && $this->baz('__get', array('name' => $name));
$this->foo && ! $this->init && $this->baz('__get', array('name' => $name));
if (isset(self::$bar[$name])) {
return $this->$name;
}
if (isset(self::$baz[$name])) {
if ($this->init) {
return $this->$name;
}
// check protected property access via compatible class
$callers = debug_backtrace(\DEBUG_BACKTRACE_PROVIDE_OBJECT, 2);
$caller = isset($callers[1]) ? $callers[1] : [];
Expand Down Expand Up @@ -109,7 +119,7 @@ class MagicGetTest extends PHPUnit_Framework_TestCase
return $accessor($this);
}
if ('ReflectionProperty' === $class) {
if ($this->init || 'ReflectionProperty' === $class) {
$tmpClass = key(self::$tab[$name]);
$cacheKey = $tmpClass . '#' . $name;
$accessor = isset($accessorCache[$cacheKey])
Expand Down Expand Up @@ -144,13 +154,18 @@ protected function setUp()
->getMockBuilder(PrivatePropertiesMap::class)
->disableOriginalConstructor()
->getMock();
$this->initializationTracker = $this
->getMockBuilder(InitializationTracker::class)
->disableOriginalConstructor()
->getMock();

$this->initializer->expects($this->any())->method('getName')->will($this->returnValue('foo'));
$this->initMethod->expects($this->any())->method('getName')->will($this->returnValue('baz'));
$this->publicProperties->expects($this->any())->method('isEmpty')->will($this->returnValue(false));
$this->publicProperties->expects($this->any())->method('getName')->will($this->returnValue('bar'));
$this->protectedProperties->expects($this->any())->method('getName')->will($this->returnValue('baz'));
$this->privateProperties->expects($this->any())->method('getName')->will($this->returnValue('tab'));
$this->initializationTracker->expects($this->any())->method('getName')->will($this->returnValue('init'));
}

/**
Expand All @@ -164,7 +179,8 @@ public function testBodyStructure()
$this->initMethod,
$this->publicProperties,
$this->protectedProperties,
$this->privateProperties
$this->privateProperties,
$this->initializationTracker
);

$this->assertSame('__get', $magicGet->getName());
Expand All @@ -184,7 +200,8 @@ public function testBodyStructureWithOverriddenMagicGet()
$this->initMethod,
$this->publicProperties,
$this->protectedProperties,
$this->privateProperties
$this->privateProperties,
$this->initializationTracker
);

$this->assertSame('__get', $magicGet->getName());
Expand Down

0 comments on commit e1c4f81

Please sign in to comment.