All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.vrap.codegen.languages.php.model.DocsProducer.kt Maven / Gradle / Ivy

Go to download

RAML API client code generators based on the REST Modeling Framework. https://github.com/vrapio/rest-modeling-framework

There is a newer version: 1.0.0-20241120142200
Show newest version
package io.vrap.codegen.languages.php.model

import com.google.common.collect.Lists
import io.vrap.codegen.languages.extensions.getMethodName
import io.vrap.codegen.languages.php.AbstractRequestBuilder
import io.vrap.codegen.languages.php.ClientConstants
import io.vrap.codegen.languages.php.extensions.*
import io.vrap.rmf.codegen.io.TemplateFile
import io.vrap.rmf.codegen.rendering.FileProducer
import io.vrap.rmf.codegen.rendering.utils.escapeAll
import io.vrap.rmf.codegen.rendering.utils.keepAngleIndent
import io.vrap.rmf.codegen.types.VrapTypeProvider
import io.vrap.rmf.raml.model.modules.Api
import io.vrap.rmf.raml.model.resources.Method
import io.vrap.rmf.raml.model.resources.Resource

class DocsProducer constructor(api: Api, vrapTypeProvider: VrapTypeProvider, clientConstants: ClientConstants) : FileProducer, AbstractRequestBuilder(api, vrapTypeProvider, clientConstants) {

    override fun produceFiles(): List = listOf(
            requestBuilder(api)
    )

    private fun requestBuilder(api: Api): TemplateFile {
        val resources = api.allContainedResources.associate { it.toResourceName() to it }.toSortedMap()
        return TemplateFile(relativePath = "docs/RequestBuilder.md",
                content = """
                    |# RequestBuilder
                    |
                    |In order to be able to build request objects you can use the RequestBuilder. The following methods return a HTTP request instance of Guzzle [PSR-7](https://github.com/guzzle/psr7).
                    |
                    |```php
                    |use ${clientPackageName.toNamespaceName().escapeAll()}\\${rootResource()};
                    |
                    |$!root = new ${rootResource()}();
                    |```
                    |
                    |<<${resources.values.flatMap { resource -> resource.methods.map { method -> resourceInfo(resource, method) }}.joinToString("\n")}>>
                """.trimMargin().keepAngleIndent().forcedLiteralEscape())
    }

    private fun resourceInfo(resource: Resource, method: Method): String {
        val builderChain = resource.resourcePathList().map { r -> "${r.getMethodName()}(${if (r.relativeUri.paramValues().isNotEmpty()) "\"${r.relativeUri.paramValues().joinToString("\", \"") }\"" else ""})" }
                .plus("${method.method}(${if (method.firstBody() != null) "null" else ""})")

        return """
            |## `${builderChain.joinToString("->")}`
            |
            |${method.description?.value}
            |
            |### Example
            |```php
            |use ${clientPackageName.toNamespaceName().escapeAll()}\\${rootResource()};
            |
            |$!builder =  new ${rootResource()}();
            |$!request = $!builder
            |                <<${builderChain.joinToString("\n->", "->")}>>;
            |```
        """.trimMargin()
    }

    private fun Resource.resourcePathList(): List {
        val path = Lists.newArrayList()
        if (this.fullUri.template == "/") {
            return path
        }
        path.add(this)
        var t = this.eContainer()
        while (t is Resource) {
            val template = t.fullUri.template
            if (template != "/") {
                path.add(t)
            }
            t = t.eContainer()
        }
        return Lists.reverse(path)
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy