mirror of
https://github.com/retailcrm/NelmioApiDocBundle.git
synced 2025-02-02 23:59:26 +03:00
Use ener-getick/swagger
This commit is contained in:
parent
d7c05edffc
commit
71f4458085
@ -13,6 +13,7 @@ namespace EXSyst\Bundle\ApiDocBundle\DependencyInjection;
|
|||||||
|
|
||||||
use Nelmio\ApiDocBundle\Annotation\ApiDoc;
|
use Nelmio\ApiDocBundle\Annotation\ApiDoc;
|
||||||
use phpDocumentor\Reflection\DocBlockFactory;
|
use phpDocumentor\Reflection\DocBlockFactory;
|
||||||
|
use Swagger\Annotations\Swagger;
|
||||||
use Symfony\Component\Config\FileLocator;
|
use Symfony\Component\Config\FileLocator;
|
||||||
use Symfony\Component\DependencyInjection\ContainerBuilder;
|
use Symfony\Component\DependencyInjection\ContainerBuilder;
|
||||||
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
|
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
|
||||||
@ -40,10 +41,13 @@ class EXSystApiDocExtension extends Extension
|
|||||||
|
|
||||||
// Removes useless services
|
// Removes useless services
|
||||||
if (!class_exists(ApiDoc::class)) {
|
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)) {
|
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');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
52
Describer/ExternalDocDescriber.php
Normal file
52
Describer/ExternalDocDescriber.php
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
@ -12,7 +12,7 @@
|
|||||||
namespace EXSyst\Bundle\ApiDocBundle\Describer;
|
namespace EXSyst\Bundle\ApiDocBundle\Describer;
|
||||||
|
|
||||||
use Doctrine\Common\Util\ClassUtils;
|
use Doctrine\Common\Util\ClassUtils;
|
||||||
use EXSyst\Bundle\ApiDocBundle\Extractor\Routing\RouteExtractorInterface;
|
use EXSyst\Bundle\ApiDocBundle\RouteDescriber\RouteDescriberInterface;
|
||||||
use gossi\swagger\Swagger;
|
use gossi\swagger\Swagger;
|
||||||
use Symfony\Bundle\FrameworkBundle\Controller\ControllerNameParser;
|
use Symfony\Bundle\FrameworkBundle\Controller\ControllerNameParser;
|
||||||
use Symfony\Component\Routing\Route;
|
use Symfony\Component\Routing\Route;
|
||||||
@ -34,7 +34,7 @@ class RouteDescriber implements DescriberInterface
|
|||||||
$this->routeDescribers = $routeDescribers;
|
$this->routeDescribers = $routeDescribers;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function describe(Swagger $swagger)
|
public function describe(Swagger $api)
|
||||||
{
|
{
|
||||||
if (0 === count($this->routeDescribers)) {
|
if (0 === count($this->routeDescribers)) {
|
||||||
return;
|
return;
|
||||||
@ -45,7 +45,7 @@ class RouteDescriber implements DescriberInterface
|
|||||||
if ($method = $this->getReflectionMethod($route->getDefault('_controller'))) {
|
if ($method = $this->getReflectionMethod($route->getDefault('_controller'))) {
|
||||||
// Extract as many informations as possible about this route
|
// Extract as many informations as possible about this route
|
||||||
foreach ($this->routeDescribers as $describer) {
|
foreach ($this->routeDescribers as $describer) {
|
||||||
$describer->describe($swagger, $route, $method);
|
$describer->describe($api, $route, $method);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
46
Describer/SwaggerPhpDescriber.php
Normal file
46
Describer/SwaggerPhpDescriber.php
Normal 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));
|
||||||
|
}
|
||||||
|
}
|
@ -9,6 +9,12 @@
|
|||||||
</service>
|
</service>
|
||||||
|
|
||||||
<!-- Extractors -->
|
<!-- 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">
|
<service id="exsyst_api_doc.describers.route" class="EXSyst\Bundle\ApiDocBundle\Describer\RouteDescriber" public="false">
|
||||||
<argument type="service" id="router" />
|
<argument type="service" id="router" />
|
||||||
<argument type="service" id="controller_name_converter" />
|
<argument type="service" id="controller_name_converter" />
|
||||||
|
@ -101,10 +101,10 @@ class NelmioAnnotationDescriber implements RouteDescriberInterface
|
|||||||
$items = $items->getItems();
|
$items = $items->getItems();
|
||||||
} while ('[]' === substr($requirement, -2));
|
} while ('[]' === substr($requirement, -2));
|
||||||
|
|
||||||
$items->setType(Swagger::T_STRING);
|
$items->setType('string');
|
||||||
$items->setFormat($requirement);
|
$items->setFormat($requirement);
|
||||||
} else {
|
} else {
|
||||||
$parameter->setType(Swagger::T_STRING);
|
$parameter->setType('string');
|
||||||
$parameter->setFormat($requirement);
|
$parameter->setFormat($requirement);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -26,7 +26,7 @@ class RouteMetadataDescriber implements RouteDescriberInterface
|
|||||||
foreach ($route->getRequirements() as $parameterName => $requirement) {
|
foreach ($route->getRequirements() as $parameterName => $requirement) {
|
||||||
$parameter = $operation->getParameters()->get($parameterName, 'path');
|
$parameter = $operation->getParameters()->get($parameterName, 'path');
|
||||||
$parameter->setRequired(true);
|
$parameter->setRequired(true);
|
||||||
$parameter->setType(Swagger::T_STRING);
|
$parameter->setType('string');
|
||||||
$parameter->setFormat($requirement);
|
$parameter->setFormat($requirement);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
24
Tests/Functional/Fixtures/SwaggerPhp/Info.php
Normal file
24
Tests/Functional/Fixtures/SwaggerPhp/Info.php
Normal 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
|
||||||
|
{
|
||||||
|
}
|
@ -50,6 +50,15 @@ class FunctionalTest extends WebTestCase
|
|||||||
$this->assertTrue($operation->getDeprecated());
|
$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()
|
private function getSwaggerDefinition()
|
||||||
{
|
{
|
||||||
static::createClient();
|
static::createClient();
|
||||||
|
@ -9,17 +9,18 @@
|
|||||||
}
|
}
|
||||||
],
|
],
|
||||||
"require": {
|
"require": {
|
||||||
"php": ">=7.0",
|
"php": "^7.0",
|
||||||
"symfony/framework-bundle": "^3.2@dev",
|
"symfony/framework-bundle": "^3.2@dev",
|
||||||
"gossi/swagger": "^0.2"
|
"exsyst/swagger": "dev-master"
|
||||||
},
|
},
|
||||||
"require-dev": {
|
"require-dev": {
|
||||||
"symfony/browser-kit": "~2.8|~3.0",
|
"symfony/browser-kit": "^2.8|^3.0",
|
||||||
"symfony/phpunit-bridge": "^3.2@dev",
|
"symfony/phpunit-bridge": "^3.2@dev",
|
||||||
"sensio/framework-extra-bundle": "~3.0",
|
"sensio/framework-extra-bundle": "^3.0",
|
||||||
"nelmio/api-doc-bundle": "^2.0",
|
"nelmio/api-doc-bundle": "^2.0",
|
||||||
"phpdocumentor/reflection-docblock": "^3.1",
|
"phpdocumentor/reflection-docblock": "^3.1",
|
||||||
"phpunit/phpunit": "^5.4"
|
"phpunit/phpunit": "^5.4",
|
||||||
|
"zircote/swagger-php": "^2.0"
|
||||||
},
|
},
|
||||||
"suggest": {
|
"suggest": {
|
||||||
"nelmio/api-doc-bundle": "For using the ApiDoc annotation.",
|
"nelmio/api-doc-bundle": "For using the ApiDoc annotation.",
|
||||||
|
Loading…
x
Reference in New Issue
Block a user