Use ener-getick/swagger

This commit is contained in:
Ener-Getick 2016-07-28 10:20:59 +02:00
parent d7c05edffc
commit 71f4458085
No known key found for this signature in database
GPG Key ID: 9E5D2DB67BF054DD
10 changed files with 155 additions and 13 deletions

View File

@ -13,6 +13,7 @@ namespace EXSyst\Bundle\ApiDocBundle\DependencyInjection;
use Nelmio\ApiDocBundle\Annotation\ApiDoc;
use phpDocumentor\Reflection\DocBlockFactory;
use Swagger\Annotations\Swagger;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
@ -40,10 +41,13 @@ class EXSystApiDocExtension extends Extension
// Removes useless services
if (!class_exists(ApiDoc::class)) {
$container->removeDefinition('exsyst_api_doc.routing_extractors.nelmio_annotation');
$container->removeDefinition('exsyst_api_doc.route_describers.nelmio_annotation');
}
if (!class_exists(DocBlockFactory::class)) {
$container->removeDefinition('exsyst_api_doc.routing_extractors.php_doc');
$container->removeDefinition('exsyst_api_doc.route_describers.php_doc');
}
if (!class_exists(Swagger::class)) {
$container->removeDefinition('exsyst_api_doc.describers.swagger_php');
}
}
}

View File

@ -0,0 +1,52 @@
<?php
<?php
/*
* This file is part of the ApiDocBundle package.
*
* (c) EXSyst
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace EXSyst\Bundle\ApiDocBundle\Describer;
use Doctrine\Common\Util\ClassUtils;
use EXSyst\Bundle\ApiDocBundle\RouteDescriber\RouteDescriberInterface;
use gossi\swagger\Swagger;
use Symfony\Bundle\FrameworkBundle\Controller\ControllerNameParser;
use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\RouterInterface;
class ExternalDocDescriber implements DescriberInterface
{
private $externalDoc;
private $stategy;
/**
* @param array|callable $externalDoc
* @param int $strategy
*/
public function __construct($externalDoc, $strategy = Swagger::PREFER_ORIGINAL)
{
$this->externalDoc = $externalDoc;
$this->strategy = $strategy;
}
public function describe(Swagger $api)
{
$externalDoc = $this->getExternalDoc();
$api->merge($externalDoc, $this->strategy);
}
private function getExternalDoc(): array
{
if (is_callable($this->externalDoc)) {
return call_user_func($this->externalDoc);
}
return $this->externalDoc;
}
}

View File

@ -12,7 +12,7 @@
namespace EXSyst\Bundle\ApiDocBundle\Describer;
use Doctrine\Common\Util\ClassUtils;
use EXSyst\Bundle\ApiDocBundle\Extractor\Routing\RouteExtractorInterface;
use EXSyst\Bundle\ApiDocBundle\RouteDescriber\RouteDescriberInterface;
use gossi\swagger\Swagger;
use Symfony\Bundle\FrameworkBundle\Controller\ControllerNameParser;
use Symfony\Component\Routing\Route;
@ -34,7 +34,7 @@ class RouteDescriber implements DescriberInterface
$this->routeDescribers = $routeDescribers;
}
public function describe(Swagger $swagger)
public function describe(Swagger $api)
{
if (0 === count($this->routeDescribers)) {
return;
@ -45,7 +45,7 @@ class RouteDescriber implements DescriberInterface
if ($method = $this->getReflectionMethod($route->getDefault('_controller'))) {
// Extract as many informations as possible about this route
foreach ($this->routeDescribers as $describer) {
$describer->describe($swagger, $route, $method);
$describer->describe($api, $route, $method);
}
}
}

View File

@ -0,0 +1,46 @@
<?php
/*
* This file is part of the ApiDocBundle package.
*
* (c) EXSyst
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace EXSyst\Bundle\ApiDocBundle\Describer;
use Doctrine\Common\Util\ClassUtils;
use EXSyst\Bundle\ApiDocBundle\RouteDescriber\RouteDescriberInterface;
use gossi\swagger\Swagger;
use Symfony\Bundle\FrameworkBundle\Controller\ControllerNameParser;
use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\RouterInterface;
class SwaggerPhpDescriber implements DescriberInterface
{
private $projectPath;
private $overwrite;
/**
* @param string $projectPath
*/
public function __construct(string $projectPath, bool $overwrite = false)
{
$this->projectPath = $projectPath;
$this->overwrite = $overwrite;
}
public function describe(Swagger $api)
{
$annotation = \Swagger\scan($this->projectPath);
$api->merge($this->normalize($annotation), $this->overwrite);
}
private function normalize($annotation)
{
return json_decode(json_encode($annotation));
}
}

View File

@ -9,6 +9,12 @@
</service>
<!-- Extractors -->
<service id="exsyst_api_doc.describers.swagger_php" class="EXSyst\Bundle\ApiDocBundle\Describer\SwaggerPhpDescriber" public="false">
<argument>%kernel.root_dir%</argument>
<tag name="exsyst_api_doc.describer" priority="-150" />
</service>
<service id="exsyst_api_doc.describers.route" class="EXSyst\Bundle\ApiDocBundle\Describer\RouteDescriber" public="false">
<argument type="service" id="router" />
<argument type="service" id="controller_name_converter" />

View File

@ -101,10 +101,10 @@ class NelmioAnnotationDescriber implements RouteDescriberInterface
$items = $items->getItems();
} while ('[]' === substr($requirement, -2));
$items->setType(Swagger::T_STRING);
$items->setType('string');
$items->setFormat($requirement);
} else {
$parameter->setType(Swagger::T_STRING);
$parameter->setType('string');
$parameter->setFormat($requirement);
}

View File

@ -26,7 +26,7 @@ class RouteMetadataDescriber implements RouteDescriberInterface
foreach ($route->getRequirements() as $parameterName => $requirement) {
$parameter = $operation->getParameters()->get($parameterName, 'path');
$parameter->setRequired(true);
$parameter->setType(Swagger::T_STRING);
$parameter->setType('string');
$parameter->setFormat($requirement);
}
}

View File

@ -0,0 +1,24 @@
<?php
/*
* This file is part of the ApiDocBundle package.
*
* (c) EXSyst
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace EXSyst\Bundle\ApiDocBundle\Tests\Functional\Fixtures\SwaggerPhp;
use Swagger\Annotations\Info as InfoAnnotation;
/**
* @InfoAnnotation(
* title="My Awesome App",
* version="1.3"
* )
*/
class Info
{
}

View File

@ -50,6 +50,15 @@ class FunctionalTest extends WebTestCase
$this->assertTrue($operation->getDeprecated());
}
public function testSwaggerPhpInfo()
{
$api = $this->getSwaggerDefinition();
$info = $api->getInfo();
$this->assertEquals('My Awesome App', $info->getTitle());
$this->assertEquals('1.3', $info->getVersion());
}
private function getSwaggerDefinition()
{
static::createClient();

View File

@ -9,17 +9,18 @@
}
],
"require": {
"php": ">=7.0",
"php": "^7.0",
"symfony/framework-bundle": "^3.2@dev",
"gossi/swagger": "^0.2"
"exsyst/swagger": "dev-master"
},
"require-dev": {
"symfony/browser-kit": "~2.8|~3.0",
"symfony/browser-kit": "^2.8|^3.0",
"symfony/phpunit-bridge": "^3.2@dev",
"sensio/framework-extra-bundle": "~3.0",
"sensio/framework-extra-bundle": "^3.0",
"nelmio/api-doc-bundle": "^2.0",
"phpdocumentor/reflection-docblock": "^3.1",
"phpunit/phpunit": "^5.4"
"phpunit/phpunit": "^5.4",
"zircote/swagger-php": "^2.0"
},
"suggest": {
"nelmio/api-doc-bundle": "For using the ApiDoc annotation.",