Files
openapi-generator/modules/swagger-codegen/src/main/resources/php-symfony/api_controller.mustache
2017-07-05 16:43:56 +08:00

177 lines
6.9 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 {{apiPackage}}\{{classname}};
{{#imports}}use {{this}};
{{/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)
{
{{#queryParams}}
// Handle query params
${{paramName}} = $request->query->get('{{paramName}}');
{{/queryParams}}
{{#headerParams}}
// Handle header params
${{paramName}} = $request->headers->get('{{paramName}}');
{{/headerParams}}
{{#pathParams}}
// Handle path params
${{paramName}} = $request->attributes->get('{{paramName}}');
{{/pathParams}}
{{#formParams}}
{{#isFile}}
// Handle file params
${{paramName}} = $request->files->get('{{paramName}}');
{{/isFile}}
{{^isFile}}
// Handle form params
${{paramName}} = $request->request->get('{{paramName}}');
{{/isFile}}
{{/formParams}}
{{#bodyParams}}
// Handle body params
${{paramName}} = $this->deserialize($request->getContent(), '{{{dataType}}}', 'json');
{{/bodyParams}}
// Parse incoming parameters
{{#allParams}}
{{#required}}
// Verify the required parameter '{{paramName}}' is set
if (${{paramName}} === null) {
return $this->createBadRequestResponse('Missing the required parameter ${{paramName}} when calling {{operationId}}');
}
{{/required}}
{{#hasValidation}}
{{#maxLength}}
if ({{^required}}!is_null(${{paramName}}) && {{/required}}(strlen(${{paramName}}) > {{maxLength}})) {
return $this->createBadRequestResponse('Invalid length for "${{paramName}}" when calling {{classname}}.{{operationId}}, must be smaller than or equal to {{maxLength}}.');
}
{{/maxLength}}
{{#minLength}}
if ({{^required}}!is_null(${{paramName}}) && {{/required}}(strlen(${{paramName}}) < {{minLength}})) {
return $this->createBadRequestResponse('Invalid length for "${{paramName}}" when calling {{classname}}.{{operationId}}, must be bigger than or equal to {{minLength}}.');
}
{{/minLength}}
{{#maximum}}
if ({{^required}}!is_null(${{paramName}}) && {{/required}}(${{paramName}} >{{#exclusiveMaximum}}={{/exclusiveMaximum}} {{maximum}})) {
return $this->createBadRequestResponse('Invalid value for "${{paramName}}" when calling {{classname}}.{{operationId}}, must be smaller than {{^exclusiveMaximum}}or equal to {{/exclusiveMaximum}}{{maximum}}.');
}
{{/maximum}}
{{#minimum}}
if ({{^required}}!is_null(${{paramName}}) && {{/required}}(${{paramName}} <{{#exclusiveMinimum}}={{/exclusiveMinimum}} {{minimum}})) {
return $this->createBadRequestResponse('Invalid value for "${{paramName}}" when calling {{classname}}.{{operationId}}, must be bigger than {{^exclusiveMinimum}}or equal to {{/exclusiveMinimum}}{{minimum}}.');
}
{{/minimum}}
{{#pattern}}
if ({{^required}}!is_null(${{paramName}}) && {{/required}}!preg_match("{{{pattern}}}", ${{paramName}})) {
return $this->createBadRequestResponse("Invalid value for \"{{paramName}}\" when calling {{classname}}.{{operationId}}, must conform to the pattern {{{pattern}}}.");
}
{{/pattern}}
{{#maxItems}}
if ({{^required}}!is_null(${{paramName}}) && {{/required}}(count(${{paramName}}) > {{maxItems}})) {
return $this->createBadRequestResponse('Invalid value for "${{paramName}}" when calling {{classname}}.{{operationId}}, number of items must be less than or equal to {{maxItems}}.');
}
{{/maxItems}}
{{#minItems}}
if ({{^required}}!is_null(${{paramName}}) && {{/required}}(count(${{paramName}}) < {{minItems}})) {
return $this->createBadRequestResponse('Invalid value for "${{paramName}}" when calling {{classname}}.{{operationId}}, number of items must be greater than or equal to {{minItems}}.');
}
{{/minItems}}
{{/hasValidation}}
{{/allParams}}
// Call the API interface
try {
{{#returnType}}
// Expecting a return value (exception otherwise)
$result = $this->getApiHandler()->{{operationId}}({{#allParams}}${{paramName}}{{#hasMore}}, {{/hasMore}}{{/allParams}});
{{#responses}}
{{^vendorExtensions.x-symfonyExceptionSimple}}
// Handle {{code}} response: {{message}}
$content = $this->serialize($result, 'json');
return new Response($content, {{code}}, [
'Content-Type' => 'application/json',
'X-Swagger-Message' => '{{message}}',
]);
{{/vendorExtensions.x-symfonyExceptionSimple}}
{{/responses}}
{{/returnType}}
{{^returnType}}
// No return type expected; return empty response
$this->getApiHandler()->{{operationId}}({{#allParams}}${{paramName}}{{#hasMore}}, {{/hasMore}}{{/allParams}});
return new Response('', 204);
{{/returnType}}
{{#responses}}
{{#vendorExtensions.x-symfonyExceptionSimple}}
} catch ({{vendorExtensions.x-symfonyExceptionSimple}} $exception) {
// {{message}}
return $this->createErrorResponse($exception);
{{/vendorExtensions.x-symfonyExceptionSimple}}
{{/responses}}
} 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->get('{{bundleAlias}}.api.api_server')->getApiHandler('{{pathPrefix}}');
}
}
{{/operations}}