作者:i-am-ains-l
项目:sd
/**
* @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;
}
作者:enlitepr
项目:zf2-scaffol
/**
* @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);
}
作者:enlitepr
项目:zf2-scaffol
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;
}
作者:gareth
项目:php-ew
public function generate(Generator\ClassGenerator $class, PHPClass $type)
{
$class = $this->fixInterfaces($class);
if (!($extends = $type->getExtends()) && class_exists($type->getNamespace())) {
$extendNamespace = $type->getNamespace();
$extendNamespace = explode('\\', $extendNamespace);
$extendClass = array_pop($extendNamespace);
$extendNamespace = implode('\\', $extendNamespace);
$extends = new PHPClass();
$extends->setName($extendClass);
$extends->setNamespace($extendNamespace);
$class->setExtendedClass($extends);
}
if ($type->getNamespace() == Enumeration::class) {
$extendNamespace = $type->getNamespace();
$extendNamespace = explode('\\', $extendNamespace);
$extendClass = array_pop($extendNamespace);
$extendNamespace = implode('\\', $extendNamespace);
$extends = new PHPClass();
$extends->setName($extendClass);
$extends->setNamespace($extendNamespace);
$class->setExtendedClass($extends);
}
if ($extends->getName() == "string" && $extends->getNamespace() == "" && class_exists($type->getNamespace() . '\\String')) {
$extends->setName('String');
$extends->setNamespace($type->getNamespace());
} elseif ($extends->getName() == "string" && $extends->getNamespace() == "" && class_exists($type->getNamespace())) {
$extendNamespace = $type->getNamespace();
$extendNamespace = explode('\\', $extendNamespace);
$extendClass = array_pop($extendNamespace);
$extendNamespace = implode('\\', $extendNamespace);
$extends = new PHPClass();
$extends->setName($extendClass);
$extends->setNamespace($extendNamespace);
$class->setExtendedClass($extends);
}
$docblock = new DocBlockGenerator("Class representing " . $type->getName());
if ($type->getDoc()) {
$docblock->setLongDescription($type->getDoc());
}
$class->setNamespaceName($type->getNamespace());
$class->setName($type->getName());
$class->setDocblock($docblock);
$class->setExtendedClass($extends->getName());
if ($extends->getNamespace() != $type->getNamespace()) {
if ($extends->getName() == $type->getName()) {
$class->addUse($type->getExtends()->getFullName(), $extends->getName() . "Base");
$class->setExtendedClass($extends->getName() . "Base");
} else {
$class->addUse($extends->getFullName());
}
}
if ($this->handleBody($class, $type)) {
return true;
}
}
作者:enlitepr
项目:zf2-scaffol
/**
* 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);
}
作者:jsiefe
项目:class-mocke
/**
* 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);
}
作者:nf
项目:pugooro
/**
* @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;
}
作者:shinichi8
项目:sd
/**
* @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);
}
作者:slavomirkuzm
项目:itc-bundl
/**
*
* @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());
}
作者:necrogam
项目:zf
/**
* fromReflection()
*
* @param MethodReflection $reflectionMethod
* @return MethodGenerator
*/
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;
}
作者:Flesh19
项目:magent
/**
* Generate from array
*
* @configkey name string [required] Class Name
* @configkey filegenerator FileGenerator File generator that holds this class
* @configkey namespacename string The namespace for this class
* @configkey docblock string The docblock information
* @configkey properties
* @configkey methods
*
* @throws Exception\InvalidArgumentException
* @param array $array
* @return TraitGenerator
*/
public static function fromArray(array $array)
{
if (!isset($array['name'])) {
throw new Exception\InvalidArgumentException('Class generator requires that a name is provided for this object');
}
$cg = new static($array['name']);
foreach ($array as $name => $value) {
// normalize key
switch (strtolower(str_replace(array('.', '-', '_'), '', $name))) {
case 'containingfile':
$cg->setContainingFileGenerator($value);
break;
case 'namespacename':
$cg->setNamespaceName($value);
break;
case 'docblock':
$docBlock = $value instanceof DocBlockGenerator ? $value : DocBlockGenerator::fromArray($value);
$cg->setDocBlock($docBlock);
break;
case 'properties':
$cg->addProperties($value);
break;
case 'methods':
$cg->addMethods($value);
break;
}
}
return $cg;
}
作者:necrogam
项目:zf
/**
* fromReflection()
*
* @param PropertyReflection $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;
}
作者:fabiopaiv
项目:PDOSimpleMigratio
private function generate($version)
{
$generator = new ClassGenerator();
$docblock = DocBlockGenerator::fromArray(array('shortDescription' => 'PDO Simple Migration Class', 'longDescription' => 'Add your queries below'));
$generator->setName('PDOSimpleMigration\\Migrations\\Migration' . $version)->setExtendedClass('AbstractMigration')->addUse('PDOSimpleMigration\\Library\\AbstractMigration')->setDocblock($docblock)->addProperties(array(array('description', 'Migration description', PropertyGenerator::FLAG_STATIC)))->addMethods(array(MethodGenerator::fromArray(array('name' => 'up', 'parameters' => array(), 'body' => '//$this->addSql(/*Sql instruction*/);', 'docblock' => DocBlockGenerator::fromArray(array('shortDescription' => 'Migrate up', 'longDescription' => null)))), MethodGenerator::fromArray(array('name' => 'down', 'parameters' => array(), 'body' => '//$this->addSql(/*Sql instruction*/);', 'docblock' => DocBlockGenerator::fromArray(array('shortDescription' => 'Migrate down', 'longDescription' => null))))));
$file = FileGenerator::fromArray(array('classes' => array($generator)));
return $file->generate();
}
作者:rajanlami
项目:IntTes
/**
* @group #3753
*/
public function testGenerateWithWordWrapDisabled()
{
$largeStr = '@var This is a very large string that will not be wrapped if it contains more than 80 characters';
$this->docBlockGenerator->setLongDescription($largeStr);
$this->docBlockGenerator->setWordWrap(false);
$expected = '/**' . DocBlockGenerator::LINE_FEED . ' * @var This is a very large string that will not be wrapped if it contains more than' . ' 80 characters' . DocBlockGenerator::LINE_FEED . ' */' . DocBlockGenerator::LINE_FEED;
$this->assertEquals($expected, $this->docBlockGenerator->generate());
}
作者:jaguevara197
项目:ZF2_REST_OAuth2_Postgres_Backen
private function addCustomFiltersMethod()
{
$body = 'return $inputFilter;';
$inputFilterParm = new ParameterGenerator('inputFilter', null, null, null, true);
$docBlock = DocBlockGenerator::fromArray(array('shortDescription' => 'You may customize the filters behaviours using this method'));
$result = new MethodGenerator('addCustomFilters', array($inputFilterParm, 'factory'), MethodGenerator::FLAG_PUBLIC, $body, $docBlock);
return $result;
}
作者:phpr
项目:soap-clien
/**
* @param ContextInterface $context
* @param ClassGenerator $class
* @param Property $property
*
* @throws \Zend\Code\Generator\Exception\InvalidArgumentException
*/
private function implementGetResult(ContextInterface $context, ClassGenerator $class, Property $property)
{
$useAssembler = new UseAssembler($this->wrapperClass ?: ResultInterface::class);
if ($useAssembler->canAssemble($context)) {
$useAssembler->assemble($context);
}
$methodName = 'getResult';
$class->removeMethod($methodName);
$class->addMethodFromGenerator(MethodGenerator::fromArray(['name' => $methodName, 'parameters' => [], 'visibility' => MethodGenerator::VISIBILITY_PUBLIC, 'body' => $this->generateGetResultBody($property), 'docblock' => DocBlockGenerator::fromArray(['tags' => [['name' => 'return', 'description' => $this->generateGetResultReturnTag($property)]]])]));
}
作者:enlitepr
项目:zf2-scaffol
/**
* 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);
}
作者:djmattyg00
项目:autocodeloade
/**
* @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;
}
作者:slavomirkuzm
项目:itc-bundl
/**
* (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());
}
作者:phpr
项目:soap-clien
/**
* @param ContextInterface|PropertyContext $context
*
* @throws AssemblerException
*/
public function assemble(ContextInterface $context)
{
$class = $context->getClass();
$property = $context->getProperty();
try {
$methodName = Normalizer::generatePropertyMethod('get', $property->getName());
$class->removeMethod($methodName);
$class->addMethodFromGenerator(MethodGenerator::fromArray(['name' => $methodName, 'parameters' => [], 'visibility' => MethodGenerator::VISIBILITY_PUBLIC, 'body' => sprintf('return $this->%s;', $property->getName()), 'docblock' => DocBlockGenerator::fromArray(['tags' => [['name' => 'return', 'description' => $property->getType()]]])]));
} catch (\Exception $e) {
throw AssemblerException::fromException($e);
}
}