From 170249b3693b60b208e87da322e28f2e7bf08486 Mon Sep 17 00:00:00 2001 From: Ener-Getick Date: Fri, 29 Jul 2016 10:22:40 +0200 Subject: [PATCH] Add ApiPlatform support --- .php_cs | 2 +- .../Compiler/AddRouteDescribersPass.php | 2 +- DependencyInjection/EXSystApiDocExtension.php | 6 ++ Describer/ApiPlatformDescriber.php | 28 ++++++++ Describer/ExternalDocDescriber.php | 15 ++-- Describer/RouteDescriber.php | 8 ++- Describer/SwaggerPhpDescriber.php | 24 ++----- Model/ModelDescriber.php | 26 +++++++ Resources/config/api_platform.xml | 19 ++++++ Resources/config/services.xml | 1 + Tests/Functional/Entity/Dummy.php | 68 +++++++++++++++++++ Tests/Functional/TestBundle.php | 18 +++++ Tests/Functional/TestKernel.php | 8 ++- composer.json | 16 +++-- 14 files changed, 203 insertions(+), 38 deletions(-) create mode 100644 Describer/ApiPlatformDescriber.php create mode 100644 Model/ModelDescriber.php create mode 100644 Resources/config/api_platform.xml create mode 100644 Tests/Functional/Entity/Dummy.php create mode 100644 Tests/Functional/TestBundle.php diff --git a/.php_cs b/.php_cs index 367c4b5..41a7cac 100644 --- a/.php_cs +++ b/.php_cs @@ -21,7 +21,7 @@ HeaderCommentFixer::setHeader($header); return Config::create() ->level(FixerInterface::SYMFONY_LEVEL) - ->fixers(array('align_double_arrow', 'header_comment')) + ->fixers(array('header_comment')) ->finder($finder) ->setUsingCache(true) ; diff --git a/DependencyInjection/Compiler/AddRouteDescribersPass.php b/DependencyInjection/Compiler/AddRouteDescribersPass.php index 8b7af49..c2d2b8a 100644 --- a/DependencyInjection/Compiler/AddRouteDescribersPass.php +++ b/DependencyInjection/Compiler/AddRouteDescribersPass.php @@ -23,6 +23,6 @@ class AddRouteDescribersPass implements CompilerPassInterface { $routeDescribers = $this->findAndSortTaggedServices('exsyst_api_doc.route_describer', $container); - $container->getDefinition('exsyst_api_doc.describers.route')->replaceArgument(2, $routeDescribers); + $container->getDefinition('exsyst_api_doc.describers.route')->replaceArgument(3, $routeDescribers); } } diff --git a/DependencyInjection/EXSystApiDocExtension.php b/DependencyInjection/EXSystApiDocExtension.php index 7aef328..f243ab1 100644 --- a/DependencyInjection/EXSystApiDocExtension.php +++ b/DependencyInjection/EXSystApiDocExtension.php @@ -49,5 +49,11 @@ class EXSystApiDocExtension extends Extension if (!class_exists(Swagger::class)) { $container->removeDefinition('exsyst_api_doc.describers.swagger_php'); } + + $bundles = $container->getParameter('kernel.bundles'); + // ApiPlatform support + if (isset($bundles['ApiPlatformBundle']) && class_exists('ApiPlatform\Core\Documentation\Documentation')) { + $loader->load('api_platform.xml'); + } } } diff --git a/Describer/ApiPlatformDescriber.php b/Describer/ApiPlatformDescriber.php new file mode 100644 index 0000000..9906e74 --- /dev/null +++ b/Describer/ApiPlatformDescriber.php @@ -0,0 +1,28 @@ +normalize($documentation); + }, $overwrite); + } +} diff --git a/Describer/ExternalDocDescriber.php b/Describer/ExternalDocDescriber.php index 2cb3a71..6b65f68 100644 --- a/Describer/ExternalDocDescriber.php +++ b/Describer/ExternalDocDescriber.php @@ -11,35 +11,30 @@ 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; + private $overwrite; /** * @param array|callable $externalDoc * @param int $strategy */ - public function __construct($externalDoc, $strategy = Swagger::PREFER_ORIGINAL) + public function __construct($externalDoc, bool $overwrite = false) { $this->externalDoc = $externalDoc; - $this->strategy = $strategy; + $this->overwrite = $overwrite; } public function describe(Swagger $api) { $externalDoc = $this->getExternalDoc(); - $api->merge($externalDoc, $this->strategy); + $api->merge($externalDoc, $this->overwrite); } - private function getExternalDoc(): array + private function getExternalDoc() { if (is_callable($this->externalDoc)) { return call_user_func($this->externalDoc); diff --git a/Describer/RouteDescriber.php b/Describer/RouteDescriber.php index 74e892a..416abfe 100644 --- a/Describer/RouteDescriber.php +++ b/Describer/RouteDescriber.php @@ -15,20 +15,26 @@ use Doctrine\Common\Util\ClassUtils; use EXSyst\Bundle\ApiDocBundle\RouteDescriber\RouteDescriberInterface; use gossi\swagger\Swagger; use Symfony\Bundle\FrameworkBundle\Controller\ControllerNameParser; +use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\Routing\Route; use Symfony\Component\Routing\RouterInterface; class RouteDescriber implements DescriberInterface { + private $container; + private $router; + private $controllerNameParser; private $routeDescribers; /** + * @param ContainerInterface $container * @param RouterInterface $router * @param ControllerNameParser $controllerNameParser * @param RouteDescriberInterface[] $routeDescribers */ - public function __construct(RouterInterface $router, ControllerNameParser $controllerNameParser, array $routeDescribers) + public function __construct(ContainerInterface $container, RouterInterface $router, ControllerNameParser $controllerNameParser, array $routeDescribers) { + $this->container = $container; $this->router = $router; $this->controllerNameParser = $controllerNameParser; $this->routeDescribers = $routeDescribers; diff --git a/Describer/SwaggerPhpDescriber.php b/Describer/SwaggerPhpDescriber.php index 22a06a9..f710d72 100644 --- a/Describer/SwaggerPhpDescriber.php +++ b/Describer/SwaggerPhpDescriber.php @@ -11,31 +11,17 @@ namespace EXSyst\Bundle\ApiDocBundle\Describer; -use gossi\swagger\Swagger; - -class SwaggerPhpDescriber implements DescriberInterface +class SwaggerPhpDescriber extends ExternalDocDescriber { - private $projectPath; - private $overwrite; - /** * @param string $projectPath */ public function __construct(string $projectPath, bool $overwrite = false) { - $this->projectPath = $projectPath; - $this->overwrite = $overwrite; - } + parent::__construct(function () use ($projectPath) { + $annotation = \Swagger\scan($projectPath); - 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)); + return json_decode(json_encode($annotation)); + }, $overwrite); } } diff --git a/Model/ModelDescriber.php b/Model/ModelDescriber.php new file mode 100644 index 0000000..b4023e4 --- /dev/null +++ b/Model/ModelDescriber.php @@ -0,0 +1,26 @@ +namingStrategy = $namingStrategy; + } + + public function describe(string $class, array $options = []): Schema + { + + } +} diff --git a/Resources/config/api_platform.xml b/Resources/config/api_platform.xml new file mode 100644 index 0000000..5d6a474 --- /dev/null +++ b/Resources/config/api_platform.xml @@ -0,0 +1,19 @@ + + + + + + + + + + + + + + + + + diff --git a/Resources/config/services.xml b/Resources/config/services.xml index 3fe3636..ebdb4ba 100644 --- a/Resources/config/services.xml +++ b/Resources/config/services.xml @@ -16,6 +16,7 @@ + diff --git a/Tests/Functional/Entity/Dummy.php b/Tests/Functional/Entity/Dummy.php new file mode 100644 index 0000000..b6a8906 --- /dev/null +++ b/Tests/Functional/Entity/Dummy.php @@ -0,0 +1,68 @@ + + * + * @ApiResource( + * collectionOperations={"get"={"method"="GET"}}, + * itemOperations={"get"={"method"="GET"}}) + * ) + */ +class Dummy +{ + /** + * @var int + */ + private $id; + + /** + * @var string + * + * @Assert\NotBlank + * @ApiProperty(iri="http://schema.org/name") + */ + private $name; + + /** + * @var array + */ + private $foo; + + public function getId(): int + { + return $this->id; + } + + public function setName(string $name) + { + $this->name = $name; + } + + public function getName(): string + { + return $this->name; + } + + public function hasRole(string $role) + { + } + + public function setFoo(array $foo = null) + { + } +} diff --git a/Tests/Functional/TestBundle.php b/Tests/Functional/TestBundle.php new file mode 100644 index 0000000..345fbf1 --- /dev/null +++ b/Tests/Functional/TestBundle.php @@ -0,0 +1,18 @@ +import(__DIR__.'/Controller/', '/', 'annotation'); + $routes->import('', '/api', 'api_platform'); } /** @@ -49,7 +54,8 @@ class TestKernel extends Kernel { $c->loadFromExtension('framework', [ 'secret' => 'MySecretKey', - 'test' => null, + 'test' => null, + 'validation' => null, ]); } } diff --git a/composer.json b/composer.json index 66f9804..0261120 100644 --- a/composer.json +++ b/composer.json @@ -10,21 +10,27 @@ ], "require": { "php": "^7.0", - "symfony/framework-bundle": "^3.2@dev", + "symfony/framework-bundle": "^3.2", "exsyst/swagger": "dev-master" }, "require-dev": { - "symfony/browser-kit": "^2.8|^3.0", - "symfony/phpunit-bridge": "^3.2@dev", + "symfony/validator": "^3.2", + "symfony/property-access": "^3.2", + "symfony/browser-kit": "^3.2", + "symfony/cache": "^3.2", + "symfony/phpunit-bridge": "^3.2", "sensio/framework-extra-bundle": "^3.0", "nelmio/api-doc-bundle": "^2.0", "phpdocumentor/reflection-docblock": "^3.1", "phpunit/phpunit": "^5.4", - "zircote/swagger-php": "^2.0" + "zircote/swagger-php": "^2.0", + "api-platform/core": "^2.0" }, "suggest": { "nelmio/api-doc-bundle": "For using the ApiDoc annotation.", - "phpdocumentor/reflection-docblock": "For parsing php docs." + "phpdocumentor/reflection-docblock": "For parsing php docs.", + "zircote/swagger-php": "For using swagger annotations.", + "api-platform/core": "For using an API oriented framework." }, "autoload": { "psr-4": {