Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Slim] Improve codebase decouple #438

Merged
merged 2 commits into from
Jul 6, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,11 @@ public PhpSlimServerCodegen() {
variableNamingConvention = "camelCase";
artifactVersion = "1.0.0";
packagePath = ""; // empty packagePath (top folder)
invokerPackage = camelize("OpenAPIServer");
modelPackage = packagePath + "\\Models";
apiPackage = packagePath;
setInvokerPackage("OpenAPIServer");
apiPackage = invokerPackage + "\\" + apiDirName;
modelPackage = invokerPackage + "\\" + modelDirName;
outputFolder = "generated-code" + File.separator + "slim";

// no api files
apiTemplateFiles.clear();
// no test files
apiTestTemplateFiles.clear();
// no doc files
Expand All @@ -63,11 +61,9 @@ public PhpSlimServerCodegen() {

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

// additionalProperties.put(CodegenConstants.INVOKER_PACKAGE, invokerPackage);
additionalProperties.put(CodegenConstants.GROUP_ID, groupId);
additionalProperties.put(CodegenConstants.ARTIFACT_ID, artifactId);
// additionalProperties.put(CodegenConstants.ARTIFACT_VERSION, artifactVersion);


// override cliOptions from AbstractPhpCodegen
for (CliOption co : cliOptions) {
if (co.getOpt().equals(AbstractPhpCodegen.VARIABLE_NAMING_CONVENTION)) {
Expand All @@ -76,12 +72,6 @@ public PhpSlimServerCodegen() {
break;
}
}

supportingFiles.add(new SupportingFile("README.mustache", packagePath.replace('/', File.separatorChar), "README.md"));
supportingFiles.add(new SupportingFile("composer.json", packagePath.replace('/', File.separatorChar), "composer.json"));
supportingFiles.add(new SupportingFile("index.mustache", packagePath.replace('/', File.separatorChar), "index.php"));
supportingFiles.add(new SupportingFile(".htaccess", packagePath.replace('/', File.separatorChar), ".htaccess"));
supportingFiles.add(new SupportingFile(".gitignore", packagePath.replace('/', File.separatorChar), ".gitignore"));
}

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

@Override
public String apiFileFolder() {
if (apiPackage.matches("^" + invokerPackage + "\\\\*(.+)")) {
// need to strip out invokerPackage from path
return (outputFolder + File.separator + toPackagePath(apiPackage.replaceFirst("^" + invokerPackage + "\\\\*(.+)", "$1"), srcBasePath));
}
return (outputFolder + File.separator + toPackagePath(apiPackage, srcBasePath));
}

@Override
public String modelFileFolder() {
if (modelPackage.matches("^" + invokerPackage + "\\\\*(.+)")) {
// need to strip out invokerPackage from path
return (outputFolder + File.separator + toPackagePath(modelPackage.replaceFirst("^" + invokerPackage + "\\\\*(.+)", "$1"), srcBasePath));
}
return (outputFolder + File.separator + toPackagePath(modelPackage, srcBasePath));
}

@Override
public void processOpts() {
super.processOpts();

supportingFiles.add(new SupportingFile("README.mustache", getPackagePath(), "README.md"));
supportingFiles.add(new SupportingFile("composer.mustache", getPackagePath(), "composer.json"));
supportingFiles.add(new SupportingFile("index.mustache", getPackagePath(), "index.php"));
supportingFiles.add(new SupportingFile(".htaccess", getPackagePath(), ".htaccess"));
supportingFiles.add(new SupportingFile(".gitignore", getPackagePath(), ".gitignore"));
supportingFiles.add(new SupportingFile("AbstractApiController.mustache", toSrcPath(invokerPackage, srcBasePath), "AbstractApiController.php"));
supportingFiles.add(new SupportingFile("SlimRouter.mustache", toSrcPath(invokerPackage, srcBasePath), "SlimRouter.php"));
}

@Override
public Map<String, Object> postProcessOperationsWithModels(Map<String, Object> objs, List<Object> allModels) {
Map<String, Object> operations = (Map<String, Object>) objs.get("operations");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php
/**
* Abstract Api Controller
*
* PHP version 5
*
* @category Class
* @package {{invokerPackage}}
* @author OpenAPI Generator team
* @link https://github.com/openapitools/openapi-generator
*/

/**
{{#appName}}
* {{{appName}}}
*
{{/appName}}
{{#appDescription}}
* {{{appDescription}}}
{{/appDescription}}
{{#version}}
* OpenAPI spec version: {{{version}}}
{{/version}}
{{#infoEmail}}
* Contact: {{{infoEmail}}}
{{/infoEmail}}
* Generated by: https://github.com/openapitools/openapi-generator.git
*/

/**
* NOTE: This class is auto generated by the openapi generator program.
* https://github.com/openapitools/openapi-generator
* Do not edit the class manually.
*/
namespace {{invokerPackage}};

/**
* ApiServer Class Doc Comment
*
* PHP version 5
*
* @category Class
* @package {{invokerPackage}}
* @author OpenAPI Generator team
* @link https://github.com/openapitools/openapi-generator
*/
abstract class AbstractApiController {

/**
* @var \Interop\Container\ContainerInterface Slim app container instance
*/
protected $container;

/**
* Route Controller constructor receives container
*
* @param \Interop\Container\ContainerInterface $container Slim app container instance
*/
public function __construct($container) {
$this->container = $container;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<?php
/**
* SlimRouter
*
* PHP version 5
*
* @category Class
* @package {{invokerPackage}}
* @author OpenAPI Generator team
* @link https://github.com/openapitools/openapi-generator
*/

/**{{#apiInfo}}{{#appName}}
* {{{appName}}}
*
{{/appName}}
{{#appDescription}}
* {{{appDescription}}}
{{/appDescription}}
{{#version}}
* OpenAPI spec version: {{{version}}}
{{/version}}
{{#infoEmail}}
* Contact: {{{infoEmail}}}
{{/infoEmail}}
* Generated by: https://github.com/openapitools/openapi-generator.git
*/

/**
* NOTE: This class is auto generated by the openapi generator program.
* https://github.com/openapitools/openapi-generator
* Do not edit the class manually.
*/
namespace {{invokerPackage}};

{{#apis}}
use {{apiPackage}}\{{classname}};
{{/apis}}
use Slim\App;
use Psr\Container\ContainerInterface;
use InvalidArgumentException;

/**
* SlimRouter Class Doc Comment
*
* PHP version 5
*
* @category Class
* @package {{apiPackage}}
* @author OpenAPI Generator team
* @link https://github.com/openapitools/openapi-generator
*/
class SlimRouter {

/**
* @var $slimApp Slim\App instance
*/
private $slimApp;

/**
* Class constructor
*
* @param ContainerInterface|array $container Either a ContainerInterface or an associative array of app settings
* @throws InvalidArgumentException when no container is provided that implements ContainerInterface
*/
public function __construct($container = []) {
$app = new App($container);

{{#apis}}
{{#operations}}
{{#operation}}
$app->{{httpMethod}}('{{{basePathWithoutHost}}}{{{path}}}', {{classname}}::class . ':{{operationId}}');
{{/operation}}
{{/operations}}
{{/apis}}

$this->slimApp = $app;
}

/**
* Returns Slim Framework instance
* @return App
*/
public function getSlimApp() {
return $this->slimApp;
}
}
{{/apiInfo}}
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
<?php
/**
* {{classname}}
*
* PHP version 5
*
* @category Class
* @package {{apiPackage}}
* @author OpenAPI Generator team
* @link https://github.com/openapitools/openapi-generator
*/

/**
{{#appName}}
* {{{appName}}}
*
{{/appName}}
{{#appDescription}}
* {{{appDescription}}}
{{/appDescription}}
{{#version}}
* OpenAPI spec version: {{{version}}}
{{/version}}
{{#infoEmail}}
* Contact: {{{infoEmail}}}
{{/infoEmail}}
* Generated by: https://github.com/openapitools/openapi-generator.git
*/

/**
* NOTE: This class is auto generated by the openapi generator program.
* https://github.com/openapitools/openapi-generator
* Do not edit the class manually.
*/
namespace {{apiPackage}};

use {{invokerPackage}}\AbstractApiController;

/**
* {{classname}} Class Doc Comment
*
* PHP version 5
*
* @category Class
* @package {{apiPackage}}
* @author OpenAPI Generator team
* @link https://github.com/openapitools/openapi-generator
*/
class {{classname}} extends AbstractApiController {

{{#operations}}
{{#operation}}
/**
* {{httpMethod}} {{operationId}}
* Summary: {{summary}}
* Notes: {{notes}}
{{#hasProduces}}
* Output-Formats: [{{#produces}}{{{mediaType}}}{{#hasMore}}, {{/hasMore}}{{/produces}}]
{{/hasProduces}}
*
* @param \Psr\Http\Message\ServerRequestInterface $request Request
* @param \Psr\Http\Message\ResponseInterface $response Response
* @param array|null $args Path arguments
*/
public function {{operationId}}($request, $response, $args) {
{{#hasHeaderParams}}
$headers = $request->getHeaders();
{{#headerParams}}
${{paramName}} = $request->hasHeader('{{baseName}}') ? $headers['{{baseName}}'] : null;
{{/headerParams}}
{{/hasHeaderParams}}
{{#hasPathParams}}
{{#pathParams}}
${{paramName}} = $args['{{baseName}}'];
{{/pathParams}}
{{/hasPathParams}}
{{#hasQueryParams}}
$queryParams = $request->getQueryParams();
{{#queryParams}}
${{paramName}} = $request->getQueryParam('{{baseName}}');
{{/queryParams}}
{{/hasQueryParams}}
{{#hasFormParams}}
{{#formParams}}
{{^isFile}}
${{paramName}} = $request->getParsedBodyParam('{{baseName}}');
{{/isFile}}
{{#isFile}}
${{paramName}} = (key_exists('{{baseName}}', $request->getUploadedFiles())) ? $request->getUploadedFiles()['{{baseName}}'] : null;
{{/isFile}}
{{/formParams}}
{{/hasFormParams}}
{{#hasBodyParam}}
$body = $request->getParsedBody();
{{/hasBodyParam}}
$response->write('How about implementing {{nickname}} as a {{httpMethod}} method ?');
return $response;
}
{{#hasMore}}{{/hasMore}}
{{/operation}}
{{/operations}}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"minimum-stability": "RC",
"require": {
"slim/slim": "3.*"
},
"autoload": {
"psr-4": { "{{escapedInvokerPackage}}\\": "{{srcBasePath}}/" }
}
}
Loading