Skip to content

Commit 8e62ef7

Browse files
ksm2wing328
authored andcommitted
[PHP][Symfony] Add a Symfony server generator #3486 (#5973)
1 parent 2774d47 commit 8e62ef7

29 files changed

+2280
-0
lines changed

modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/AbstractPhpCodegen.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -660,4 +660,12 @@ public String escapeUnsafeCharacters(String input) {
660660
return input.replace("*/", "");
661661
}
662662

663+
protected String extractSimpleName(String phpClassName) {
664+
if (phpClassName == null) {
665+
return null;
666+
}
667+
668+
final int lastBackslashIndex = phpClassName.lastIndexOf('\\');
669+
return phpClassName.substring(lastBackslashIndex + 1);
670+
}
663671
}

modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/SymfonyServerCodegen.java

Lines changed: 450 additions & 0 deletions
Large diffs are not rendered by default.

modules/swagger-codegen/src/main/resources/META-INF/services/io.swagger.codegen.CodegenConfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ io.swagger.codegen.languages.NodeJSServerCodegen
4343
io.swagger.codegen.languages.ObjcClientCodegen
4444
io.swagger.codegen.languages.PerlClientCodegen
4545
io.swagger.codegen.languages.PhpClientCodegen
46+
io.swagger.codegen.languages.SymfonyServerCodegen
4647
io.swagger.codegen.languages.PowerShellClientCodegen
4748
io.swagger.codegen.languages.PistacheServerCodegen
4849
io.swagger.codegen.languages.PythonClientCodegen
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
return Symfony\CS\Config::create()
4+
->level(Symfony\CS\FixerInterface::PSR2_LEVEL)
5+
->setUsingCache(true)
6+
->fixers(
7+
[
8+
'ordered_use',
9+
'phpdoc_order',
10+
'short_array_syntax',
11+
'strict',
12+
'strict_param'
13+
]
14+
)
15+
->finder(
16+
Symfony\CS\Finder\DefaultFinder::create()
17+
->in(__DIR__)
18+
);
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
language: php
2+
sudo: false
3+
php:
4+
- 5.4
5+
- 5.5
6+
- 5.6
7+
- 7.0
8+
- hhvm
9+
before_install: "composer install"
10+
script: "vendor/bin/phpunit"
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
/**
3+
* {{bundleName}}ApiPass
4+
*
5+
* PHP version 5
6+
*
7+
* @category Class
8+
* @package {{invokerPackage}}\DependencyInjection\Compiler
9+
* @author Swagger Codegen team
10+
* @link https://github.com/swagger-api/swagger-codegen
11+
*/
12+
13+
{{>partial_header}}
14+
/**
15+
* NOTE: This class is auto generated by the swagger code generator program.
16+
* https://github.com/swagger-api/swagger-codegen
17+
* Do not edit the class manually.
18+
*/
19+
20+
namespace {{invokerPackage}}\DependencyInjection\Compiler;
21+
22+
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
23+
use Symfony\Component\DependencyInjection\ContainerBuilder;
24+
use Symfony\Component\DependencyInjection\Reference;
25+
26+
/**
27+
* {{bundleName}}ApiPass Class Doc Comment
28+
*
29+
* @category Class
30+
* @package {{invokerPackage}}\DependencyInjection\Compiler
31+
* @author Swagger Codegen team
32+
* @link https://github.com/swagger-api/swagger-codegen
33+
*/
34+
class {{bundleName}}ApiPass implements CompilerPassInterface
35+
{
36+
37+
/**
38+
* You can modify the container here before it is dumped to PHP code.
39+
*
40+
* @param ContainerBuilder $container
41+
*/
42+
public function process(ContainerBuilder $container) {
43+
// always first check if the primary service is defined
44+
if (!$container->has('{{bundleAlias}}.api.api_server')) {
45+
return;
46+
}
47+
48+
$definition = $container->findDefinition('{{bundleAlias}}.api.api_server');
49+
50+
// find all service IDs with the {{bundleAlias}}.api tag
51+
$taggedServices = $container->findTaggedServiceIds('{{bundleAlias}}.api');
52+
53+
foreach ($taggedServices as $id => $tags) {
54+
foreach ($tags as $tag) {
55+
// add the transport service to the ChainTransport service
56+
$definition->addMethodCall('addApiHandler', [$tag['api'], new Reference($id)]);
57+
}
58+
}
59+
}
60+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?php
2+
/**
3+
* ApiServer
4+
*
5+
* PHP version 5
6+
*
7+
* @category Class
8+
* @package {{apiPackage}}
9+
* @author Swagger Codegen team
10+
* @link https://github.com/swagger-api/swagger-codegen
11+
*/
12+
13+
{{>partial_header}}
14+
/**
15+
* NOTE: This class is auto generated by the swagger code generator program.
16+
* https://github.com/swagger-api/swagger-codegen
17+
* Do not edit the class manually.
18+
*/
19+
20+
namespace {{apiPackage}};
21+
22+
/**
23+
* ApiServer Class Doc Comment
24+
*
25+
* PHP version 5
26+
*
27+
* @category Class
28+
* @package {{apiPackage}}
29+
* @author Swagger Codegen team
30+
* @link https://github.com/swagger-api/swagger-codegen
31+
*/
32+
class ApiServer
33+
{
34+
35+
/**
36+
* @var array
37+
*/
38+
private $apis = array();
39+
40+
/**
41+
* Adds an API handler to the server.
42+
*
43+
* @param string $api An API name of the handle
44+
* @param mixed $handler A handler to set for the given API
45+
*/
46+
public function addApiHandler($api, $handler)
47+
{
48+
if (isset($this->apis[$api])) {
49+
throw new \InvalidArgumentException('API has already a handler: '.$api);
50+
}
51+
52+
$this->apis[$api] = $handler;
53+
}
54+
55+
/**
56+
* Returns an API handler.
57+
*
58+
* @param string $api An API name of the handle
59+
* @return mixed Returns a handler
60+
* @throws \InvalidArgumentException When no such handler exists
61+
*/
62+
public function getApiHandler($api)
63+
{
64+
if (!isset($this->apis[$api])) {
65+
throw new \InvalidArgumentException('No handler for '.$api.' implemented.');
66+
}
67+
68+
return $this->apis[$api];
69+
}
70+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
/**
3+
* {{bundleClassName}}
4+
*
5+
* PHP version 5
6+
*
7+
* @category Class
8+
* @package {{invokerPackage}}
9+
* @author Swagger Codegen team
10+
* @link https://github.com/swagger-api/swagger-codegen
11+
*/
12+
13+
{{>partial_header}}
14+
/**
15+
* NOTE: This class is auto generated by the swagger code generator program.
16+
* https://github.com/swagger-api/swagger-codegen
17+
* Do not edit the class manually.
18+
*/
19+
20+
namespace {{invokerPackage}};
21+
22+
use Symfony\Component\HttpKernel\Bundle\Bundle;
23+
use Symfony\Component\DependencyInjection\ContainerBuilder;
24+
use {{invokerPackage}}\DependencyInjection\Compiler\{{bundleName}}ApiPass;
25+
26+
/**
27+
* {{bundleClassName}} Class Doc Comment
28+
*
29+
* @category Class
30+
* @package {{invokerPackage}}
31+
* @author Swagger Codegen team
32+
* @link https://github.com/swagger-api/swagger-codegen
33+
*/
34+
class {{bundleClassName}} extends Bundle
35+
{
36+
public function build(ContainerBuilder $container)
37+
{
38+
$container->addCompilerPass(new {{bundleName}}ApiPass());
39+
}
40+
}
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
<?php
2+
/**
3+
* Controller
4+
*
5+
* PHP version 5
6+
*
7+
* @category Class
8+
* @package {{controllerPackage}}
9+
* @author Swagger Codegen team
10+
* @link https://github.com/swagger-api/swagger-codegen
11+
*/
12+
13+
{{>partial_header}}
14+
/**
15+
* NOTE: This class is auto generated by the swagger code generator program.
16+
* https://github.com/swagger-api/swagger-codegen
17+
* Do not edit the class manually.
18+
*/
19+
20+
namespace {{controllerPackage}};
21+
22+
use Symfony\Bundle\FrameworkBundle\Controller\Controller as BaseController;
23+
use Symfony\Component\HttpFoundation\Response;
24+
use Symfony\Component\HttpKernel\Exception\HttpException;
25+
26+
/**
27+
* Controller Class Doc Comment
28+
*
29+
* @category Class
30+
* @package {{controllerPackage}}
31+
* @author Swagger Codegen team
32+
* @link https://github.com/swagger-api/swagger-codegen
33+
*/
34+
class Controller extends BaseController
35+
{
36+
37+
/**
38+
* This will return a response with code 400. Usage example:
39+
*
40+
* return $this->createBadRequestResponse('Unable to access this page!');
41+
*
42+
* @param string $message A message
43+
*
44+
* @return Response
45+
*/
46+
public function createBadRequestResponse($message = 'Bad Request.')
47+
{
48+
return new Response($message, 400);
49+
}
50+
51+
/**
52+
* This will return an error response. Usage example:
53+
*
54+
* return $this->createErrorResponse(new UnauthorizedHttpException());
55+
*
56+
* @param HttpException $exception An HTTP exception
57+
*
58+
* @return Response
59+
*/
60+
public function createErrorResponse(HttpException $exception)
61+
{
62+
$statusCode = $exception->getStatusCode();
63+
$headers = array_merge($exception->getHeaders(), ['Content-Type' => 'application/json']);
64+
65+
$json = $this->exceptionToArray($exception);
66+
$json["statusCode"] = $statusCode;
67+
68+
return new Response(json_encode($json, 15, 512), $statusCode, $headers);
69+
}
70+
71+
/**
72+
* Serializes data to a given type format.
73+
*
74+
* @param mixed $data The data to serialize.
75+
* @param string $class The source data class.
76+
* @param string $format The target serialization format.
77+
* @return string A serialized data string.
78+
*/
79+
public function serialize($data, $format)
80+
{
81+
return $this->get('{{bundleAlias}}.model.model_serializer')->serialize($data, $format);
82+
}
83+
84+
/**
85+
* Deserializes data from a given type format.
86+
*
87+
* @param string $data The data to deserialize.
88+
* @param string $class The target data class.
89+
* @param string $format The source serialization format.
90+
* @return mixed A deserialized data.
91+
*/
92+
public function deserialize($data, $class, $format)
93+
{
94+
return $this->get('{{bundleAlias}}.model.model_serializer')->deserialize($data, $class, $format);
95+
}
96+
97+
/**
98+
* Converts an exception to a serializable array.
99+
*
100+
* @param \Exception|null $exception
101+
*
102+
* @return array
103+
*/
104+
private function exceptionToArray(\Exception $exception = null)
105+
{
106+
if (null === $exception) {
107+
return null;
108+
}
109+
110+
return [
111+
"message" => $exception->getMessage(),
112+
"type" => get_class($exception),
113+
"previous" => $this->exceptionToArray($exception->getPrevious()),
114+
];
115+
}
116+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
/**
3+
* {{bundleExtensionName}}
4+
*
5+
* PHP version 5
6+
*
7+
* @category Class
8+
* @package {{invokerPackage}}\DependencyInjection
9+
* @author Swagger Codegen team
10+
* @link https://github.com/swagger-api/swagger-codegen
11+
*/
12+
13+
{{>partial_header}}
14+
/**
15+
* NOTE: This class is auto generated by the swagger code generator program.
16+
* https://github.com/swagger-api/swagger-codegen
17+
* Do not edit the class manually.
18+
*/
19+
20+
namespace {{invokerPackage}}\DependencyInjection;
21+
22+
use Symfony\Component\Config\FileLocator;
23+
use Symfony\Component\DependencyInjection\ContainerBuilder;
24+
use Symfony\Component\DependencyInjection\Extension\Extension;
25+
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
26+
27+
/**
28+
* {{bundleExtensionName}} Class Doc Comment
29+
*
30+
* @category Class
31+
* @package {{invokerPackage}}\DependencyInjection
32+
* @author Swagger Codegen team
33+
* @link https://github.com/swagger-api/swagger-codegen
34+
*/
35+
class {{bundleExtensionName}} extends Extension
36+
{
37+
public function load(array $configs, ContainerBuilder $container)
38+
{
39+
$loader = new YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
40+
$loader->load('services.yml');
41+
}
42+
43+
public function getAlias()
44+
{
45+
return '{{bundleAlias}}';
46+
}
47+
}

0 commit comments

Comments
 (0)