/**
  * 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);
 }
Пример #2
0
 /**
  * Create a setter for the aspect property with the received aspect name
  *
  * @param string $aspect
  * @return \CG\Generator\PhpMethod
  */
 protected function generateAspectSetter($aspect, AspectCodeGenerator $generator)
 {
     $loaderSetter = new PhpMethod();
     $loaderSetter->setName($generator->getSetterName($aspect))->setVisibility(PhpMethod::VISIBILITY_PUBLIC)->setBody($generator->getSetterCode($aspect));
     $loaderSetter->addParameter($this->generateAspectParameter($aspect));
     return $loaderSetter;
 }
Пример #3
0
 private function parseMethod(\SimpleXMLElement $doc)
 {
     list($className, $methodName) = explode("::", (string) $doc->refnamediv->refname);
     $class = $this->getOrCreateClass($className);
     $class->setMethod($method = new PhpMethod($methodName));
     $method->setAttribute('purpose', (string) $doc->refnamediv->refpurpose);
     foreach ($doc->refsect1 as $refsect) {
         if (isset($refsect->methodsynopsis)) {
             foreach ($doc->refsect1->methodsynopsis as $methodElem) {
                 $method->setAttribute('return_type', (string) $methodElem->type);
                 foreach ($methodElem->methodparam as $paramElem) {
                     $method->addParameter($this->createParamForElem($paramElem));
                     if ('...' === (string) $paramElem->parameter) {
                         $method->setAttribute('variable_parameters', true);
                     }
                 }
             }
             continue;
         }
         $this->parseRefsect($refsect, $method);
     }
     $this->typeRefiner->refineMethodTypes($class, $method);
 }