public function generate() { if (!$this->isSourceDirty()) { return $this->getSourceContent(); } $output = ''; if ($this->_docblock) { $output .= $this->_docblock->generate(); } if ($this->_isAbstract) { $output .= 'abstract '; } $output .= 'class ' . $this->_name; if ($this->_extendedClass) { $output .= ' extends ' . $this->_extendedClass; } if ($this->_implementedInterfaces) { $output .= ' implements ' . implode(', ', $this->_implementedInterfaces); } $output .= PHP_EOL . '{' . PHP_EOL . PHP_EOL; if ($this->_properties) { foreach ($this->_properties as $property) { $output .= $property->generate() . PHP_EOL . PHP_EOL; } } if ($this->_methods) { foreach ($this->_methods as $method) { $output .= $method->generate() . PHP_EOL . PHP_EOL; } } $output .= PHP_EOL . '}' . PHP_EOL; return $output; }
public function generate() { if ($this->isSourceDirty() === false) { return $this->_sourceContent; } // start with the body (if there), or open tag $output = $this->_body ? $this->_body : '<?php' . PHP_EOL; // need to start with the body and then produce a php file // put file docblock in if ($this->_docblock) { $regex = preg_quote(self::$_markerDocblock, '#'); if (preg_match('#' . $regex . '#', $output, $matches)) { $output = preg_replace('#' . $regex . '#', $this->_docblock->generate(), $output, 1); } else { $output .= $this->_docblock->generate() . PHP_EOL; } } // newline $output .= PHP_EOL; // process required files // @todo marker replacement for required files if ($this->_requiredFiles) { foreach ($this->_requiredFiles as $requiredFile) { $output .= 'require_once \'' . $requiredFile . '\';' . PHP_EOL; } $output .= PHP_EOL; } // process classes if ($this->_classes) { foreach ($this->_classes as $class) { $regex = str_replace('?', $class->getName(), self::$_markerClass); $regex = preg_quote($regex, '#'); if (preg_match('#' . $regex . '#', $output, $matches)) { $output = preg_replace('#' . $regex . '#', $class->generate(), $output, 1); } else { $output .= $class->generate() . PHP_EOL; } } $output .= PHP_EOL; } return $output; }
public static function fromReflection(ZendL_Reflection_Method $reflectionMethod) { $method = new self(); $method->setSourceContent($reflectionMethod->getContents(false)); $method->setSourceDirty(false); if ($reflectionMethod->getDocComment() != '') { $method->setDocblock(ZendL_Tool_CodeGenerator_Php_Docblock::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(ZendL_Tool_CodeGenerator_Php_Parameter::fromReflection($reflectionParameter)); } $method->setBody($reflectionMethod->getBody()); return $method; }