/** * fromReflection() * * @param ReflectionProperty $reflectionProperty * @return PropertyGenerator */ public static function fromReflection(PropertyReflection $reflectionProperty) { $property = new self(); $property->setName($reflectionProperty->getName()); $allDefaultProperties = $reflectionProperty->getDeclaringClass()->getDefaultProperties(); $property->setDefaultValue($allDefaultProperties[$reflectionProperty->getName()]); if ($reflectionProperty->getDocComment() != '') { $property->setDocblock(DocblockGenerator::fromReflection($reflectionProperty->getDocComment())); } if ($reflectionProperty->isStatic()) { $property->setStatic(true); } if ($reflectionProperty->isPrivate()) { $property->setVisibility(self::VISIBILITY_PRIVATE); } elseif ($reflectionProperty->isProtected()) { $property->setVisibility(self::VISIBILITY_PROTECTED); } else { $property->setVisibility(self::VISIBILITY_PUBLIC); } $property->setSourceDirty(false); return $property; }
/** * fromReflection() * * @param \Zend\Reflection\ReflectionMethod $reflectionMethod * @return \MethodGenerator\Code\Generator\PhpMethod */ public static function fromReflection(MethodReflection $reflectionMethod) { $method = new self(); $method->setSourceContent($reflectionMethod->getContents(false)); $method->setSourceDirty(false); if ($reflectionMethod->getDocComment() != '') { $method->setDocblock(DocblockGenerator::fromReflection($reflectionMethod->getDocblock())); } $method->setFinal($reflectionMethod->isFinal()); if ($reflectionMethod->isPrivate()) { $method->setVisibility(self::VISIBILITY_PRIVATE); } elseif ($reflectionMethod->isProtected()) { $method->setVisibility(self::VISIBILITY_PROTECTED); } else { $method->setVisibility(self::VISIBILITY_PUBLIC); } $method->setStatic($reflectionMethod->isStatic()); $method->setName($reflectionMethod->getName()); foreach ($reflectionMethod->getParameters() as $reflectionParameter) { $method->setParameter(ParameterGenerator::fromReflection($reflectionParameter)); } $method->setBody($reflectionMethod->getBody()); return $method; }
/** * fromReflection() - build a Code Generation Php Object from a Class Reflection * * @param ReflectionClass $classReflection * @return ClassGenerator */ public static function fromReflection(ClassReflection $classReflection) { // class generator $cg = new static($classReflection->getName()); $cg->setSourceContent($cg->getSourceContent()); $cg->setSourceDirty(false); if ($classReflection->getDocComment() != '') { $cg->setDocblock(DocblockGenerator::fromReflection($classReflection->getDocblock())); } $cg->setAbstract($classReflection->isAbstract()); // set the namespace if ($classReflection->inNamespace()) { $cg->setNamespaceName($classReflection->getNamespaceName()); } /* @var $parentClass \Zend\Code\Reflection\ReflectionClass */ if ($parentClass = $classReflection->getParentClass()) { $cg->setExtendedClass($parentClass->getName()); $interfaces = array_diff($classReflection->getInterfaces(), $parentClass->getInterfaces()); } else { $interfaces = $classReflection->getInterfaces(); } $interfaceNames = array(); foreach ($interfaces as $interface) { /* @var $interface \Zend\Code\Reflection\ReflectionClass */ $interfaceNames[] = $interface->getName(); } $cg->setImplementedInterfaces($interfaceNames); $properties = array(); foreach ($classReflection->getProperties() as $reflectionProperty) { /* @var $reflectionProperty \PropertyReflection\Code\Reflection\ReflectionProperty */ if ($reflectionProperty->getDeclaringClass()->getName() == $cg->getName()) { $properties[] = PropertyGenerator::fromReflection($reflectionProperty); } } $cg->setProperties($properties); $methods = array(); foreach ($classReflection->getMethods() as $reflectionMethod) { /* @var $reflectionMethod \MethodReflection\Code\Reflection\ReflectionMethod */ if ($reflectionMethod->getDeclaringClass()->getName() == $cg->getName()) { $methods[] = MethodGenerator::fromReflection($reflectionMethod); } } $cg->setMethods($methods); return $cg; }
/** * fromReflection() * * @param FileReflection $fileReflection * @return FileGenerator */ public static function fromReflection(FileReflection $fileReflection) { $file = new self(); $file->setSourceContent($fileReflection->getContents()); $file->setSourceDirty(false); $body = $fileReflection->getContents(); foreach ($fileReflection->getClasses() as $class) { /* @var $class \Zend\Code\Reflection\ReflectionClass */ $phpClass = ClassGenerator::fromReflection($class); $phpClass->setContainingFileGenerator($file); $file->setClass($phpClass); $classStartLine = $class->getStartLine(true); $classEndLine = $class->getEndLine(); $bodyLines = explode("\n", $body); $bodyReturn = array(); for ($lineNum = 1; $lineNum <= count($bodyLines); $lineNum++) { if ($lineNum == $classStartLine) { $bodyReturn[] = str_replace('?', $class->getName(), '/* Zend_CodeGenerator_Php_File-ClassMarker: {?} */'); $lineNum = $classEndLine; } else { $bodyReturn[] = $bodyLines[$lineNum - 1]; // adjust for index -> line conversion } } $body = implode("\n", $bodyReturn); unset($bodyLines, $bodyReturn, $classStartLine, $classEndLine); } $namespace = $fileReflection->getNamespace(); if ($namespace != '') { $file->setNamespace($fileReflection->getNamespace()); } $uses = $fileReflection->getUses(); if ($uses) { $file->setUses($uses); } if ($fileReflection->getDocComment() != '') { /* @var $docblock \DocBlockReflection\Code\Reflection\ReflectionDocblock */ $docblock = $fileReflection->getDocblock(); $file->setDocblock(DocblockGenerator::fromReflection($docblock)); $bodyLines = explode("\n", $body); $bodyReturn = array(); for ($lineNum = 1; $lineNum <= count($bodyLines); $lineNum++) { if ($lineNum == $docblock->getStartLine()) { $bodyReturn[] = str_replace('?', $class->getName(), '/* Zend_CodeGenerator_Php_File-DocblockMarker */'); $lineNum = $docblock->getEndLine(); } else { $bodyReturn[] = $bodyLines[$lineNum - 1]; // adjust for index -> line conversion } } $body = implode("\n", $bodyReturn); unset($bodyLines, $bodyReturn, $classStartLine, $classEndLine); } $file->setBody($body); return $file; }