io.vrap.codegen.languages.php.model.PhpMethodBuilderRenderer.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of php Show documentation
Show all versions of php Show documentation
RAML API client code generators based on the REST Modeling Framework. https://github.com/vrapio/rest-modeling-framework
package io.vrap.codegen.languages.php.model
import io.vrap.codegen.languages.php.AbstractRequestBuilder
import io.vrap.codegen.languages.php.ClientConstants
import io.vrap.codegen.languages.php.PhpSubTemplates
import io.vrap.codegen.languages.php.extensions.*
import io.vrap.rmf.codegen.io.TemplateFile
import io.vrap.rmf.codegen.rendering.ResourceRenderer
import io.vrap.rmf.codegen.rendering.utils.escapeAll
import io.vrap.rmf.codegen.rendering.utils.keepAngleIndent
import io.vrap.rmf.codegen.types.VrapObjectType
import io.vrap.rmf.codegen.types.VrapTypeProvider
import io.vrap.rmf.raml.model.modules.Api
import io.vrap.rmf.raml.model.resources.Resource
import org.eclipse.emf.ecore.EObject
const val PLACEHOLDER_PARAM_ANNOTATION = "placeholderParam"
class PhpMethodBuilderRenderer constructor(api: Api, vrapTypeProvider: VrapTypeProvider, clientConstants: ClientConstants) : ResourceRenderer, AbstractRequestBuilder(api, vrapTypeProvider, clientConstants) {
private val resourcePackage = "Resource"
override fun render(type: Resource): TemplateFile {
val vrapType = vrapTypeProvider.doSwitch(type as EObject) as VrapObjectType
val content = """
|>
|
|/**
| * @psalm-suppress PropertyNotSetInConstructor${if (type.markDeprecated() || type.deprecated()) """
| * @deprecated""" else ""}
| */
|class ${type.resourceBuilderName()} extends ApiResource
|{
| /**
| * @psalm-param array $!args
| */
| public function __construct(array $!args = [], ClientInterface $!client = null) {
| parent::__construct('${type.fullUri.template}', $!args, $!client);
| }
|
| <<${type.subResources()}>>
| <<${type.methods()}>>
|}
""".trimMargin().keepAngleIndent().forcedLiteralEscape()
val relativeTypeNamespace = vrapType.`package`.toNamespaceName().replace(basePackagePrefix.toNamespaceName() + "\\", "").replace("\\", "/") + "/$resourcePackage"
val relativePath = "src/" + relativeTypeNamespace + "/" + type.resourceBuilderName() + ".php"
return TemplateFile(
relativePath = relativePath,
content = content
)
}
// fun Method.getAllParamNames(): List? {
// val params = this.getAbsoluteUri().getComponents().stream()
// .filter({ uriTemplatePart -> uriTemplatePart is ExpressioResourceByProjectKeyProductsByIDImagesn })
// .flatMap({ uriTemplatePart -> (uriTemplatePart as Expression).varSpecs.stream().map(Function { it.getVariableName() }) })
// .collect(Collectors.toList())
// return if (params.size > 0) {
// params
// } else null
// }
//
// fun getQueryParameters(): List {
// return method.getQueryParameters().stream().filter({ parameter ->
// val placeholderParam = parameter.getAnnotation("placeholderParam") == null
// placeholderParam
// }).collect(Collectors.toList())
// }
//
// fun ResourceCollection.methods(): String {
// return this.resources
// .flatMap { resource -> resource.methods.map { javaBody(resource, it) } }
// .joinToString(separator = "\n\n")
// }
//
// fun javaBody(resource: Resource, method: Method): String {
// val methodReturnType = vrapTypeProvider.doSwitch(method.retyurnType())
// val body = """
// |${method.toComment().escapeAll()}
// |@Retryable(
// | value = { ConnectException.class },
// | maxAttemptsExpression = "#{${'$'}{retry.${method.method.name}.maxAttempts}}",
// | backoff = @Backoff(delayExpression = "#{1}", maxDelayExpression = "#{5}", multiplierExpression = "#{2}"))
// |public ${methodReturnType.fullClassName().escapeAll()} ${method.method.name.toLowerCase()}(${methodParameters(resource, method)}) {
// |
// | final Map\ parameters = new HashMap\<\>();
// |
// | <${resource.allUriParameters.map { "parameters.put(\"${it.name}\",${it.name});" }.joinToString(separator = "\n")}>
// |
// | <${method.mediaType().escapeAll()}>
// |
// | final ParameterizedTypeReference\<${method.retyurnType().toVrapType().fullClassName().escapeAll()}\> type = new ParameterizedTypeReference\<${method.retyurnType().toVrapType().fullClassName().escapeAll()}\>() {};
// | final String fullUri = baseUri + "${resource.fullUri.template}";
// |
// | return restTemplate.exchange(fullUri, HttpMethod.${method.method.name.toUpperCase()}, entity, type, parameters).getBody();
// |
// |}
// """.trimMargin()
// return body
// }
//
// fun methodParameters(resource: Resource, method: Method): String {
//
// val paramList = resource.allUriParameters
// .map { "final ${it.type.toVrapType().simpleName()} ${it.name}" }
// .toMutableList()
//
// if (method.bodies?.isNotEmpty() ?: false) {
// paramList.add(method.bodies[0].type.toVrapType().fullClassName())
// }
// return paramList.joinToString(separator = ", ")
// }
//
// fun Method.mediaType(): String {
// if (this.bodies?.isNotEmpty() ?: false) {
// val result = """
// |final HttpHeaders headers = new HttpHeaders();
// |headers.setContentType(MediaType.parseMediaType(("${this.bodies[0].contentType}")));
// |final HttpEntity<${this.bodies[0].type.toVrapType().fullClassName()}> entity = new HttpEntity<>(body, headers);
// """.trimMargin()
// return result
//
// }
// return "final HttpEntity> entity = null;"
//
// }
//
// fun Method.retyurnType(): AnyType {
// return this.responses
// .filter { it.isSuccessfull() }
// .filter { it.bodies?.isNotEmpty() ?: false }
// .first()
// .let { it.bodies[0].type }
// ?: TypesFactoryImpl.eINSTANCE.createNilType()
// }
}