Example #1
0
 /**
  * Copied from ClassGenerator::fromReflection and tweaked slightly
  * @param ClassReflection $classReflection
  *
  * @return ClassGenerator
  */
 public function getGeneratorFromReflection(ClassReflection $classReflection)
 {
     // class generator
     $cg = new ClassGenerator($classReflection->getName());
     $cg->setSourceContent($cg->getSourceContent());
     $cg->setSourceDirty(false);
     if ($classReflection->getDocComment() != '') {
         $docblock = DocBlockGenerator::fromReflection($classReflection->getDocBlock());
         $docblock->setIndentation(Generator::$indentation);
         $cg->setDocBlock($docblock);
     }
     $cg->setAbstract($classReflection->isAbstract());
     // set the namespace
     if ($classReflection->inNamespace()) {
         $cg->setNamespaceName($classReflection->getNamespaceName());
     }
     /* @var \Zend\Code\Reflection\ClassReflection $parentClass */
     $parentClass = $classReflection->getParentClass();
     if ($parentClass) {
         $cg->setExtendedClass('\\' . ltrim($parentClass->getName(), '\\'));
         $interfaces = array_diff($classReflection->getInterfaces(), $parentClass->getInterfaces());
     } else {
         $interfaces = $classReflection->getInterfaces();
     }
     $interfaceNames = array();
     foreach ($interfaces as $interface) {
         /* @var \Zend\Code\Reflection\ClassReflection $interface */
         $interfaceNames[] = $interface->getName();
     }
     $cg->setImplementedInterfaces($interfaceNames);
     $properties = array();
     foreach ($classReflection->getProperties() as $reflectionProperty) {
         if ($reflectionProperty->getDeclaringClass()->getName() == $classReflection->getName()) {
             $property = PropertyGenerator::fromReflection($reflectionProperty);
             $property->setIndentation(Generator::$indentation);
             $properties[] = $property;
         }
     }
     $cg->addProperties($properties);
     $methods = array();
     foreach ($classReflection->getMethods() as $reflectionMethod) {
         $className = $cg->getNamespaceName() ? $cg->getNamespaceName() . "\\" . $cg->getName() : $cg->getName();
         if ($reflectionMethod->getDeclaringClass()->getName() == $className) {
             $method = MethodGenerator::fromReflection($reflectionMethod);
             $method->setBody(preg_replace("/^\\s+/m", '', $method->getBody()));
             $method->setIndentation(Generator::$indentation);
             $methods[] = $method;
         }
     }
     $cg->addMethods($methods);
     return $cg;
 }