コード例 #1
0
 /**
  * Generates the necessary methods in the class.
  *
  * @param  \ReflectionClass $originalClass
  * @param  PhpClass         $class
  * @return void
  */
 public function generate(\ReflectionClass $originalClass, PhpClass $class)
 {
     $methods = ReflectionUtils::getOverrideableMethods($originalClass, true);
     // no public, non final methods
     if (empty($methods)) {
         return;
     }
     if (null !== $this->markerInterface) {
         $class->setImplementedInterfaces(array_merge($class->getImplementedInterfaces(), array($this->markerInterface)));
     }
     $initializer = new PhpProperty();
     $initializer->setName($this->prefix . 'lazyInitializer');
     $initializer->setVisibility(PhpProperty::VISIBILITY_PRIVATE);
     $class->setProperty($initializer);
     $initialized = new PhpProperty();
     $initialized->setName($this->prefix . 'initialized');
     $initialized->setDefaultValue(false);
     $initialized->setVisibility(PhpProperty::VISIBILITY_PRIVATE);
     $class->setProperty($initialized);
     $initializerSetter = new PhpMethod();
     $initializerSetter->setName($this->prefix . 'setLazyInitializer');
     $initializerSetter->setBody('$this->' . $this->prefix . 'lazyInitializer = $initializer;');
     $parameter = new PhpParameter();
     $parameter->setName('initializer');
     $parameter->setType('\\CG\\Proxy\\LazyInitializerInterface');
     $initializerSetter->addParameter($parameter);
     $class->setMethod($initializerSetter);
     $this->addMethods($class, $methods);
     $initializingMethod = new PhpMethod();
     $initializingMethod->setName($this->prefix . 'initialize');
     $initializingMethod->setVisibility(PhpMethod::VISIBILITY_PRIVATE);
     $initializingMethod->setBody($this->writer->reset()->writeln('if (null === $this->' . $this->prefix . 'lazyInitializer) {')->indent()->writeln('throw new \\RuntimeException("' . $this->prefix . 'setLazyInitializer() must be called prior to any other public method on this object.");')->outdent()->write("}\n\n")->writeln('$this->' . $this->prefix . 'lazyInitializer->initializeObject($this);')->writeln('$this->' . $this->prefix . 'initialized = true;')->getContent());
     $class->setMethod($initializingMethod);
 }