Skip to content

Commit 9eeeded

Browse files
ybelenkowing328
authored andcommitted
[Slim] Improve codebase decouple (#438)
* [Slim] Decouple Api files into separated PHP Classes This enhancement required for modular testing and code coverage generating. * [Slim] Define all app routes in SlimRouter PHP Class. Generate new samples
1 parent 3408866 commit 9eeeded

74 files changed

Lines changed: 2000 additions & 941 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/PhpSlimServerCodegen.java

Lines changed: 35 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,11 @@ public PhpSlimServerCodegen() {
4848
variableNamingConvention = "camelCase";
4949
artifactVersion = "1.0.0";
5050
packagePath = ""; // empty packagePath (top folder)
51-
invokerPackage = camelize("OpenAPIServer");
52-
modelPackage = packagePath + "\\Models";
53-
apiPackage = packagePath;
51+
setInvokerPackage("OpenAPIServer");
52+
apiPackage = invokerPackage + "\\" + apiDirName;
53+
modelPackage = invokerPackage + "\\" + modelDirName;
5454
outputFolder = "generated-code" + File.separator + "slim";
5555

56-
// no api files
57-
apiTemplateFiles.clear();
5856
// no test files
5957
apiTestTemplateFiles.clear();
6058
// no doc files
@@ -63,11 +61,9 @@ public PhpSlimServerCodegen() {
6361

6462
embeddedTemplateDir = templateDir = "php-slim-server";
6563

66-
// additionalProperties.put(CodegenConstants.INVOKER_PACKAGE, invokerPackage);
6764
additionalProperties.put(CodegenConstants.GROUP_ID, groupId);
6865
additionalProperties.put(CodegenConstants.ARTIFACT_ID, artifactId);
69-
// additionalProperties.put(CodegenConstants.ARTIFACT_VERSION, artifactVersion);
70-
66+
7167
// override cliOptions from AbstractPhpCodegen
7268
for (CliOption co : cliOptions) {
7369
if (co.getOpt().equals(AbstractPhpCodegen.VARIABLE_NAMING_CONVENTION)) {
@@ -76,12 +72,6 @@ public PhpSlimServerCodegen() {
7672
break;
7773
}
7874
}
79-
80-
supportingFiles.add(new SupportingFile("README.mustache", packagePath.replace('/', File.separatorChar), "README.md"));
81-
supportingFiles.add(new SupportingFile("composer.json", packagePath.replace('/', File.separatorChar), "composer.json"));
82-
supportingFiles.add(new SupportingFile("index.mustache", packagePath.replace('/', File.separatorChar), "index.php"));
83-
supportingFiles.add(new SupportingFile(".htaccess", packagePath.replace('/', File.separatorChar), ".htaccess"));
84-
supportingFiles.add(new SupportingFile(".gitignore", packagePath.replace('/', File.separatorChar), ".gitignore"));
8575
}
8676

8777
@Override
@@ -99,6 +89,37 @@ public String getHelp() {
9989
return "Generates a PHP Slim Framework server library.";
10090
}
10191

92+
@Override
93+
public String apiFileFolder() {
94+
if (apiPackage.matches("^" + invokerPackage + "\\\\*(.+)")) {
95+
// need to strip out invokerPackage from path
96+
return (outputFolder + File.separator + toPackagePath(apiPackage.replaceFirst("^" + invokerPackage + "\\\\*(.+)", "$1"), srcBasePath));
97+
}
98+
return (outputFolder + File.separator + toPackagePath(apiPackage, srcBasePath));
99+
}
100+
101+
@Override
102+
public String modelFileFolder() {
103+
if (modelPackage.matches("^" + invokerPackage + "\\\\*(.+)")) {
104+
// need to strip out invokerPackage from path
105+
return (outputFolder + File.separator + toPackagePath(modelPackage.replaceFirst("^" + invokerPackage + "\\\\*(.+)", "$1"), srcBasePath));
106+
}
107+
return (outputFolder + File.separator + toPackagePath(modelPackage, srcBasePath));
108+
}
109+
110+
@Override
111+
public void processOpts() {
112+
super.processOpts();
113+
114+
supportingFiles.add(new SupportingFile("README.mustache", getPackagePath(), "README.md"));
115+
supportingFiles.add(new SupportingFile("composer.mustache", getPackagePath(), "composer.json"));
116+
supportingFiles.add(new SupportingFile("index.mustache", getPackagePath(), "index.php"));
117+
supportingFiles.add(new SupportingFile(".htaccess", getPackagePath(), ".htaccess"));
118+
supportingFiles.add(new SupportingFile(".gitignore", getPackagePath(), ".gitignore"));
119+
supportingFiles.add(new SupportingFile("AbstractApiController.mustache", toSrcPath(invokerPackage, srcBasePath), "AbstractApiController.php"));
120+
supportingFiles.add(new SupportingFile("SlimRouter.mustache", toSrcPath(invokerPackage, srcBasePath), "SlimRouter.php"));
121+
}
122+
102123
@Override
103124
public Map<String, Object> postProcessOperationsWithModels(Map<String, Object> objs, List<Object> allModels) {
104125
Map<String, Object> operations = (Map<String, Object>) objs.get("operations");
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
/**
3+
* Abstract Api Controller
4+
*
5+
* PHP version 5
6+
*
7+
* @category Class
8+
* @package {{invokerPackage}}
9+
* @author OpenAPI Generator team
10+
* @link https://github.com/openapitools/openapi-generator
11+
*/
12+
13+
/**
14+
{{#appName}}
15+
* {{{appName}}}
16+
*
17+
{{/appName}}
18+
{{#appDescription}}
19+
* {{{appDescription}}}
20+
{{/appDescription}}
21+
{{#version}}
22+
* OpenAPI spec version: {{{version}}}
23+
{{/version}}
24+
{{#infoEmail}}
25+
* Contact: {{{infoEmail}}}
26+
{{/infoEmail}}
27+
* Generated by: https://github.com/openapitools/openapi-generator.git
28+
*/
29+
30+
/**
31+
* NOTE: This class is auto generated by the openapi generator program.
32+
* https://github.com/openapitools/openapi-generator
33+
* Do not edit the class manually.
34+
*/
35+
namespace {{invokerPackage}};
36+
37+
/**
38+
* ApiServer Class Doc Comment
39+
*
40+
* PHP version 5
41+
*
42+
* @category Class
43+
* @package {{invokerPackage}}
44+
* @author OpenAPI Generator team
45+
* @link https://github.com/openapitools/openapi-generator
46+
*/
47+
abstract class AbstractApiController {
48+
49+
/**
50+
* @var \Interop\Container\ContainerInterface Slim app container instance
51+
*/
52+
protected $container;
53+
54+
/**
55+
* Route Controller constructor receives container
56+
*
57+
* @param \Interop\Container\ContainerInterface $container Slim app container instance
58+
*/
59+
public function __construct($container) {
60+
$this->container = $container;
61+
}
62+
63+
}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
<?php
2+
/**
3+
* SlimRouter
4+
*
5+
* PHP version 5
6+
*
7+
* @category Class
8+
* @package {{invokerPackage}}
9+
* @author OpenAPI Generator team
10+
* @link https://github.com/openapitools/openapi-generator
11+
*/
12+
13+
/**{{#apiInfo}}{{#appName}}
14+
* {{{appName}}}
15+
*
16+
{{/appName}}
17+
{{#appDescription}}
18+
* {{{appDescription}}}
19+
{{/appDescription}}
20+
{{#version}}
21+
* OpenAPI spec version: {{{version}}}
22+
{{/version}}
23+
{{#infoEmail}}
24+
* Contact: {{{infoEmail}}}
25+
{{/infoEmail}}
26+
* Generated by: https://github.com/openapitools/openapi-generator.git
27+
*/
28+
29+
/**
30+
* NOTE: This class is auto generated by the openapi generator program.
31+
* https://github.com/openapitools/openapi-generator
32+
* Do not edit the class manually.
33+
*/
34+
namespace {{invokerPackage}};
35+
36+
{{#apis}}
37+
use {{apiPackage}}\{{classname}};
38+
{{/apis}}
39+
use Slim\App;
40+
use Psr\Container\ContainerInterface;
41+
use InvalidArgumentException;
42+
43+
/**
44+
* SlimRouter Class Doc Comment
45+
*
46+
* PHP version 5
47+
*
48+
* @category Class
49+
* @package {{apiPackage}}
50+
* @author OpenAPI Generator team
51+
* @link https://github.com/openapitools/openapi-generator
52+
*/
53+
class SlimRouter {
54+
55+
/**
56+
* @var $slimApp Slim\App instance
57+
*/
58+
private $slimApp;
59+
60+
/**
61+
* Class constructor
62+
*
63+
* @param ContainerInterface|array $container Either a ContainerInterface or an associative array of app settings
64+
* @throws InvalidArgumentException when no container is provided that implements ContainerInterface
65+
*/
66+
public function __construct($container = []) {
67+
$app = new App($container);
68+
69+
{{#apis}}
70+
{{#operations}}
71+
{{#operation}}
72+
$app->{{httpMethod}}('{{{basePathWithoutHost}}}{{{path}}}', {{classname}}::class . ':{{operationId}}');
73+
{{/operation}}
74+
{{/operations}}
75+
{{/apis}}
76+
77+
$this->slimApp = $app;
78+
}
79+
80+
/**
81+
* Returns Slim Framework instance
82+
* @return App
83+
*/
84+
public function getSlimApp() {
85+
return $this->slimApp;
86+
}
87+
}
88+
{{/apiInfo}}
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
<?php
2+
/**
3+
* {{classname}}
4+
*
5+
* PHP version 5
6+
*
7+
* @category Class
8+
* @package {{apiPackage}}
9+
* @author OpenAPI Generator team
10+
* @link https://github.com/openapitools/openapi-generator
11+
*/
12+
13+
/**
14+
{{#appName}}
15+
* {{{appName}}}
16+
*
17+
{{/appName}}
18+
{{#appDescription}}
19+
* {{{appDescription}}}
20+
{{/appDescription}}
21+
{{#version}}
22+
* OpenAPI spec version: {{{version}}}
23+
{{/version}}
24+
{{#infoEmail}}
25+
* Contact: {{{infoEmail}}}
26+
{{/infoEmail}}
27+
* Generated by: https://github.com/openapitools/openapi-generator.git
28+
*/
29+
30+
/**
31+
* NOTE: This class is auto generated by the openapi generator program.
32+
* https://github.com/openapitools/openapi-generator
33+
* Do not edit the class manually.
34+
*/
35+
namespace {{apiPackage}};
36+
37+
use {{invokerPackage}}\AbstractApiController;
38+
39+
/**
40+
* {{classname}} Class Doc Comment
41+
*
42+
* PHP version 5
43+
*
44+
* @category Class
45+
* @package {{apiPackage}}
46+
* @author OpenAPI Generator team
47+
* @link https://github.com/openapitools/openapi-generator
48+
*/
49+
class {{classname}} extends AbstractApiController {
50+
51+
{{#operations}}
52+
{{#operation}}
53+
/**
54+
* {{httpMethod}} {{operationId}}
55+
* Summary: {{summary}}
56+
* Notes: {{notes}}
57+
{{#hasProduces}}
58+
* Output-Formats: [{{#produces}}{{{mediaType}}}{{#hasMore}}, {{/hasMore}}{{/produces}}]
59+
{{/hasProduces}}
60+
*
61+
* @param \Psr\Http\Message\ServerRequestInterface $request Request
62+
* @param \Psr\Http\Message\ResponseInterface $response Response
63+
* @param array|null $args Path arguments
64+
*/
65+
public function {{operationId}}($request, $response, $args) {
66+
{{#hasHeaderParams}}
67+
$headers = $request->getHeaders();
68+
{{#headerParams}}
69+
${{paramName}} = $request->hasHeader('{{baseName}}') ? $headers['{{baseName}}'] : null;
70+
{{/headerParams}}
71+
{{/hasHeaderParams}}
72+
{{#hasPathParams}}
73+
{{#pathParams}}
74+
${{paramName}} = $args['{{baseName}}'];
75+
{{/pathParams}}
76+
{{/hasPathParams}}
77+
{{#hasQueryParams}}
78+
$queryParams = $request->getQueryParams();
79+
{{#queryParams}}
80+
${{paramName}} = $request->getQueryParam('{{baseName}}');
81+
{{/queryParams}}
82+
{{/hasQueryParams}}
83+
{{#hasFormParams}}
84+
{{#formParams}}
85+
{{^isFile}}
86+
${{paramName}} = $request->getParsedBodyParam('{{baseName}}');
87+
{{/isFile}}
88+
{{#isFile}}
89+
${{paramName}} = (key_exists('{{baseName}}', $request->getUploadedFiles())) ? $request->getUploadedFiles()['{{baseName}}'] : null;
90+
{{/isFile}}
91+
{{/formParams}}
92+
{{/hasFormParams}}
93+
{{#hasBodyParam}}
94+
$body = $request->getParsedBody();
95+
{{/hasBodyParam}}
96+
$response->write('How about implementing {{nickname}} as a {{httpMethod}} method ?');
97+
return $response;
98+
}
99+
{{#hasMore}}{{/hasMore}}
100+
{{/operation}}
101+
{{/operations}}
102+
}

modules/openapi-generator/src/main/resources/php-slim-server/composer.json

Lines changed: 0 additions & 6 deletions
This file was deleted.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"minimum-stability": "RC",
3+
"require": {
4+
"slim/slim": "3.*"
5+
},
6+
"autoload": {
7+
"psr-4": { "{{escapedInvokerPackage}}\\": "{{srcBasePath}}/" }
8+
}
9+
}

0 commit comments

Comments
 (0)