forked from swagger-api/swagger-codegen
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[PHP][Symfony] Add a Symfony server generator swagger-api#3486
- Loading branch information
Showing
29 changed files
with
2,280 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
450 changes: 450 additions & 0 deletions
450
modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/SymfonyServerCodegen.java
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
modules/swagger-codegen/src/main/resources/php-symfony/.php_cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
return Symfony\CS\Config::create() | ||
->level(Symfony\CS\FixerInterface::PSR2_LEVEL) | ||
->setUsingCache(true) | ||
->fixers( | ||
[ | ||
'ordered_use', | ||
'phpdoc_order', | ||
'short_array_syntax', | ||
'strict', | ||
'strict_param' | ||
] | ||
) | ||
->finder( | ||
Symfony\CS\Finder\DefaultFinder::create() | ||
->in(__DIR__) | ||
); |
10 changes: 10 additions & 0 deletions
10
modules/swagger-codegen/src/main/resources/php-symfony/.travis.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
language: php | ||
sudo: false | ||
php: | ||
- 5.4 | ||
- 5.5 | ||
- 5.6 | ||
- 7.0 | ||
- hhvm | ||
before_install: "composer install" | ||
script: "vendor/bin/phpunit" |
60 changes: 60 additions & 0 deletions
60
modules/swagger-codegen/src/main/resources/php-symfony/ApiPass.mustache
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
<?php | ||
/** | ||
* {{bundleName}}ApiPass | ||
* | ||
* PHP version 5 | ||
* | ||
* @category Class | ||
* @package {{invokerPackage}}\DependencyInjection\Compiler | ||
* @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 {{invokerPackage}}\DependencyInjection\Compiler; | ||
|
||
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
use Symfony\Component\DependencyInjection\Reference; | ||
|
||
/** | ||
* {{bundleName}}ApiPass Class Doc Comment | ||
* | ||
* @category Class | ||
* @package {{invokerPackage}}\DependencyInjection\Compiler | ||
* @author Swagger Codegen team | ||
* @link https://github.com/swagger-api/swagger-codegen | ||
*/ | ||
class {{bundleName}}ApiPass implements CompilerPassInterface | ||
{ | ||
/** | ||
* You can modify the container here before it is dumped to PHP code. | ||
* | ||
* @param ContainerBuilder $container | ||
*/ | ||
public function process(ContainerBuilder $container) { | ||
// always first check if the primary service is defined | ||
if (!$container->has('{{bundleAlias}}.api.api_server')) { | ||
return; | ||
} | ||
|
||
$definition = $container->findDefinition('{{bundleAlias}}.api.api_server'); | ||
|
||
// find all service IDs with the {{bundleAlias}}.api tag | ||
$taggedServices = $container->findTaggedServiceIds('{{bundleAlias}}.api'); | ||
|
||
foreach ($taggedServices as $id => $tags) { | ||
foreach ($tags as $tag) { | ||
// add the transport service to the ChainTransport service | ||
$definition->addMethodCall('addApiHandler', [$tag['api'], new Reference($id)]); | ||
} | ||
} | ||
} | ||
} |
70 changes: 70 additions & 0 deletions
70
modules/swagger-codegen/src/main/resources/php-symfony/ApiServer.mustache
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
<?php | ||
/** | ||
* ApiServer | ||
* | ||
* PHP version 5 | ||
* | ||
* @category Class | ||
* @package {{apiPackage}} | ||
* @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 {{apiPackage}}; | ||
|
||
/** | ||
* ApiServer Class Doc Comment | ||
* | ||
* PHP version 5 | ||
* | ||
* @category Class | ||
* @package {{apiPackage}} | ||
* @author Swagger Codegen team | ||
* @link https://github.com/swagger-api/swagger-codegen | ||
*/ | ||
class ApiServer | ||
{ | ||
/** | ||
* @var array | ||
*/ | ||
private $apis = array(); | ||
/** | ||
* Adds an API handler to the server. | ||
* | ||
* @param string $api An API name of the handle | ||
* @param mixed $handler A handler to set for the given API | ||
*/ | ||
public function addApiHandler($api, $handler) | ||
{ | ||
if (isset($this->apis[$api])) { | ||
throw new \InvalidArgumentException('API has already a handler: '.$api); | ||
} | ||
|
||
$this->apis[$api] = $handler; | ||
} | ||
|
||
/** | ||
* Returns an API handler. | ||
* | ||
* @param string $api An API name of the handle | ||
* @return mixed Returns a handler | ||
* @throws \InvalidArgumentException When no such handler exists | ||
*/ | ||
public function getApiHandler($api) | ||
{ | ||
if (!isset($this->apis[$api])) { | ||
throw new \InvalidArgumentException('No handler for '.$api.' implemented.'); | ||
} | ||
|
||
return $this->apis[$api]; | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
modules/swagger-codegen/src/main/resources/php-symfony/Bundle.mustache
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?php | ||
/** | ||
* {{bundleClassName}} | ||
* | ||
* PHP version 5 | ||
* | ||
* @category Class | ||
* @package {{invokerPackage}} | ||
* @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 {{invokerPackage}}; | ||
|
||
use Symfony\Component\HttpKernel\Bundle\Bundle; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
use {{invokerPackage}}\DependencyInjection\Compiler\{{bundleName}}ApiPass; | ||
|
||
/** | ||
* {{bundleClassName}} Class Doc Comment | ||
* | ||
* @category Class | ||
* @package {{invokerPackage}} | ||
* @author Swagger Codegen team | ||
* @link https://github.com/swagger-api/swagger-codegen | ||
*/ | ||
class {{bundleClassName}} extends Bundle | ||
{ | ||
public function build(ContainerBuilder $container) | ||
{ | ||
$container->addCompilerPass(new {{bundleName}}ApiPass()); | ||
} | ||
} |
116 changes: 116 additions & 0 deletions
116
modules/swagger-codegen/src/main/resources/php-symfony/Controller.mustache
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
<?php | ||
/** | ||
* Controller | ||
* | ||
* 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 Symfony\Bundle\FrameworkBundle\Controller\Controller as BaseController; | ||
use Symfony\Component\HttpFoundation\Response; | ||
use Symfony\Component\HttpKernel\Exception\HttpException; | ||
|
||
/** | ||
* Controller Class Doc Comment | ||
* | ||
* @category Class | ||
* @package {{controllerPackage}} | ||
* @author Swagger Codegen team | ||
* @link https://github.com/swagger-api/swagger-codegen | ||
*/ | ||
class Controller extends BaseController | ||
{ | ||
/** | ||
* This will return a response with code 400. Usage example: | ||
* | ||
* return $this->createBadRequestResponse('Unable to access this page!'); | ||
* | ||
* @param string $message A message | ||
* | ||
* @return Response | ||
*/ | ||
public function createBadRequestResponse($message = 'Bad Request.') | ||
{ | ||
return new Response($message, 400); | ||
} | ||
|
||
/** | ||
* This will return an error response. Usage example: | ||
* | ||
* return $this->createErrorResponse(new UnauthorizedHttpException()); | ||
* | ||
* @param HttpException $exception An HTTP exception | ||
* | ||
* @return Response | ||
*/ | ||
public function createErrorResponse(HttpException $exception) | ||
{ | ||
$statusCode = $exception->getStatusCode(); | ||
$headers = array_merge($exception->getHeaders(), ['Content-Type' => 'application/json']); | ||
$json = $this->exceptionToArray($exception); | ||
$json["statusCode"] = $statusCode; | ||
return new Response(json_encode($json, 15, 512), $statusCode, $headers); | ||
} | ||
|
||
/** | ||
* Serializes data to a given type format. | ||
* | ||
* @param mixed $data The data to serialize. | ||
* @param string $class The source data class. | ||
* @param string $format The target serialization format. | ||
* @return string A serialized data string. | ||
*/ | ||
public function serialize($data, $format) | ||
{ | ||
return $this->get('{{bundleAlias}}.model.model_serializer')->serialize($data, $format); | ||
} | ||
|
||
/** | ||
* Deserializes data from a given type format. | ||
* | ||
* @param string $data The data to deserialize. | ||
* @param string $class The target data class. | ||
* @param string $format The source serialization format. | ||
* @return mixed A deserialized data. | ||
*/ | ||
public function deserialize($data, $class, $format) | ||
{ | ||
return $this->get('{{bundleAlias}}.model.model_serializer')->deserialize($data, $class, $format); | ||
} | ||
|
||
/** | ||
* Converts an exception to a serializable array. | ||
* | ||
* @param \Exception|null $exception | ||
* | ||
* @return array | ||
*/ | ||
private function exceptionToArray(\Exception $exception = null) | ||
{ | ||
if (null === $exception) { | ||
return null; | ||
} | ||
|
||
return [ | ||
"message" => $exception->getMessage(), | ||
"type" => get_class($exception), | ||
"previous" => $this->exceptionToArray($exception->getPrevious()), | ||
]; | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
modules/swagger-codegen/src/main/resources/php-symfony/Extension.mustache
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?php | ||
/** | ||
* {{bundleExtensionName}} | ||
* | ||
* PHP version 5 | ||
* | ||
* @category Class | ||
* @package {{invokerPackage}}\DependencyInjection | ||
* @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 {{invokerPackage}}\DependencyInjection; | ||
|
||
use Symfony\Component\Config\FileLocator; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
use Symfony\Component\DependencyInjection\Extension\Extension; | ||
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader; | ||
|
||
/** | ||
* {{bundleExtensionName}} Class Doc Comment | ||
* | ||
* @category Class | ||
* @package {{invokerPackage}}\DependencyInjection | ||
* @author Swagger Codegen team | ||
* @link https://github.com/swagger-api/swagger-codegen | ||
*/ | ||
class {{bundleExtensionName}} extends Extension | ||
{ | ||
public function load(array $configs, ContainerBuilder $container) | ||
{ | ||
$loader = new YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); | ||
$loader->load('services.yml'); | ||
} | ||
|
||
public function getAlias() | ||
{ | ||
return '{{bundleAlias}}'; | ||
} | ||
} |
Oops, something went wrong.