/** * {@inheritDoc} */ public function generate(ReflectionClass $originalClass, ClassGenerator $classGenerator) { $interfaces = array('ProxyManager\\Proxy\\VirtualProxyInterface'); $publicProperties = new PublicPropertiesMap($originalClass); if ($originalClass->isInterface()) { $interfaces[] = $originalClass->getName(); } else { $classGenerator->setExtendedClass($originalClass->getName()); } $classGenerator->setImplementedInterfaces($interfaces); $classGenerator->addPropertyFromGenerator($valueHolder = new ValueHolderProperty()); $classGenerator->addPropertyFromGenerator($initializer = new InitializerProperty()); $classGenerator->addPropertyFromGenerator($publicProperties); foreach (ProxiedMethodsFilter::getProxiedMethods($originalClass) as $method) { $classGenerator->addMethodFromGenerator(LazyLoadingMethodInterceptor::generateMethod(new MethodReflection($method->getDeclaringClass()->getName(), $method->getName()), $initializer, $valueHolder)); } $classGenerator->addMethodFromGenerator(new Constructor($originalClass, $initializer)); $classGenerator->addMethodFromGenerator(new MagicGet($originalClass, $initializer, $valueHolder, $publicProperties)); $classGenerator->addMethodFromGenerator(new MagicSet($originalClass, $initializer, $valueHolder, $publicProperties)); $classGenerator->addMethodFromGenerator(new MagicIsset($originalClass, $initializer, $valueHolder, $publicProperties)); $classGenerator->addMethodFromGenerator(new MagicUnset($originalClass, $initializer, $valueHolder, $publicProperties)); $classGenerator->addMethodFromGenerator(new MagicClone($originalClass, $initializer, $valueHolder)); $classGenerator->addMethodFromGenerator(new MagicSleep($originalClass, $initializer, $valueHolder)); $classGenerator->addMethodFromGenerator(new MagicWakeup($originalClass)); $classGenerator->addMethodFromGenerator(new SetProxyInitializer($initializer)); $classGenerator->addMethodFromGenerator(new GetProxyInitializer($initializer)); $classGenerator->addMethodFromGenerator(new InitializeProxy($initializer, $valueHolder)); $classGenerator->addMethodFromGenerator(new IsProxyInitialized($valueHolder)); $classGenerator->addMethodFromGenerator(new GetWrappedValueHolderValue($valueHolder)); }
/** * @covers \ProxyManager\ProxyGenerator\LazyLoadingValueHolder\MethodGenerator\LazyLoadingMethodInterceptor */ public function testBodyStructureWithoutParameters() { $reflectionMethod = new MethodReflection(__CLASS__, 'testBodyStructureWithoutParameters'); $initializer = $this->getMock('Zend\\Code\\Generator\\PropertyGenerator'); $valueHolder = $this->getMock('Zend\\Code\\Generator\\PropertyGenerator'); $initializer->expects($this->any())->method('getName')->will($this->returnValue('foo')); $valueHolder->expects($this->any())->method('getName')->will($this->returnValue('bar')); $initializer->expects($this->any())->method('getName')->will($this->returnValue('foo')); $method = LazyLoadingMethodInterceptor::generateMethod($reflectionMethod, $initializer, $valueHolder); $this->assertSame('testBodyStructureWithoutParameters', $method->getName()); $this->assertCount(0, $method->getParameters()); $this->assertSame("\$this->foo && \$this->foo->__invoke(\$this->bar, \$this, " . "'testBodyStructureWithoutParameters', array(), \$this->foo);\n\n" . "return \$this->bar->testBodyStructureWithoutParameters();", $method->getBody()); }
/** * {@inheritDoc} */ public function generate(ReflectionClass $originalClass, ClassGenerator $classGenerator) { CanProxyAssertion::assertClassCanBeProxied($originalClass); $interfaces = [VirtualProxyInterface::class]; $publicProperties = new PublicPropertiesMap(Properties::fromReflectionClass($originalClass)); if ($originalClass->isInterface()) { $interfaces[] = $originalClass->getName(); } else { $classGenerator->setExtendedClass($originalClass->getName()); } $classGenerator->setImplementedInterfaces($interfaces); $classGenerator->addPropertyFromGenerator($valueHolder = new ValueHolderProperty()); $classGenerator->addPropertyFromGenerator($initializer = new InitializerProperty()); $classGenerator->addPropertyFromGenerator($publicProperties); array_map(function (MethodGenerator $generatedMethod) use($originalClass, $classGenerator) { ClassGeneratorUtils::addMethodIfNotFinal($originalClass, $classGenerator, $generatedMethod); }, array_merge(array_map(function (ReflectionMethod $method) use($initializer, $valueHolder) { return LazyLoadingMethodInterceptor::generateMethod(new MethodReflection($method->getDeclaringClass()->getName(), $method->getName()), $initializer, $valueHolder); }, ProxiedMethodsFilter::getProxiedMethods($originalClass)), [new StaticProxyConstructor($initializer, Properties::fromReflectionClass($originalClass)), Constructor::generateMethod($originalClass, $valueHolder), new MagicGet($originalClass, $initializer, $valueHolder, $publicProperties), new MagicSet($originalClass, $initializer, $valueHolder, $publicProperties), new MagicIsset($originalClass, $initializer, $valueHolder, $publicProperties), new MagicUnset($originalClass, $initializer, $valueHolder, $publicProperties), new MagicClone($originalClass, $initializer, $valueHolder), new MagicSleep($originalClass, $initializer, $valueHolder), new MagicWakeup($originalClass), new SetProxyInitializer($initializer), new GetProxyInitializer($initializer), new InitializeProxy($initializer, $valueHolder), new IsProxyInitialized($valueHolder), new GetWrappedValueHolderValue($valueHolder)])); }
private function buildLazyLoadingMethodInterceptor(InitializerProperty $initializer, ValueHolderProperty $valueHolder) : callable { return function (ReflectionMethod $method) use($initializer, $valueHolder) : LazyLoadingMethodInterceptor { return LazyLoadingMethodInterceptor::generateMethod(new MethodReflection($method->getDeclaringClass()->getName(), $method->getName()), $initializer, $valueHolder); }; }