public function testTagGettersAndSetters() { $paramTag = new Tag\ParamTag(); $paramTag->setDatatype('string'); $returnTag = new Tag\ReturnTag(); $returnTag->setDatatype('int'); $this->docBlockGenerator->setTag(array('name' => 'blah')); $this->docBlockGenerator->setTag($paramTag); $this->docBlockGenerator->setTag($returnTag); $this->assertEquals(3, count($this->docBlockGenerator->getTags())); $target = <<<EOS /** * @blah * @param string * @return int */ EOS; $this->assertEquals($target, $this->docBlockGenerator->generate()); }
protected function getClassDocBlock(State $state) { $repository = $state->getRepositoryModel()->getName(); $doc = new DocBlockGenerator(); $doc->setTag(new Tag\GenericTag('ORM\\Entity(repositoryClass="' . $repository . '")')); $doc->setTag(new Tag\GenericTag('ORM\\Table(name="' . strtolower($this->config->getName()) . '")')); return $doc; }
/** * FileGenerator constructor. */ public function __construct() { parent::__construct(); $mockTag = new GenericTag(); $mockTag->setName('mock'); $author = new AuthorTag('ClassMocker'); $docBlock = new DocBlockGenerator(); $docBlock->setShortDescription("Auto generated file by ClassMocker, do not change"); $docBlock->setTag($author); $docBlock->setTag($mockTag); $this->setDocBlock($docBlock); }
/** * * @param DTDDocument $document * @param string $outputDirectory * @param string $namespace * @param string $parentClass */ public function generate(DTDDocument $document, $outputDirectory, $namespace, $parentClass) { if (!file_exists($outputDirectory)) { mkdir($outputDirectory, 0777, true); } $name = ucfirst($document->getFileInfo()->getBasename('.dtd')); $filename = sprintf("%s/%s.php", $outputDirectory, $name); $classGenerator = new ClassGenerator($name, $namespace, null, $parentClass); $fileGenerator = new FileGenerator(); $fileGenerator->setClass($classGenerator); $fileDocblock = new DocBlockGenerator(sprintf("%s %s", str_replace("\\", " ", $namespace), $name)); $fileDocblock->setTag(new Tag("author", "Generator")); $fileDocblock->setTag(new Tag("licence", "LGPL")); $fileGenerator->setDocBlock($fileDocblock); file_put_contents($filename, $fileGenerator->generate()); }
/** * @param string $requestMethod * @param Config $action * @param ParameterGenerator[] $params * * @return DocBlockGenerator */ private function createDocblockForMethod($requestMethod, $action, $params) { if (empty($action->doc)) { $action->doc = 'TODO'; } /* * $action->doc * METHOD: /{url:[a-z]+} */ $docblock = new DocBlockGenerator($action->doc, strtoupper($requestMethod) . ': ' . $action->url); foreach ($params as $param) { /* * @param $paramName */ $docblock->setTag(new GenericTag('param', "\${$param->getName()}")); } if (!empty($action->throws)) { /* * @throws \Name\Space\Version\Exceptions\EntityName\SomethingException */ $docblock->setTag(new GenericTag('throws', sprintf("\\%s\\%s\\Exceptions\\%s\\%s", Generator::$namespace, $this->version, $this->entityName, $action->throws->exception))); } if (!empty($action->returns)) { /* * @returns \Name\Space\Version\EntityName\EntityName(s)?Response */ $docblock->setTag(new GenericTag('returns', sprintf("\\%s\\%s\\Responses\\%s\\%s", Generator::$namespace, $this->version, $this->entityName, $action->returns))); } return $docblock; }
/** * @param State|\Scaffold\State $state * @return State|void */ public function build(State $state) { $model = $state->getModel('options-factory'); $generator = new ClassGenerator($model->getName()); $generator->setImplementedInterfaces(['FactoryInterface']); $model->setGenerator($generator); $generator->addUse('Zend\\ServiceManager\\FactoryInterface'); $generator->addUse('Zend\\ServiceManager\\ServiceLocatorInterface'); $options = $state->getModel('options'); $key = $options->getServiceName(); $key = substr($key, 0, -7); $body = <<<EOF \$config = \$serviceLocator->get('Config'); return new {$options->getClassName()}( isset(\$config['{$key}']) ? \$config['{$key}'] : [] ); EOF; $method = new MethodGenerator('createService'); $method->setParameter(new ParameterGenerator('serviceLocator', 'ServiceLocatorInterface')); $method->setBody($body); $doc = new DocBlockGenerator(''); $doc->setTag(new Tag(['name' => 'inhertidoc'])); $method->setDocBlock($doc); $generator->addMethodFromGenerator($method); }
/** * Build generators * * @param State|\Scaffold\State $state * @return \Scaffold\State|void */ public function build(State $state) { $model = $this->model; $generator = new ClassGenerator($model->getName()); $generator->addUse('Zend\\ServiceManager\\FactoryInterface'); $generator->addUse('Zend\\ServiceManager\\ServiceLocatorInterface'); $generator->addUse('Zend\\ServiceManager\\ServiceManager'); $generator->addUse($state->getServiceModel()->getName()); $generator->setImplementedInterfaces(['FactoryInterface']); $method = new MethodGenerator('createService'); $method->setParameter(new ParameterGenerator('serviceLocator', 'ServiceLocatorInterface')); $method->setBody('return new ' . $state->getServiceModel()->getClassName() . '($serviceLocator);'); $doc = new DocBlockGenerator('Create service'); $doc->setTag(new Tag\GenericTag('param', 'ServiceLocatorInterface|ServiceManager $serviceLocator')); $doc->setTag(new Tag\GenericTag('return', $state->getServiceModel()->getClassName())); $method->setDocBlock($doc); $generator->addMethodFromGenerator($method); $model->setGenerator($generator); }
/** * Build method factory * * @param ClassGenerator $generator * @param \Scaffold\State $state */ public function buildFactory(ClassGenerator $generator, State $state) { $repository = ucfirst($state->getRepositoryModel()->getClassName()); $docBlock = new DocBlockGenerator(); $docBlock->setTag(new Tag\GenericTag('return', $this->config->getName())); $factory = new MethodGenerator(); $factory->setDocBlock($docBlock); $factory->setName('factory'); $factory->setBody('return $this->get' . $repository . '()->factory();'); $generator->addMethodFromGenerator($factory); }
/** * @param type $name * @param type $type * @param type $docString * @return PhpProperty */ public function addProperty($name, $type, $docString) { $property = new PropertyGenerator(); $docblock = new DocBlockGenerator(); $property->setName($name); $property->setVisibility(PropertyGenerator::VISIBILITY_PROTECTED); $docblock->setShortDescription($docString); $docblock->setTag(new Tag(array('name' => 'var', 'description' => $type))); $property->setDocblock($docblock); $this->addPropertyFromGenerator($property); return $property; }
/** * (non-PHPdoc) * * @see \Symfony\Component\Console\Command\Command::execute() */ public function execute(InputInterface $input, OutputInterface $output) { $document = $this->getDocument($input->getArgument('document')); $directory = $input->getArgument('output'); $namespace = $input->getArgument('namespace'); $parentClass = $input->getArgument('parentClass'); if (!file_exists($directory)) { mkdir($directory, 0777, true); } $name = ucfirst($document->getFileInfo()->getBasename('.dtd')); $output->writeln("Generating Document " . $name); $filename = sprintf("%s/%s.php", $directory, $name); $classGenerator = new ClassGenerator($name, $namespace, null, $parentClass); $fileGenerator = new FileGenerator(); $fileGenerator->setClass($classGenerator); $fileDocblock = new DocBlockGenerator(sprintf("%s %s", str_replace("\\", " ", $namespace), $name)); $fileDocblock->setTag(new Tag("author", "Generator")); $fileDocblock->setTag(new Tag("licence", "LGPL")); $fileGenerator->setDocBlock($fileDocblock); file_put_contents($filename, $fileGenerator->generate()); }
/** * @param $name * @param ParameterGenerator[] $params * @param string $visibility * @param string $body * @param string|DocBlockGenerator $docblock * * @return \Zend\Code\Generator\MethodGenerator */ public static function method($name, $params = [], $visibility = 'public', $body = '//todo', $docblock = '') { if (empty($docblock)) { if (!empty($params)) { $docblock = new DocBlockGenerator(); foreach ($params as $param) { $tag = new Tag('param', $param->getType() . ' $' . $param->getName()); $docblock->setTag($tag); } } } return (new MethodGenerator($name, $params))->setBody($body)->setVisibility($visibility)->setDocBlock($docblock)->setIndentation(Generator::$indentation); }
/** * @param string $className * @return ClassGenerator */ public function handle(string $className) { if (!preg_match(static::MATCH_PATTERN, $className, $matches)) { return null; } $baseName = $matches[1]; $namespace = $this->deriveNamespaceFromClassName($className); $typeName = "{$namespace}\\{$baseName}"; $reflectedClass = new ReflectionClass($typeName); if ($reflectedClass->isTrait() === true || $reflectedClass->isAbstract() === true) { return null; } $class = $this->getClassTemplate(); $class->setNamespaceName($namespace)->setName($baseName . $class->getName()); $propertyDocBlock = new DocBlockGenerator(null, null, array(array("name" => "var", "content" => "\\{$typeName}"))); $class->getProperty("_instance")->setDocBlock($propertyDocBlock); $methodDocBlock = new DocBlockGenerator(); $returnTag = new ReturnTag("\\{$typeName}"); $methodDocBlock->setTag($returnTag); $instanceMethod = $class->getMethod("_getInstance"); $instanceMethod->setDocBlock($methodDocBlock)->setReturnType($typeName); if ($reflectedClass->isInterface() === true) { $class->setImplementedInterfaces(array("\\{$typeName}")); } else { $class->setExtendedClass("\\{$typeName}"); } $publicMethods = $reflectedClass->getMethods(ReflectionMethod::IS_PUBLIC); $addMethods = array(); foreach ($publicMethods as $method) { if ($method->isConstructor() || $method->isDestructor() || $method->isFinal() || $method->isStatic()) { continue; } if ($method->getName() === "__clone") { continue; } $addMethods[] = $this->getMethodDetails($method); } $class->addMethods($addMethods); $this->makeExtraModifications($class, $typeName); return $class; }
/** * {@inheritdoc} */ public function createClassmap() { /* * INI FILE GENERATION */ $outputPath = array($this->config->getOutputPath()); // if the psr0 autoloader has been selected, transform the class namespace into a filesystem path if ($this->config->getAutoloader() === Config::AUTOLOADER_PSR0) { $outputPath[] = str_ireplace('\\', DIRECTORY_SEPARATOR, $this->config->getNamespace()); } // append the file name $outputPath[] = 'classmap.ini'; // finalize the output path $outputPath = implode(DIRECTORY_SEPARATOR, $outputPath); // remove the file if exists $fs = new Filesystem(); $fs->remove($outputPath); foreach ($this->classmap as $wsdlType => $phpType) { file_put_contents($outputPath, "{$wsdlType} = {$phpType}" . AbstractGenerator::LINE_FEED, FILE_APPEND); } /* * CLASS GENERATION */ // create the class $class = ClassGenerator::fromReflection(new ClassReflection('\\Sapone\\Template\\ClassmapTemplate')); $class->setName('Classmap'); $class->setExtendedClass('\\ArrayObject'); $class->setNamespaceName($this->config->getNamespace()); $doc = new DocBlockGenerator(); $doc->setTag(new GenericTag('@service', $this->config->getWsdlDocumentPath())); $class->setDocBlock($doc); $this->serializeClass($class); return $class->getName(); }
/** * writes module config * * @param array $config * @param string $path */ protected function writeModuleConfig(array $config, $path) { $moduleName = $this->params->getParam('moduleName'); $generatorConfig = new FileGenerator(); $docBlock = new DocBlockGenerator(); $docBlock->setTag(new GenericTag('author', $this->params->getParam('author'))); $docBlock->setTag(new GenericTag('project', $this->params->getParam('project'))); $docBlock->setTag(new GenericTag('license', $this->params->getParam('license'))); $docBlock->setTag(new GenericTag('copyright', $this->params->getParam('copyright'))); $docBlock->setShortDescription(sprintf($this->codeLibrary()->get('module.generatedConfigDescription'), $moduleName)); $generatorConfig->setDocBlock($docBlock); $configString = var_export($config, true); $configString = str_replace("'/../view'", '__DIR__ . \'/../view\'', $configString); $generatorConfig->setBody('return ' . $configString . ';'); file_put_contents($path, $generatorConfig->generate()); }
private function handleAdder(Generator\ClassGenerator $generator, PHPProperty $prop, PHPClass $class) { $type = $prop->getType(); $propName = $type->getArg()->getName(); $docblock = new DocBlockGenerator(); $docblock->setShortDescription("Adds as {$propName}"); if ($prop->getDoc()) { $docblock->setLongDescription($prop->getDoc()); } $return = new ReturnTag(); $return->setTypes("self"); $docblock->setTag($return); $patramTag = new ParamTag($propName, $this->getPhpType($type->getArg()->getType())); $docblock->setTag($patramTag); $method = new MethodGenerator("addTo" . Inflector::classify($prop->getName())); $parameter = new ParameterGenerator($propName); $tt = $type->getArg()->getType(); if (!$this->isNativeType($tt)) { if ($p = $this->isOneType($tt)) { if ($t = $p->getType()) { $patramTag->setTypes($this->getPhpType($t)); if (!$this->isNativeType($t)) { $parameter->setType($this->getPhpType($t)); } } } elseif (!$this->isNativeType($tt)) { $parameter->setType($this->getPhpType($tt)); } } $methodBody = "\$this->" . $prop->getName() . "[] = \$" . $propName . ";" . PHP_EOL; $methodBody .= "return \$this;"; $method->setBody($methodBody); $method->setDocBlock($docblock); $method->setParameter($parameter); $generator->addMethodFromGenerator($method); }
/** * @param ClassGenerator $class * @param Attribute $tagAttribute */ protected function addAttribute(ClassGenerator $class, Attribute $tagAttribute) { $methodName = str_replace(':', '-', $tagAttribute->getName()); $methodName = ucfirst($this->stringToCamelCaseConverter->convert($methodName, '-')); $method = new MethodGenerator(); if ($tagAttribute->isFlag()) { $method->setName('is' . $methodName); $method->setParameter(new ParameterGenerator('v', 'bool', true)); } else { $method->setName('set' . $methodName); $method->setParameter(new ParameterGenerator('v', 'string')); } $body = []; $body[] = '$this->attributes[\'' . $tagAttribute->getName() . '\'] = $v;'; $body[] = 'return $this;'; $method->setBody(implode(PHP_EOL, $body)); $docBlock = new DocBlockGenerator(); $docBlock->setTag(new GenericTag('var', 'string')); $docBlock->setTag(new GenericTag('return', '$this')); if (!is_null($tagAttribute->getDescription())) { $docBlock->setLongDescription($tagAttribute->getDescription()); } $method->setDocBlock($docBlock); $class->addMethodFromGenerator($method); }
public function getDocBlockGenerator() { if (!$this->docBlockGenerator) { $generator = new CodeGenerator\DocBlockGenerator(); $apiTag = new CodeGenerator\DocBlock\Tag(); $apiTag->setName('api'); $generator->setTag($apiTag); $this->docBlockGenerator = $generator; } return $this->docBlockGenerator; }
private function getServiceCode() { $code = new ClassGenerator($this->service->getName(), $this->namespace, null, '\\SoapClient'); $doc = $this->service->getDoc(); if ($doc) { $docBlock = new DocBlockGenerator($doc); $code->setDocBlock($docBlock); } foreach ($this->service->getFunctions() as $function) { $method = new MethodGenerator($function->getMethod()); $docBlock = new DocBlockGenerator($function->getDoc()); foreach ($function->getParams() as $param) { $methodParam = new ParameterGenerator($param->getName()); if (false === $param->isPrimitive()) { $methodParam->setType('\\' . $this->getFullNamespace($param->getType())); } $method->setParameter($methodParam); $tag = new Tag(); $tag->setName('property'); $type = $param->getType(); if (false === $param->isPrimitive()) { $type = '\\' . $this->getFullNamespace($param->getType()); } $tag->setDescription(sprintf('%s $%s', $type, $param->getName())); $docBlock->setTag($tag); } $tag = new Tag(); $tag->setName('returns'); $tag->setDescription("\\" . $this->getFullNamespace($function->getReturns())); $docBlock->setTag($tag); $method->setBody(sprintf('return $this->__soapCall("%s", func_get_args());', $function->getName())); $method->setDocBlock($docBlock); $code->addMethodFromGenerator($method); } return $code; }
/** * returns filled doc block for file * * @return \Zend\Code\Generator\DocBlockGenerator */ protected function getFileDocBlock() { $docBlock = new DocBlockGenerator(); $docBlock->setTag(new GenericTag('author', $this->params->getParam('author'))); $docBlock->setTag(new GenericTag('project', $this->params->getParam('project'))); $docBlock->setTag(new GenericTag('license', $this->params->getParam('license'))); $docBlock->setTag(new GenericTag('copyright', $this->params->getParam('copyright'))); return $docBlock; }