17
17
18
18
package org .openapitools .codegen .languages ;
19
19
20
- import org .apache .commons .lang3 .StringUtils ;
21
20
import org .openapitools .codegen .CodegenOperation ;
22
- import org .openapitools .codegen .CodegenConfig ;
21
+ import org .openapitools .codegen .CliOption ;
23
22
import org .openapitools .codegen .CodegenConstants ;
24
23
import org .openapitools .codegen .CodegenType ;
25
- import org .openapitools .codegen .DefaultCodegen ;
26
24
import org .openapitools .codegen .SupportingFile ;
27
- import org .openapitools .codegen .utils .ModelUtils ;
28
25
import org .slf4j .Logger ;
29
26
import org .slf4j .LoggerFactory ;
30
27
31
28
import io .swagger .v3 .oas .models .media .*;
32
29
33
30
import java .io .File ;
34
- import java .util .Arrays ;
35
31
import java .util .Map ;
36
32
import java .util .List ;
37
33
import java .util .HashMap ;
38
- import java .util .HashSet ;
39
- import java .util .regex .Matcher ;
40
34
import java .util .Comparator ;
41
35
import java .util .Collections ;
42
36
43
- public class PhpSlimServerCodegen extends DefaultCodegen implements CodegenConfig {
37
+ public class PhpSlimServerCodegen extends AbstractPhpCodegen {
44
38
private static final Logger LOGGER = LoggerFactory .getLogger (PhpSlimServerCodegen .class );
45
39
46
- protected String invokerPackage ;
47
- protected String srcBasePath = "lib" ;
48
40
protected String groupId = "org.openapitools" ;
49
41
protected String artifactId = "openapi-server" ;
50
- protected String artifactVersion = "1.0.0" ;
51
- protected String packagePath = "" ; // empty packagePath (top folder)
52
-
53
-
54
- private String variableNamingConvention = "camelCase" ;
55
42
56
43
public PhpSlimServerCodegen () {
57
44
super ();
@@ -60,62 +47,37 @@ public PhpSlimServerCodegen() {
60
47
// at the moment
61
48
importMapping .clear ();
62
49
50
+ variableNamingConvention = "camelCase" ;
51
+ artifactVersion = "1.0.0" ;
52
+ packagePath = "" ; // empty packagePath (top folder)
63
53
invokerPackage = camelize ("OpenAPIServer" );
64
54
modelPackage = packagePath + "\\ Models" ;
65
55
apiPackage = packagePath ;
66
56
outputFolder = "generated-code" + File .separator + "slim" ;
67
- modelTemplateFiles .put ("model.mustache" , ".php" );
68
57
69
58
// no api files
70
59
apiTemplateFiles .clear ();
60
+ // no test files
61
+ apiTestTemplateFiles .clear ();
62
+ // no doc files
63
+ modelDocTemplateFiles .clear ();
64
+ apiDocTemplateFiles .clear ();
71
65
72
- embeddedTemplateDir = templateDir = "slim" ;
73
-
74
- setReservedWordsLowerCase (
75
- Arrays .asList (
76
- "__halt_compiler" , "abstract" , "and" , "array" , "as" , "break" , "callable" , "case" , "catch" , "class" , "clone" , "const" , "continue" , "declare" , "default" , "die" , "do" , "echo" , "else" , "elseif" , "empty" , "enddeclare" , "endfor" , "endforeach" , "endif" , "endswitch" , "endwhile" , "eval" , "exit" , "extends" , "final" , "for" , "foreach" , "function" , "global" , "goto" , "if" , "implements" , "include" , "include_once" , "instanceof" , "insteadof" , "interface" , "isset" , "list" , "namespace" , "new" , "or" , "print" , "private" , "protected" , "public" , "require" , "require_once" , "return" , "static" , "switch" , "throw" , "trait" , "try" , "unset" , "use" , "var" , "while" , "xor" )
77
- );
66
+ embeddedTemplateDir = templateDir = "php-slim-server" ;
78
67
79
- additionalProperties .put (CodegenConstants .INVOKER_PACKAGE , invokerPackage );
68
+ // additionalProperties.put(CodegenConstants.INVOKER_PACKAGE, invokerPackage);
80
69
additionalProperties .put (CodegenConstants .GROUP_ID , groupId );
81
70
additionalProperties .put (CodegenConstants .ARTIFACT_ID , artifactId );
82
- additionalProperties .put (CodegenConstants .ARTIFACT_VERSION , artifactVersion );
83
-
84
- // ref: http://php.net/manual/en/language.types.intro.php
85
- languageSpecificPrimitives = new HashSet <String >(
86
- Arrays .asList (
87
- "boolean" ,
88
- "int" ,
89
- "integer" ,
90
- "double" ,
91
- "float" ,
92
- "string" ,
93
- "object" ,
94
- "DateTime" ,
95
- "mixed" ,
96
- "number" )
97
- );
98
-
99
- instantiationTypes .put ("array" , "array" );
100
- instantiationTypes .put ("map" , "map" );
101
-
102
- // ref: https://github.com/OAI/OpenAPI-Specification/blob/master/versions/2.0.md#data-types
103
- typeMapping = new HashMap <String , String >();
104
- typeMapping .put ("integer" , "int" );
105
- typeMapping .put ("long" , "int" );
106
- typeMapping .put ("float" , "float" );
107
- typeMapping .put ("double" , "double" );
108
- typeMapping .put ("string" , "string" );
109
- typeMapping .put ("byte" , "int" );
110
- typeMapping .put ("boolean" , "bool" );
111
- typeMapping .put ("date" , "\\ DateTime" );
112
- typeMapping .put ("datetime" , "\\ DateTime" );
113
- typeMapping .put ("file" , "\\ SplFileObject" );
114
- typeMapping .put ("map" , "map" );
115
- typeMapping .put ("array" , "array" );
116
- typeMapping .put ("list" , "array" );
117
- typeMapping .put ("object" , "object" );
118
- typeMapping .put ("binary" , "\\ SplFileObject" );
71
+ // additionalProperties.put(CodegenConstants.ARTIFACT_VERSION, artifactVersion);
72
+
73
+ // override cliOptions from AbstractPhpCodegen
74
+ for (CliOption co : cliOptions ) {
75
+ if (co .getOpt ().equals (AbstractPhpCodegen .VARIABLE_NAMING_CONVENTION )) {
76
+ co .setDescription ("naming convention of variable name, e.g. camelCase." );
77
+ co .setDefault ("camelCase" );
78
+ break ;
79
+ }
80
+ }
119
81
120
82
supportingFiles .add (new SupportingFile ("README.mustache" , packagePath .replace ('/' , File .separatorChar ), "README.md" ));
121
83
supportingFiles .add (new SupportingFile ("composer.json" , packagePath .replace ('/' , File .separatorChar ), "composer.json" ));
@@ -139,212 +101,6 @@ public String getHelp() {
139
101
return "Generates a PHP Slim Framework server library." ;
140
102
}
141
103
142
- @ Override
143
- public String escapeReservedWord (String name ) {
144
- if (this .reservedWordsMappings ().containsKey (name )) {
145
- return this .reservedWordsMappings ().get (name );
146
- }
147
- return "_" + name ;
148
- }
149
-
150
- @ Override
151
- public String apiFileFolder () {
152
- return (outputFolder + File .separator + toPackagePath (apiPackage , srcBasePath ));
153
- }
154
-
155
- @ Override
156
- public String modelFileFolder () {
157
- return (outputFolder + File .separator + toPackagePath (modelPackage , srcBasePath ));
158
- }
159
-
160
- @ Override
161
- public String getTypeDeclaration (Schema p ) {
162
- if (ModelUtils .isArraySchema (p )) {
163
- ArraySchema ap = (ArraySchema ) p ;
164
- Schema inner = ap .getItems ();
165
- return getTypeDeclaration (inner ) + "[]" ;
166
- } else if (ModelUtils .isMapSchema (p )) {
167
- Schema inner = (Schema ) p .getAdditionalProperties ();
168
- return getSchemaType (p ) + "[string," + getTypeDeclaration (inner ) + "]" ;
169
- } else if (!StringUtils .isEmpty (p .get$ref ())) {
170
- String type = super .getTypeDeclaration (p );
171
- return (!languageSpecificPrimitives .contains (type ))
172
- ? "\\ " + modelPackage + "\\ " + type : type ;
173
- }
174
- return super .getTypeDeclaration (p );
175
- }
176
-
177
- @ Override
178
- public String getSchemaType (Schema p ) {
179
- String openAPIType = super .getSchemaType (p );
180
- String type = null ;
181
- if (typeMapping .containsKey (openAPIType )) {
182
- type = typeMapping .get (openAPIType );
183
- if (languageSpecificPrimitives .contains (type )) {
184
- return type ;
185
- } else if (instantiationTypes .containsKey (type )) {
186
- return type ;
187
- }
188
- } else {
189
- type = openAPIType ;
190
- }
191
- if (type == null ) {
192
- return null ;
193
- }
194
- return toModelName (type );
195
- }
196
-
197
- @ Override
198
- public String getTypeDeclaration (String name ) {
199
- if (!languageSpecificPrimitives .contains (name )) {
200
- return "\\ " + modelPackage + "\\ " + name ;
201
- }
202
- return super .getTypeDeclaration (name );
203
- }
204
-
205
- @ Override
206
- public String toDefaultValue (Schema p ) {
207
- return "null" ;
208
- }
209
-
210
- public void setParameterNamingConvention (String variableNamingConvention ) {
211
- this .variableNamingConvention = variableNamingConvention ;
212
- }
213
-
214
- @ Override
215
- public String toVarName (String name ) {
216
- name = sanitizeName (name ); // FIXME: a parameter should not be assigned. Also declare the methods parameters as 'final'.
217
-
218
- if ("camelCase" .equals (variableNamingConvention )) {
219
- // return the name in camelCase style
220
- // phone_number => phoneNumber
221
- name = camelize (name , true );
222
- } else { // default to snake case
223
- // return the name in underscore style
224
- // PhoneNumber => phone_number
225
- name = underscore (name );
226
- }
227
-
228
- // parameter name starting with number won't compile
229
- // need to escape it by appending _ at the beginning
230
- if (name .matches ("^\\ d.*" )) {
231
- name = "_" + name ;
232
- }
233
-
234
- return name ;
235
- }
236
-
237
- @ Override
238
- public String toParamName (String name ) {
239
- // should be the same as variable name
240
- return toVarName (name );
241
- }
242
-
243
- @ Override
244
- public String toModelName (String name ) {
245
- // remove [
246
- name = name .replaceAll ("\\ ]" , "" );
247
-
248
- // Note: backslash ("\\") is allowed for e.g. "\\DateTime"
249
- name = name .replaceAll ("[^\\ w\\ \\ ]+" , "_" ); // FIXME: a parameter should not be assigned. Also declare the methods parameters as 'final'.
250
-
251
- // remove dollar sign
252
- name = name .replaceAll ("$" , "" );
253
-
254
- // model name cannot use reserved keyword
255
- if (isReservedWord (name )) {
256
- LOGGER .warn (name + " (reserved word) cannot be used as model name. Renamed to " + camelize ("model_" + name ));
257
- name = "model_" + name ; // e.g. return => ModelReturn (after camelize)
258
- }
259
-
260
- // model name starts with number
261
- if (name .matches ("^\\ d.*" )) {
262
- LOGGER .warn (name + " (model name starts with number) cannot be used as model name. Renamed to " + camelize ("model_" + name ));
263
- name = "model_" + name ; // e.g. 200Response => Model200Response (after camelize)
264
- }
265
-
266
- // add prefix and/or suffic only if name does not start wth \ (e.g. \DateTime)
267
- if (!name .matches ("^\\ \\ .*" )) {
268
- if (!StringUtils .isEmpty (modelNamePrefix )) {
269
- name = modelNamePrefix + "_" + name ;
270
- }
271
-
272
- if (!StringUtils .isEmpty (modelNameSuffix )) {
273
- name = name + "_" + modelNameSuffix ;
274
- }
275
- }
276
-
277
- // camelize the model name
278
- // phone_number => PhoneNumber
279
- return camelize (name );
280
- }
281
-
282
- @ Override
283
- public String toModelFilename (String name ) {
284
- // should be the same as the model name
285
- return toModelName (name );
286
- }
287
-
288
- @ Override
289
- public String toOperationId (String operationId ) {
290
- // throw exception if method name is empty
291
- if (StringUtils .isEmpty (operationId )) {
292
- throw new RuntimeException ("Empty method name (operationId) not allowed" );
293
- }
294
-
295
- // method name cannot use reserved keyword, e.g. return
296
- if (isReservedWord (operationId )) {
297
- LOGGER .warn (operationId + " (reserved word) cannot be used as method name. Renamed to " + camelize (sanitizeName ("call_" + operationId ), true ));
298
- operationId = "call_" + operationId ;
299
- }
300
-
301
- return camelize (sanitizeName (operationId ), true );
302
- }
303
-
304
- public String toPackagePath (String packageName , String basePath ) {
305
- packageName = packageName .replace (invokerPackage , "" ); // FIXME: a parameter should not be assigned. Also declare the methods parameters as 'final'.
306
- if (basePath != null && basePath .length () > 0 ) {
307
- basePath = basePath .replaceAll ("[\\ \\ /]?$" , "" ) + File .separatorChar ; // FIXME: a parameter should not be assigned. Also declare the methods parameters as 'final'.
308
- }
309
-
310
- String regFirstPathSeparator ;
311
- if ("/" .equals (File .separator )) { // for mac, linux
312
- regFirstPathSeparator = "^/" ;
313
- } else { // for windows
314
- regFirstPathSeparator = "^\\ \\ " ;
315
- }
316
-
317
- String regLastPathSeparator ;
318
- if ("/" .equals (File .separator )) { // for mac, linux
319
- regLastPathSeparator = "/$" ;
320
- } else { // for windows
321
- regLastPathSeparator = "\\ \\ $" ;
322
- }
323
-
324
- return (getPackagePath () + File .separatorChar + basePath
325
- // Replace period, backslash, forward slash with file separator in package name
326
- + packageName .replaceAll ("[\\ .\\ \\ /]" , Matcher .quoteReplacement (File .separator ))
327
- // Trim prefix file separators from package path
328
- .replaceAll (regFirstPathSeparator , "" ))
329
- // Trim trailing file separators from the overall path
330
- .replaceAll (regLastPathSeparator + "$" , "" );
331
- }
332
-
333
- public String getPackagePath () {
334
- return packagePath ;
335
- }
336
-
337
- @ Override
338
- public String escapeQuotationMark (String input ) {
339
- // remove ' to avoid code injection
340
- return input .replace ("'" , "" );
341
- }
342
-
343
- @ Override
344
- public String escapeUnsafeCharacters (String input ) {
345
- return input .replace ("*/" , "" );
346
- }
347
-
348
104
@ Override
349
105
public Map <String , Object > postProcessOperations (Map <String , Object > objs ) {
350
106
Map <String , Object > operations = (Map <String , Object >) objs .get ("operations" );
0 commit comments