Skip to content

Commit

Permalink
[PHP][Symfony] Add a Symfony server generator swagger-api#3486
Browse files Browse the repository at this point in the history
  • Loading branch information
ksm2 committed Jul 4, 2017
1 parent c2b5756 commit ebd77fe
Show file tree
Hide file tree
Showing 29 changed files with 2,280 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -660,4 +660,12 @@ public String escapeUnsafeCharacters(String input) {
return input.replace("*/", "");
}

protected String extractSimpleName(String phpClassName) {
if (phpClassName == null) {
return null;
}

final int lastBackslashIndex = phpClassName.lastIndexOf('\\');
return phpClassName.substring(lastBackslashIndex + 1);
}
}

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ io.swagger.codegen.languages.NodeJSServerCodegen
io.swagger.codegen.languages.ObjcClientCodegen
io.swagger.codegen.languages.PerlClientCodegen
io.swagger.codegen.languages.PhpClientCodegen
io.swagger.codegen.languages.SymfonyServerCodegen
io.swagger.codegen.languages.PowerShellClientCodegen
io.swagger.codegen.languages.PistacheServerCodegen
io.swagger.codegen.languages.PythonClientCodegen
Expand Down
18 changes: 18 additions & 0 deletions modules/swagger-codegen/src/main/resources/php-symfony/.php_cs
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 modules/swagger-codegen/src/main/resources/php-symfony/.travis.yml
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"
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)]);
}
}
}
}
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];
}
}
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());
}
}
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()),
];
}
}
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}}';
}
}
Loading

0 comments on commit ebd77fe

Please sign in to comment.