forked from loafle/openapi-generator-original
* Removed commented code * Input validation is now supported as strict JSON validation * [PHP][Symfony] Improve the implementation Closes #6614 * Generated code is tested to assure it compiles and updated README to dynamically load dependencies via composer * Updated shell script because shippable tests were failing
204 lines
6.6 KiB
Plaintext
204 lines
6.6 KiB
Plaintext
<?php
|
|
|
|
{{#operations}}/**
|
|
* {{controllerName}}
|
|
* PHP version 5
|
|
*
|
|
* @category Class
|
|
* @package {{controllerPackage}}
|
|
* @author Swagger Codegen team
|
|
* @link https://github.com/swagger-api/swagger-codegen
|
|
*/
|
|
|
|
{{>partial_header}}
|
|
/**
|
|
* NOTE: This class is auto generated by the swagger code generator program.
|
|
* https://github.com/swagger-api/swagger-codegen
|
|
* Do not edit the class manually.
|
|
*/
|
|
|
|
namespace {{controllerPackage}};
|
|
|
|
use \Exception;
|
|
use Symfony\Component\HttpFoundation\Request;
|
|
use Symfony\Component\HttpFoundation\Response;
|
|
use Symfony\Component\HttpKernel\Exception\HttpException;
|
|
use Symfony\Component\Validator\Constraints as Assert;
|
|
use {{apiPackage}}\{{classname}};
|
|
{{#imports}}use {{import}};
|
|
{{/imports}}
|
|
|
|
/**
|
|
* {{controllerName}} Class Doc Comment
|
|
*
|
|
* @category Class
|
|
* @package {{controllerPackage}}
|
|
* @author Swagger Codegen team
|
|
* @link https://github.com/swagger-api/swagger-codegen
|
|
*/
|
|
class {{controllerName}} extends Controller
|
|
{
|
|
{{#operation}}
|
|
|
|
/**
|
|
* Operation {{{operationId}}}
|
|
{{#summary}}
|
|
*
|
|
* {{{summary}}}
|
|
{{/summary}}
|
|
*
|
|
{{#description}}
|
|
* {{.}}
|
|
*
|
|
{{/description}}
|
|
* @param Request $request The Symfony request to handle.
|
|
* @return Response The Symfony response.
|
|
*/
|
|
public function {{operationId}}Action(Request $request{{#hasPathParams}}{{#pathParams}}, ${{paramName}}{{/pathParams}}{{/hasPathParams}})
|
|
{
|
|
{{#bodyParams}}
|
|
// Make sure that the client is providing something that we can consume
|
|
$consumes = [{{#consumes}}'{{mediaType}}'{{#hasMore}}, {{/hasMore}}{{/consumes}}];
|
|
$inputFormat = $request->headers->has('Content-Type')?$request->headers->get('Content-Type'):$consumes[0];
|
|
if (!in_array($inputFormat, $consumes)) {
|
|
// We can't consume the content that the client is sending us
|
|
return new Response('', 415);
|
|
}
|
|
|
|
{{/bodyParams}}
|
|
// Figure out what data format to return to the client
|
|
$produces = [{{#produces}}'{{mediaType}}'{{#hasMore}}, {{/hasMore}}{{/produces}}];
|
|
// Figure out what the client accepts
|
|
$clientAccepts = $request->headers->has('Accept')?$request->headers->get('Accept'):'*/*';
|
|
$responseFormat = $this->getOutputFormat($clientAccepts, $produces);
|
|
if ($responseFormat === null) {
|
|
return new Response('', 406);
|
|
}
|
|
|
|
// Handle authentication
|
|
{{#authMethods}}
|
|
// Authentication '{{name}}' required
|
|
{{#isApiKey}}
|
|
{{#isKeyInHeader}}
|
|
// Set key with prefix in header
|
|
$security{{name}} = $request->headers->get('{{keyParamName}}');
|
|
{{/isKeyInHeader}}
|
|
{{#isKeyInQuery}}
|
|
// Set key with prefix in query string
|
|
$security{{name}} = $request->query->get('{{keyParamName}}');
|
|
{{/isKeyInQuery}}
|
|
{{/isApiKey}}
|
|
{{#isBasic}}
|
|
// HTTP basic authentication required
|
|
$security{{name}} = $request->headers->get('authorization');
|
|
{{/isBasic}}
|
|
{{#isOAuth}}
|
|
// Oauth required
|
|
$security{{name}} = $request->headers->get('authorization');
|
|
{{/isOAuth}}
|
|
{{/authMethods}}
|
|
|
|
// Read out all input parameter values into variables
|
|
{{#allParams}}
|
|
{{#queryParams}}
|
|
${{paramName}} = $request->query->get('{{paramName}}');
|
|
{{/queryParams}}
|
|
{{#headerParams}}
|
|
${{paramName}} = $request->headers->get('{{paramName}}');
|
|
{{/headerParams}}
|
|
{{#formParams}}
|
|
{{#isFile}}
|
|
${{paramName}} = $request->files->get('{{paramName}}');
|
|
{{/isFile}}
|
|
{{^isFile}}
|
|
${{paramName}} = $request->request->get('{{paramName}}');
|
|
{{/isFile}}
|
|
{{/formParams}}
|
|
{{#bodyParams}}
|
|
${{paramName}} = $request->getContent();
|
|
{{/bodyParams}}
|
|
|
|
// Use the default value if no value was provided
|
|
{{^required}}
|
|
{{#isContainer}}
|
|
{{#items}}
|
|
{{#defaultValue}}
|
|
${{paramName}} = ${{paramName}}?:[{{{defaultValue}}}];
|
|
{{/defaultValue}}
|
|
{{/items}}
|
|
{{/isContainer}}
|
|
{{^isContainer}}
|
|
{{#defaultValue}}
|
|
${{paramName}} = ${{paramName}}?:{{{defaultValue}}};
|
|
{{/defaultValue}}
|
|
{{/isContainer}}
|
|
{{/required}}
|
|
|
|
// Deserialize the input values that needs it
|
|
{{^isFile}}
|
|
{{#bodyParams}}
|
|
|
|
${{paramName}} = $this->deserialize(${{paramName}}, '{{{dataType}}}', $inputFormat);
|
|
{{/bodyParams}}
|
|
{{^bodyParams}}
|
|
${{paramName}} = $this->deserialize(${{paramName}}, '{{#isContainer}}array<{{#collectionFormat}}{{collectionFormat}}{{/collectionFormat}}{{^collectionFormat}}csv{{/collectionFormat}}>{{/isContainer}}{{^isContainer}}{{dataType}}{{/isContainer}}', 'string');
|
|
{{/bodyParams}}
|
|
{{/isFile}}
|
|
{{/allParams}}
|
|
|
|
// Validate the input values
|
|
{{>api_input_validation}}
|
|
|
|
try {
|
|
$handler = $this->getApiHandler();
|
|
|
|
{{#authMethods}}
|
|
// Set authentication method '{{name}}'
|
|
$handler->set{{name}}($security{{name}});
|
|
{{/authMethods}}
|
|
|
|
// Make the call to the business logic
|
|
$responseCode = {{#returnType}}200{{/returnType}}{{^returnType}}204{{/returnType}};
|
|
$responseHeaders = [];
|
|
$result = $handler->{{operationId}}({{#allParams}}${{paramName}}, {{/allParams}}$responseCode, $responseHeaders);
|
|
|
|
// Find default response message
|
|
$message = '{{#responses}}{{#isDefault}}{{message}}{{/isDefault}}{{/responses}}';
|
|
|
|
// Find a more specific message, if available
|
|
switch ($responseCode) {
|
|
{{#responses}}
|
|
case {{code}}:
|
|
$message = '{{message}}';
|
|
break;
|
|
{{/responses}}
|
|
}
|
|
|
|
return new Response(
|
|
$result?$this->serialize($result, $responseFormat):'',
|
|
$responseCode,
|
|
array_merge(
|
|
$responseHeaders,
|
|
[
|
|
'Content-Type' => $responseFormat,
|
|
'X-Swagger-Message' => $message
|
|
]
|
|
)
|
|
);
|
|
} catch (Exception $fallthrough) {
|
|
return $this->createErrorResponse(new HttpException(500, 'An unsuspected error occurred.', $fallthrough));
|
|
}
|
|
}
|
|
{{/operation}}
|
|
|
|
/**
|
|
* Returns the handler for this API controller.
|
|
* @return {{classname}}
|
|
*/
|
|
public function getApiHandler()
|
|
{
|
|
return $this->apiServer->getApiHandler('{{pathPrefix}}');
|
|
}
|
|
}
|
|
{{/operations}}
|