|
| 1 | +/* |
| 2 | + * Copyright 2018 OpenAPI-Generator Contributors (https://openapi-generator.tech) |
| 3 | + * Copyright 2018 SmartBear Software |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package org.openapitools.codegen.languages; |
| 19 | + |
| 20 | +import io.swagger.models.properties.*; |
| 21 | +import io.swagger.v3.oas.models.*; |
| 22 | +import io.swagger.v3.oas.models.media.*; |
| 23 | +import io.swagger.v3.oas.models.parameters.*; |
| 24 | +import org.openapitools.codegen.*; |
| 25 | + |
| 26 | +import java.io.File; |
| 27 | +import java.util.*; |
| 28 | + |
| 29 | +import io.swagger.v3.oas.models.media.*; |
| 30 | +import io.swagger.v3.oas.models.PathItem; |
| 31 | +import io.swagger.v3.oas.models.PathItem.HttpMethod; |
| 32 | +import io.swagger.v3.oas.models.*; |
| 33 | +import io.swagger.v3.oas.models.parameters.*; |
| 34 | +import io.swagger.v3.core.util.Yaml; |
| 35 | + |
| 36 | +import org.apache.commons.lang3.StringUtils; |
| 37 | +import org.slf4j.Logger; |
| 38 | +import org.slf4j.LoggerFactory; |
| 39 | + |
| 40 | + |
| 41 | +public class PhpLaravelServerCodegen extends AbstractPhpCodegen implements CodegenConfig { |
| 42 | + @SuppressWarnings("hiding") |
| 43 | + protected String apiVersion = "1.0.0"; |
| 44 | + protected String variableNamingConvention = "camelCase"; |
| 45 | + |
| 46 | + /** |
| 47 | + * Configures the type of generator. |
| 48 | + * |
| 49 | + * @return the CodegenType for this generator |
| 50 | + * @see org.openapitools.codegen.CodegenType |
| 51 | + */ |
| 52 | + public CodegenType getTag() { |
| 53 | + return CodegenType.SERVER; |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * Configures a friendly name for the generator. This will be used by the generator |
| 58 | + * to select the library with the -g flag. |
| 59 | + * |
| 60 | + * @return the friendly name for the generator |
| 61 | + */ |
| 62 | + public String getName() { |
| 63 | + return "php-laravel"; |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * Returns human-friendly help for the generator. Provide the consumer with help |
| 68 | + * tips, parameters here |
| 69 | + * |
| 70 | + * @return A string value for the help message |
| 71 | + */ |
| 72 | + public String getHelp() { |
| 73 | + return "Generates a PHP laravel server library."; |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Class constructor |
| 78 | + */ |
| 79 | + public PhpLaravelServerCodegen() { |
| 80 | + super(); |
| 81 | + |
| 82 | + embeddedTemplateDir = templateDir = "php-laravel"; |
| 83 | + |
| 84 | + /* |
| 85 | + * packPath |
| 86 | + */ |
| 87 | + invokerPackage = "php-laravel"; |
| 88 | + packagePath = ""; |
| 89 | + outputFolder = packagePath + File.separator + srcBasePath; |
| 90 | + |
| 91 | + /* |
| 92 | + * Api Package. Optional, if needed, this can be used in templates |
| 93 | + */ |
| 94 | + apiPackage = "app.Http.Controllers"; |
| 95 | + |
| 96 | + /* |
| 97 | + * Model Package. Optional, if needed, this can be used in templates |
| 98 | + */ |
| 99 | + modelPackage = "models"; |
| 100 | + |
| 101 | + // template files want to be ignored |
| 102 | + modelTemplateFiles.clear(); |
| 103 | + apiTestTemplateFiles.clear(); |
| 104 | + apiDocTemplateFiles.clear(); |
| 105 | + modelDocTemplateFiles.clear(); |
| 106 | + |
| 107 | + /* |
| 108 | + * Additional Properties. These values can be passed to the templates and |
| 109 | + * are available in models, apis, and supporting files |
| 110 | + */ |
| 111 | + additionalProperties.put("apiVersion", apiVersion); |
| 112 | + |
| 113 | + /* |
| 114 | + * Supporting Files. You can write single files for the generator with the |
| 115 | + * entire object tree available. If the input file has a suffix of `.mustache |
| 116 | + * it will be processed by the template engine. Otherwise, it will be copied |
| 117 | + */ |
| 118 | + supportingFiles.add(new SupportingFile("composer.mustache", outputFolder, "composer.json")); |
| 119 | + supportingFiles.add(new SupportingFile("README.md", outputFolder, "README.md")); |
| 120 | + supportingFiles.add(new SupportingFile("artisan", outputFolder, "artisan")); |
| 121 | + |
| 122 | + supportingFiles.add(new SupportingFile("bootstrap/cache/.gitignore", outputFolder + File.separator + "bootstrap" + File.separator + "cache", ".gitignore")); |
| 123 | + supportingFiles.add(new SupportingFile("bootstrap/app.php", outputFolder + File.separator + "bootstrap", "app.php")); |
| 124 | + supportingFiles.add(new SupportingFile("bootstrap/testingAutoload.php", outputFolder + File.separator + "bootstrap", "testingAutoload.php")); |
| 125 | + |
| 126 | + /* /public/ */ |
| 127 | + supportingFiles.add(new SupportingFile("public/.htaccess", outputFolder + File.separator + "public", ".htaccess")); |
| 128 | + supportingFiles.add(new SupportingFile("public/favicon.ico", outputFolder + File.separator + "public", "favicon.ico")); |
| 129 | + supportingFiles.add(new SupportingFile("public/index.php", outputFolder + File.separator + "public", "index.php")); |
| 130 | + supportingFiles.add(new SupportingFile("public/robots.txt", outputFolder + File.separator + "public", "robots.txt")); |
| 131 | + |
| 132 | + /* routes */ |
| 133 | + supportingFiles.add(new SupportingFile("routes/api.mustache", outputFolder + File.separator + "routes", "api.php")); |
| 134 | + supportingFiles.add(new SupportingFile("routes/web.mustache", outputFolder + File.separator + "routes", "web.php")); |
| 135 | + |
| 136 | + /* /app/Http/Controllers/ */ |
| 137 | + supportingFiles.add(new SupportingFile("app/Http/Controllers/Controller.php", outputFolder + File.separator + "app" + File.separator + "Http" + File.separator + "Controllers", "Controller.php")); |
| 138 | + } |
| 139 | + |
| 140 | + // override with any special post-processing |
| 141 | + @Override |
| 142 | + public Map<String, Object> postProcessOperations(Map<String, Object> objs) { |
| 143 | + @SuppressWarnings("unchecked") |
| 144 | + Map<String, Object> objectMap = (Map<String, Object>) objs.get("operations"); |
| 145 | + @SuppressWarnings("unchecked") |
| 146 | + List<CodegenOperation> operations = (List<CodegenOperation>) objectMap.get("operation"); |
| 147 | + |
| 148 | + for (CodegenOperation op : operations) { |
| 149 | + op.httpMethod = op.httpMethod.toLowerCase(); |
| 150 | + // check to see if the path contains ".", which is not supported by PHP laravel |
| 151 | + // ref: https://github.com/swagger-api/swagger-codegen/issues/6897 |
| 152 | + if (op.path != null && op.path.contains(".")) { |
| 153 | + throw new IllegalArgumentException("'.' (dot) is not supported by PHP laravel. Please refer to https://github.com/swagger-api/swagger-codegen/issues/6897 for more info."); |
| 154 | + } |
| 155 | + } |
| 156 | + |
| 157 | + // sort the endpoints in ascending to avoid the route priority issure. |
| 158 | + // https://github.com/swagger-api/swagger-codegen/issues/2643 |
| 159 | + Collections.sort(operations, new Comparator<CodegenOperation>() { |
| 160 | + @Override |
| 161 | + public int compare(CodegenOperation lhs, CodegenOperation rhs) { |
| 162 | + return lhs.path.compareTo(rhs.path); |
| 163 | + } |
| 164 | + }); |
| 165 | + |
| 166 | + return objs; |
| 167 | + } |
| 168 | + |
| 169 | + @Override |
| 170 | + public String toApiName(String name) { |
| 171 | + if (name.isEmpty()) { |
| 172 | + return "DefaultController"; |
| 173 | + } |
| 174 | + |
| 175 | + return camelize(name, false) + "Controller"; |
| 176 | + } |
| 177 | + |
| 178 | + protected String controllerFileFolder() { |
| 179 | + return (outputFolder + File.separator + srcBasePath + File.separator + "app" + File.separator + "Http" + File.separator + "Controllers"); |
| 180 | + } |
| 181 | + |
| 182 | + @Override |
| 183 | + public String apiFilename(String templateName, String tag) { |
| 184 | + String suffix = apiTemplateFiles().get(templateName); |
| 185 | + if (templateName.equals("api.mustache")) { |
| 186 | + return controllerFileFolder() + '/' + toControllerName(tag) + suffix; |
| 187 | + } |
| 188 | + |
| 189 | + return apiFileFolder() + '/' + toApiFilename(tag) + suffix; |
| 190 | + } |
| 191 | + |
| 192 | + protected String toControllerName(String name) { |
| 193 | + if (name.isEmpty()) { |
| 194 | + return "DefaultController"; |
| 195 | + } |
| 196 | + |
| 197 | + return camelize(name, false) + "Controller"; |
| 198 | + } |
| 199 | +} |
0 commit comments