Пример #1
0
 /**
  * {@inheritDoc}
  */
 public function generate(ReflectionClass $originalClass, ClassGenerator $classGenerator)
 {
     $interfaces = array('ProxyManager\\Proxy\\GhostObjectInterface');
     $publicProperties = new PublicPropertiesMap($originalClass);
     $publicPropsDefaults = new PublicPropertiesDefaults($originalClass);
     if ($originalClass->isInterface()) {
         $interfaces[] = $originalClass->getName();
     } else {
         $classGenerator->setExtendedClass($originalClass->getName());
     }
     $classGenerator->setImplementedInterfaces($interfaces);
     $classGenerator->addPropertyFromGenerator($initializer = new InitializerProperty());
     $classGenerator->addPropertyFromGenerator($initializationTracker = new InitializationTracker());
     $classGenerator->addPropertyFromGenerator($publicProperties);
     $classGenerator->addPropertyFromGenerator($publicPropsDefaults);
     $init = new CallInitializer($initializer, $publicPropsDefaults, $initializationTracker);
     $classGenerator->addMethodFromGenerator($init);
     foreach (ProxiedMethodsFilter::getProxiedMethods($originalClass) as $method) {
         $classGenerator->addMethodFromGenerator(LazyLoadingMethodInterceptor::generateMethod(new MethodReflection($method->getDeclaringClass()->getName(), $method->getName()), $initializer, $init));
     }
     $classGenerator->addMethodFromGenerator(new Constructor($originalClass, $initializer));
     $classGenerator->addMethodFromGenerator(new MagicGet($originalClass, $initializer, $init, $publicProperties));
     $classGenerator->addMethodFromGenerator(new MagicSet($originalClass, $initializer, $init, $publicProperties));
     $classGenerator->addMethodFromGenerator(new MagicIsset($originalClass, $initializer, $init, $publicProperties));
     $classGenerator->addMethodFromGenerator(new MagicUnset($originalClass, $initializer, $init, $publicProperties));
     $classGenerator->addMethodFromGenerator(new MagicClone($originalClass, $initializer, $init));
     $classGenerator->addMethodFromGenerator(new MagicSleep($originalClass, $initializer, $init));
     $classGenerator->addMethodFromGenerator(new SetProxyInitializer($initializer));
     $classGenerator->addMethodFromGenerator(new GetProxyInitializer($initializer));
     $classGenerator->addMethodFromGenerator(new InitializeProxy($initializer, $init));
     $classGenerator->addMethodFromGenerator(new IsProxyInitialized($initializer));
 }
 /**
  * @covers \ProxyManager\ProxyGenerator\LazyLoadingGhost\MethodGenerator\LazyLoadingMethodInterceptor
  */
 public function testBodyStructureWithoutParameters()
 {
     $reflectionMethod = new MethodReflection(__CLASS__, 'testBodyStructureWithoutParameters');
     $initializer = $this->getMock('Zend\\Code\\Generator\\PropertyGenerator');
     $initCall = $this->getMock('Zend\\Code\\Generator\\MethodGenerator');
     $initializer->expects($this->any())->method('getName')->will($this->returnValue('foo'));
     $initCall->expects($this->any())->method('getName')->will($this->returnValue('bar'));
     $method = LazyLoadingMethodInterceptor::generateMethod($reflectionMethod, $initializer, $initCall);
     $this->assertSame('testBodyStructureWithoutParameters', $method->getName());
     $this->assertCount(0, $method->getParameters());
     $this->assertSame("\$this->foo && \$this->bar('testBodyStructureWithoutParameters', array());\n\n" . "return parent::testBodyStructureWithoutParameters();", $method->getBody());
 }
 /**
  * {@inheritDoc}
  */
 public function generate(ReflectionClass $originalClass, ClassGenerator $classGenerator)
 {
     CanProxyAssertion::assertClassCanBeProxied($originalClass);
     $interfaces = array('ProxyManager\\Proxy\\GhostObjectInterface');
     $publicProperties = new PublicPropertiesMap($originalClass);
     $publicPropsDefaults = new PublicPropertiesDefaults($originalClass);
     if ($originalClass->isInterface()) {
         $interfaces[] = $originalClass->getName();
     } else {
         $classGenerator->setExtendedClass($originalClass->getName());
     }
     $classGenerator->setImplementedInterfaces($interfaces);
     $classGenerator->addPropertyFromGenerator($initializer = new InitializerProperty());
     $classGenerator->addPropertyFromGenerator($initializationTracker = new InitializationTracker());
     $classGenerator->addPropertyFromGenerator($publicProperties);
     $classGenerator->addPropertyFromGenerator($publicPropsDefaults);
     $init = new CallInitializer($initializer, $publicPropsDefaults, $initializationTracker);
     array_map(function (MethodGenerator $generatedMethod) use($originalClass, $classGenerator) {
         ClassGeneratorUtils::addMethodIfNotFinal($originalClass, $classGenerator, $generatedMethod);
     }, array_merge(array_map(function (ReflectionMethod $method) use($initializer, $init) {
         return LazyLoadingMethodInterceptor::generateMethod(new MethodReflection($method->getDeclaringClass()->getName(), $method->getName()), $initializer, $init);
     }, ProxiedMethodsFilter::getProxiedMethods($originalClass)), array($init, new Constructor($originalClass, $initializer), new MagicGet($originalClass, $initializer, $init, $publicProperties), new MagicSet($originalClass, $initializer, $init, $publicProperties), new MagicIsset($originalClass, $initializer, $init, $publicProperties), new MagicUnset($originalClass, $initializer, $init, $publicProperties), new MagicClone($originalClass, $initializer, $init, $publicProperties), new MagicSleep($originalClass, $initializer, $init, $publicProperties), new SetProxyInitializer($initializer), new GetProxyInitializer($initializer), new InitializeProxy($initializer, $init), new IsProxyInitialized($initializer))));
 }