/** * @param ParameterReflection $reflectionParameter * @return ParameterGenerator */ public static function fromReflection(ParameterReflection $reflectionParameter) { $param = new ParameterGenerator(); $param->setName($reflectionParameter->getName()); if ($reflectionParameter->isArray()) { $param->setType('array'); } elseif (method_exists($reflectionParameter, 'isCallable') && $reflectionParameter->isCallable()) { $param->setType('callable'); } else { $typeClass = $reflectionParameter->getClass(); if ($typeClass) { $param->setType($typeClass->getName()); } } $param->setPosition($reflectionParameter->getPosition()); if ($reflectionParameter->isOptional()) { $param->setDefaultValue($reflectionParameter->getDefaultValue()); } $param->setPassedByReference($reflectionParameter->isPassedByReference()); return $param; }
/** * @param ParameterReflection $reflectionParameter * @return ParameterGenerator */ public static function fromReflection(ParameterReflection $reflectionParameter) { $param = new ParameterGenerator(); $param->setName($reflectionParameter->getName()); if ($reflectionParameter->isArray()) { $param->setType('array'); } elseif (method_exists($reflectionParameter, 'isCallable') && $reflectionParameter->isCallable()) { $param->setType('callable'); } else { $typeClass = $reflectionParameter->getClass(); if ($typeClass) { $parameterType = $typeClass->getName(); $currentNamespace = $reflectionParameter->getDeclaringClass()->getNamespaceName(); if (!empty($currentNamespace) && substr($parameterType, 0, strlen($currentNamespace)) == $currentNamespace) { $parameterType = substr($parameterType, strlen($currentNamespace) + 1); } else { $parameterType = '\\' . trim($parameterType, '\\'); } $param->setType($parameterType); } } $param->setPosition($reflectionParameter->getPosition()); if ($reflectionParameter->isOptional()) { $param->setDefaultValue($reflectionParameter->getDefaultValue()); } $param->setPassedByReference($reflectionParameter->isPassedByReference()); return $param; }
public function testGenerateIsCorrect() { $parameterGenerator = new ParameterGenerator(); $parameterGenerator->setType('Foo'); $parameterGenerator->setName('bar'); $parameterGenerator->setDefaultValue(15); $this->assertEquals('Foo $bar = 15', $parameterGenerator->generate()); $parameterGenerator->setDefaultValue('foo'); $this->assertEquals('Foo $bar = \'foo\'', $parameterGenerator->generate()); }
/** * Constructor * * @param PropertyGenerator $initializerProperty */ public function __construct(PropertyGenerator $initializerProperty) { parent::__construct('setProxyInitializer'); $initializerParameter = new ParameterGenerator('initializer'); $initializerParameter->setType(Closure::class); $initializerParameter->setDefaultValue(null); $this->setParameter($initializerParameter); $this->setDocblock('{@inheritDoc}'); $this->setBody('$this->' . $initializerProperty->getName() . ' = $initializer;'); }
/** * Creates a new {@link \bitExpert\Disco\Proxy\Configuration\MethodGenerator\HasAlias}. * * @param ReflectionClass $originalClass * @param AliasesProperty $aliasesProperty * @throws InvalidArgumentException */ public function __construct(ReflectionClass $originalClass, AliasesProperty $aliasesProperty) { parent::__construct('hasAlias'); $aliasParameter = new ParameterGenerator('alias'); $aliasParameter->setType('string'); $this->setParameter($aliasParameter); $this->setVisibility(self::VISIBILITY_PUBLIC); $this->setReturnType('bool'); $this->setBody('return !empty($' . $aliasParameter->getName() . ') && ' . 'isset($this->' . $aliasesProperty->getName() . '[$' . $aliasParameter->getName() . ']);'); }
/** * Constructor * * @param PropertyGenerator $suffixInterceptor */ public function __construct(PropertyGenerator $suffixInterceptor) { parent::__construct('setMethodSuffixInterceptor'); $interceptor = new ParameterGenerator('suffixInterceptor'); $interceptor->setType(Closure::class); $interceptor->setDefaultValue(null); $this->setParameter(new ParameterGenerator('methodName', 'string')); $this->setParameter($interceptor); $this->setDocblock('{@inheritDoc}'); $this->setBody('$this->' . $suffixInterceptor->getName() . '[$methodName] = $suffixInterceptor;'); }
/** * Creates a new {@link \bitExpert\Disco\Proxy\Configuration\MethodGenerator\GetAlias}. * * @param ReflectionClass $originalClass * @param AliasesProperty $aliasesProperty * @throws InvalidArgumentException */ public function __construct(ReflectionClass $originalClass, AliasesProperty $aliasesProperty) { parent::__construct('getAlias'); $aliasParameter = new ParameterGenerator('alias'); $aliasParameter->setType('string'); $body = 'if ($this->hasAlias($' . $aliasParameter->getName() . ')) {' . PHP_EOL; $body .= ' $methodname = $this->' . $aliasesProperty->getName() . '[$' . $aliasParameter->getName() . '];' . PHP_EOL; $body .= ' return $this->$methodname();' . PHP_EOL; $body .= '}' . PHP_EOL . PHP_EOL; $body .= 'throw new ' . BeanNotFoundException::class . '(sprintf(\'Alias "%s" is not defined!\', $' . $aliasParameter->getName() . '));' . PHP_EOL; $this->setParameter($aliasParameter); $this->setVisibility(self::VISIBILITY_PUBLIC); $this->setBody($body); }
/** * Constructor * * @param ReflectionClass $originalClass * @param PropertyGenerator $valueHolder * @param PropertyGenerator $prefixInterceptors * @param PropertyGenerator $suffixInterceptors */ public function __construct(ReflectionClass $originalClass, PropertyGenerator $valueHolder, PropertyGenerator $prefixInterceptors, PropertyGenerator $suffixInterceptors) { parent::__construct('staticProxyConstructor', [], static::FLAG_PUBLIC | static::FLAG_STATIC); $prefix = new ParameterGenerator('prefixInterceptors'); $suffix = new ParameterGenerator('suffixInterceptors'); $prefix->setDefaultValue([]); $suffix->setDefaultValue([]); $prefix->setType('array'); $suffix->setType('array'); $this->setParameter(new ParameterGenerator('wrappedObject')); $this->setParameter($prefix); $this->setParameter($suffix); $this->setReturnType($originalClass->getName()); $this->setDocblock("Constructor to setup interceptors\n\n" . "@param \\" . $originalClass->getName() . " \$wrappedObject\n" . "@param \\Closure[] \$prefixInterceptors method interceptors to be used before method logic\n" . "@param \\Closure[] \$suffixInterceptors method interceptors to be used before method logic\n\n" . "@return self"); $this->setBody('static $reflection;' . "\n\n" . '$reflection = $reflection ?: $reflection = new \\ReflectionClass(__CLASS__);' . "\n" . '$instance = (new \\ReflectionClass(get_class()))->newInstanceWithoutConstructor();' . "\n\n" . UnsetPropertiesGenerator::generateSnippet(Properties::fromReflectionClass($originalClass), 'instance') . '$instance->' . $valueHolder->getName() . " = \$wrappedObject;\n" . '$instance->' . $prefixInterceptors->getName() . " = \$prefixInterceptors;\n" . '$instance->' . $suffixInterceptors->getName() . " = \$suffixInterceptors;\n\n" . 'return $instance;'); }
/** * Constructor * * @param ReflectionClass $originalClass */ public function __construct(ReflectionClass $originalClass) { parent::__construct('staticProxyConstructor', [], static::FLAG_PUBLIC | static::FLAG_STATIC); $localizedObject = new ParameterGenerator('localizedObject'); $prefix = new ParameterGenerator('prefixInterceptors'); $suffix = new ParameterGenerator('suffixInterceptors'); $localizedObject->setType($originalClass->getName()); $prefix->setDefaultValue([]); $suffix->setDefaultValue([]); $prefix->setType('array'); $suffix->setType('array'); $this->setParameter($localizedObject); $this->setParameter($prefix); $this->setParameter($suffix); $this->setDocblock("Constructor to setup interceptors\n\n" . "@param \\" . $originalClass->getName() . " \$localizedObject\n" . "@param \\Closure[] \$prefixInterceptors method interceptors to be used before method logic\n" . "@param \\Closure[] \$suffixInterceptors method interceptors to be used before method logic\n\n" . "@return self"); $this->setBody('static $reflection;' . "\n\n" . '$reflection = $reflection ?: $reflection = new \\ReflectionClass(__CLASS__);' . "\n" . '$instance = $reflection->newInstanceWithoutConstructor();' . "\n\n" . '$instance->bindProxyProperties($localizedObject, $prefixInterceptors, $suffixInterceptors);' . "\n\n" . 'return $instance;'); }
/** * @param ParameterReflection $reflectionParameter * @return ParameterGenerator */ public static function fromReflection(ParameterReflection $reflectionParameter) { $param = new ParameterGenerator(); $param->setName($reflectionParameter->getName()); if ($type = self::extractFQCNTypeFromReflectionType($reflectionParameter)) { $param->setType($type); } $param->setPosition($reflectionParameter->getPosition()); $variadic = method_exists($reflectionParameter, 'isVariadic') && $reflectionParameter->isVariadic(); $param->setVariadic($variadic); if (!$variadic && $reflectionParameter->isOptional()) { try { $param->setDefaultValue($reflectionParameter->getDefaultValue()); } catch (\ReflectionException $e) { $param->setDefaultValue(null); } } $param->setPassedByReference($reflectionParameter->isPassedByReference()); return $param; }
/** * Creates a new {@link \bitExpert\Disco\Proxy\Configuration\MethodGenerator\Constructor}. * * @param ReflectionClass $originalClass * @param ParameterValuesProperty $parameterValuesProperty * @param SessionBeansProperty $sessionBeansProperty * @param BeanFactoryConfigurationProperty $beanFactoryConfigurationProperty * @param BeanPostProcessorsProperty $beanPostProcessorsProperty * @param string[] $beanPostProcessorMethodNames */ public function __construct(ReflectionClass $originalClass, ParameterValuesProperty $parameterValuesProperty, SessionBeansProperty $sessionBeansProperty, BeanFactoryConfigurationProperty $beanFactoryConfigurationProperty, BeanPostProcessorsProperty $beanPostProcessorsProperty, array $beanPostProcessorMethodNames) { parent::__construct('__construct'); $beanFactoryConfigurationParameter = new ParameterGenerator('config'); $beanFactoryConfigurationParameter->setType(BeanFactoryConfiguration::class); $parametersParameter = new ParameterGenerator('params'); $parametersParameter->setDefaultValue([]); $body = '$this->' . $parameterValuesProperty->getName() . ' = $' . $parametersParameter->getName() . ';' . PHP_EOL; $body .= '$this->' . $beanFactoryConfigurationProperty->getName() . ' = $' . $beanFactoryConfigurationParameter->getName() . ';' . PHP_EOL; $body .= '$this->' . $sessionBeansProperty->getName() . ' = $' . $beanFactoryConfigurationParameter->getName() . '->getSessionBeanStore();' . PHP_EOL; $body .= '// register {@link \\bitExpert\\Disco\\BeanPostProcessor} instances' . PHP_EOL; $body .= '$this->' . $beanPostProcessorsProperty->getName() . '[] = new \\bitExpert\\Disco\\BeanFactoryPostProcessor();' . PHP_EOL; foreach ($beanPostProcessorMethodNames as $methodName) { $body .= '$this->' . $beanPostProcessorsProperty->getName() . '[] = $this->' . $methodName . '(); '; $body .= PHP_EOL; } $this->setParameter($beanFactoryConfigurationParameter); $this->setParameter($parametersParameter); $this->setBody($body); $this->setDocBlock("@override constructor"); }
/** * Generate method * * @param string $methodName * @return void */ protected function generateMethod($methodName) { $methodReflection = $this->_method[$methodName]; $docBlock = new DocBlockGenerator(); $docBlock->setShortDescription("Delicate {$methodName}() to __call() method "); if ($methodReflection->getDocComment()) { $docBlockReflection = new DocBlockReflection($methodReflection); $docBlock->fromReflection($docBlockReflection); } $method = new MethodGenerator(); $method->setName($methodName); $method->setDocBlock($docBlock); $method->setBody(sprintf(self::METHOD_TEMPLATE, $methodName)); if ($methodReflection->isPublic()) { $method->setVisibility(MethodGenerator::VISIBILITY_PUBLIC); } else { if ($methodReflection->isProtected()) { $method->setVisibility(MethodGenerator::VISIBILITY_PROTECTED); } else { if ($methodReflection->isPrivate()) { $method->setVisibility(MethodGenerator::VISIBILITY_PRIVATE); } } } foreach ($methodReflection->getParameters() as $parameter) { $parameterGenerator = new ParameterGenerator(); $parameterGenerator->setPosition($parameter->getPosition()); $parameterGenerator->setName($parameter->getName()); $parameterGenerator->setPassedByReference($parameter->isPassedByReference()); if ($parameter->isDefaultValueAvailable()) { $parameterGenerator->setDefaultValue($parameter->getDefaultValue()); } if ($parameter->isArray()) { $parameterGenerator->setType('array'); } if ($typeClass = $parameter->getClass()) { $parameterGenerator->setType($typeClass->getName()); } $method->setParameter($parameterGenerator); } $this->addMethodFromGenerator($method); }
protected function viewStub($part) { /** * @var $part \Model\Generator\Part\Model */ /** * @var $file \Model\Code\Generator\FileGenerator */ $file = $part->getFile(); $table = $part->getTable(); $tableNameAsCamelCase = $table->getNameAsCamelCase(); $entity = $tableNameAsCamelCase . 'Entity'; $collection = $tableNameAsCamelCase . 'Collection'; $p = new \Zend\Code\Generator\ParameterGenerator('cond'); $p->setType('Cond'); $p->setDefaultValue(null); $params[] = $p; $docblock = new DocBlockGenerator('Получить объект условий в виде представления \'Extended\' $param Cond $cond $return Cond '); $method = new MethodGenerator(); $method->setName('getCondAsExtendedView'); $method->setVisibility(AbstractMemberGenerator::VISIBILITY_PUBLIC); $method->setFinal(false); $method->setDocBlock($docblock); $method->setParameters($params); $method->setBody(<<<EOS \$cond = \$this->prepareCond(\$cond); \$cond->where(array('status' => 'active')); return \$cond; EOS ); $file->getClass()->addMethodFromGenerator($method); $p = new \Zend\Code\Generator\ParameterGenerator('cond'); $p->setType('Cond'); $p->setDefaultValue(null); $params[] = $p; $docblock = new DocBlockGenerator('Получить элемент в виде представления \'Extended\' @param Cond $cond @return ' . $entity); $method = new MethodGenerator(); $method->setName('getAsExtendedView'); $method->setVisibility(AbstractMemberGenerator::VISIBILITY_PUBLIC); $method->setFinal(false); $method->setDocBlock($docblock); $method->setParameters($params); $method->setBody(<<<EOS \$cond = \$this->getCondAsExtendedView(\$cond); return \$this->get(\$cond); EOS ); $file->getClass()->addMethodFromGenerator($method); $p = new \Zend\Code\Generator\ParameterGenerator('cond'); $p->setType('Cond'); $p->setDefaultValue(null); $params[] = $p; $docblock = new DocBlockGenerator('Получить коллекцию в виде представления \'Extended\' @param Cond $cond @return ' . $collection); $method = new MethodGenerator(); $method->setName('getCollectionAsExtendedView'); $method->setVisibility(AbstractMemberGenerator::VISIBILITY_PUBLIC); $method->setFinal(false); $method->setDocBlock($docblock); $method->setParameters($params); $method->setBody(<<<EOS \$cond = \$this->getCondAsExtendedView(\$cond); return \$this->getCollection(\$cond); EOS ); $file->getClass()->addMethodFromGenerator($method); }
private function buildClass($replacement) { $type = $this->splitNsandClass($replacement['originalFullyQualifiedType']); $class = new ClassGenerator(); $class->setName($type['class']); $class->setNamespaceName($type['ns']); $class->setExtendedClass('\\Brads\\Ppm\\Proxy'); $properties = []; $methods = []; $implementedInterfaces = []; foreach ($versions as $version => $info) { foreach ($info['files'] as $file) { echo "Parsing: " . $this->vendorDir . '/' . $package . '/' . $version . '/' . $file . "\n"; $parsedFile = new ReflectionFile($this->vendorDir . '/' . $package . '/' . $version . '/' . $file); $parsedClass = $parsedFile->getFileNamespace($info['toNs'])->getClass($info['toNs'] . '\\' . $type['class']); if ($parsedClass->getInterfaceNames() != null) { $implementedInterfaces = array_merge($implementedInterfaces, $parsedClass->getInterfaceNames()); } foreach ($parsedClass->getMethods() as $method) { if ($method->isPublic()) { $generatedMethod = new MethodGenerator(); $generatedMethod->setName($method->name); $generatedMethod->setBody('echo "Hello world!";'); $generatedMethod->setAbstract($method->isAbstract()); $generatedMethod->setFinal($method->isFinal()); $generatedMethod->setStatic($method->isStatic()); $generatedMethod->setVisibility(MethodGenerator::VISIBILITY_PUBLIC); foreach ($method->getParameters() as $param) { $generatedParam = new ParameterGenerator(); $generatedParam->setName($param->name); if ($param->hasType()) { $generatedParam->setType($param->getType()); } //$generatedParam->setDefaultValue($param->getDefaultValue()); $generatedParam->setPosition($param->getPosition()); $generatedParam->setVariadic($param->isVariadic()); $generatedParam->setPassedByReference($param->isPassedByReference()); $generatedMethod->setParameter($generatedParam); } $existingMethod = Linq::from($methods)->firstOrDefault(null, function (MethodGenerator $v) use($method) { return $v->getName() == $method->name; }); if ($existingMethod != null) { $existingParams = $existingMethod->getParameters(); foreach ($generatedMethod->getParameters() as $newParam) { $existingParam = Linq::from($existingParams)->firstOrDefault(function (ParameterGenerator $v) { return $v->getName() == $newParam->getName(); }); if ($existingParam == null) { $existingMethod->setParameter($newParam); } } } else { $methods[] = $generatedMethod; } } } foreach ($parsedClass->getProperties() as $prop) { //$properties[] = PropertyGenerator::fromReflection($prop); } } } $class->setImplementedInterfaces($implementedInterfaces); $class->addMethods($methods); $class->addProperties($properties); return (new FileGenerator(['classes' => [$class]]))->generate(); }
public function generateMethodsByIndex($part) { /** @var $part \Model\Generator\Part\Model */ /** @var $file \Model\Code\Generator\FileGenerator */ $file = $part->getFile(); $table = $part->getTable(); $tableName = $table->getName(); $indexList = $table->getIndex(); foreach ($indexList as $index) { $params = $getBy = array(); $prepare = ''; $paramNames = array(); $checkForEmpty = ''; $indexColumn = 'id'; $where = ''; foreach ($index as $column) { if ($index->getName() == 'PRIMARY') { $indexColumn = $column->getName(); } //echo $index->getName() . "\n"; if ($index->getName() == 'PRIMARY' || $index->count() == 1 && ($link = $table->getLinkByColumn($column, $table->getName()))) { continue 2; } $link = $table->getLinkByColumn($column, $table->getName()); if ($link) { $indexColumnName = $link->getForeignEntity(); $indexColumnNameAsVar = $link->getForeignEntityAsVar(); $indexColumnNameAsCamelCase = $link->getForeignEntityAsCamelCase(); } else { $indexColumnName = $column->getName(); $indexColumnNameAsVar = $column->getNameAsVar(); $indexColumnNameAsCamelCase = $column->getNameAsCamelCase(); } $indexColumnField = $column->getName(); $params[] = new \Zend\Code\Generator\ParameterGenerator($indexColumnNameAsVar); $getBy[] = $indexColumnNameAsCamelCase; $paramNames[] = '$' . $indexColumnNameAsVar; if ($link) { $foreignTableNameAsCamelCase = $link->getForeignTable()->getNameAsCamelCase(); $prepare .= <<<EOS \${$indexColumnNameAsVar}Ids = {$foreignTableNameAsCamelCase}Model::getInstance()->getIdsFromMixed(\${$indexColumnNameAsVar}); EOS; } else { $prepare .= <<<EOS \${$indexColumnNameAsVar}Ids = \$this->filterValue(\${$indexColumnNameAsVar}, '{$indexColumnName}'); EOS; } $checkForEmpty .= "empty(\${$indexColumnNameAsVar}Ids) || "; $where .= "'`{$tableName}`.`{$indexColumnField}`' => \${$indexColumnNameAsVar}Ids,\n"; } //@method int borp() borp(int $int1, int $int2) multiply two integers $p = new \Zend\Code\Generator\ParameterGenerator('cond'); $p->setDefaultValue(null); $p->setType('Cond'); $params[] = $p; $where = 'array(' . rtrim($where, " \n,") . ")"; $checkForEmpty = rtrim($checkForEmpty, "\r\n |"); $method = new \Zend\Code\Generator\MethodGenerator(); $method->setName('getBy' . implode('And', $getBy)); $method->setVisibility(\Zend\Code\Generator\AbstractMemberGenerator::VISIBILITY_PUBLIC); //$method->setDocBlock($docblock); $method->setParameters($params); $method->setBody(<<<EOS \$cond = \$this->prepareCond(\$cond); {$prepare} if ({$checkForEmpty}) { return \$cond->getEmptySelectResult(); } \$cond->where({$where}); return \$this->get(\$cond); EOS ); try { $part->getFile()->getClass()->addMethodFromGenerator($method); } catch (\Exception $e) { } } }
/** * @group 6023 * * @coversNothing */ public function testGeneratedParametersHaveEscapedDefaultValues() { $parameter = new ParameterGenerator(); $parameter->setName('foo'); $parameter->setDefaultValue("\\'"); $parameter->setType('stdClass'); $this->assertSame("stdClass \$foo = '\\\\\\''", $parameter->generate()); }
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; }
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); }