Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
440 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
123 changes: 123 additions & 0 deletions
123
src/Bridge/Symfony/Bundle/SwaggerUi/SwaggerUiAction.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the API Platform project. | ||
* | ||
* (c) Kévin Dunglas <dunglas@gmail.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace ApiPlatform\Core\Bridge\Symfony\Bundle\SwaggerUi; | ||
|
||
use ApiPlatform\Core\Exception\RuntimeException; | ||
use ApiPlatform\Core\Metadata\Resource\Factory\ResourceMetadataFactoryInterface; | ||
use ApiPlatform\Core\OpenApi\Factory\OpenApiFactoryInterface; | ||
use ApiPlatform\Core\OpenApi\Options; | ||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\HttpFoundation\Response; | ||
use Symfony\Component\Routing\Generator\UrlGeneratorInterface; | ||
use Symfony\Component\Serializer\Normalizer\NormalizerInterface; | ||
use Twig\Environment as TwigEnvironment; | ||
|
||
/** | ||
* Displays the swaggerui interface. | ||
* | ||
* @author Antoine Bluchet <soyuka@gmail.com> | ||
*/ | ||
final class SwaggerUiAction | ||
{ | ||
private $twig; | ||
private $urlGenerator; | ||
private $normalizer; | ||
private $openApiFactory; | ||
private $openApiOptions; | ||
private $swaggerUiContext; | ||
private $formats; | ||
private $resourceMetadataFactory; | ||
private $oauthClientId; | ||
private $oauthClientSecret; | ||
|
||
public function __construct(ResourceMetadataFactoryInterface $resourceMetadataFactory, TwigEnvironment $twig, UrlGeneratorInterface $urlGenerator, NormalizerInterface $normalizer, OpenApiFactoryInterface $openApiFactory, Options $openApiOptions, SwaggerUiContext $swaggerUiContext, array $formats = [], string $oauthClientId = null, string $oauthClientSecret = null) | ||
{ | ||
$this->resourceMetadataFactory = $resourceMetadataFactory; | ||
$this->twig = $twig; | ||
$this->urlGenerator = $urlGenerator; | ||
$this->normalizer = $normalizer; | ||
$this->openApiFactory = $openApiFactory; | ||
$this->openApiOptions = $openApiOptions; | ||
$this->swaggerUiContext = $swaggerUiContext; | ||
$this->formats = $formats; | ||
$this->oauthClientId = $oauthClientId; | ||
$this->oauthClientSecret = $oauthClientSecret; | ||
} | ||
|
||
public function __invoke(Request $request) | ||
{ | ||
$openApi = $this->openApiFactory->__invoke(['base_url' => $request->getBaseUrl() ?: '/']); | ||
|
||
$swaggerContext = [ | ||
'formats' => $this->formats, | ||
'title' => $openApi->getInfo()->getTitle(), | ||
'description' => $openApi->getInfo()->getDescription(), | ||
'showWebby' => $this->swaggerUiContext->isWebbyShown(), | ||
'swaggerUiEnabled' => $this->swaggerUiContext->isSwaggerUiEnabled(), | ||
'reDocEnabled' => $this->swaggerUiContext->isRedocEnabled(), | ||
// FIXME: typo graphql => graphQl | ||
'graphqlEnabled' => $this->swaggerUiContext->isGraphQlEnabled(), | ||
'graphiQlEnabled' => $this->swaggerUiContext->isGraphiQlEnabled(), | ||
'graphQlPlaygroundEnabled' => $this->swaggerUiContext->isGraphQlPlaygroundEnabled(), | ||
]; | ||
|
||
$swaggerData = [ | ||
'url' => $this->urlGenerator->generate('api_doc', ['format' => 'json']), | ||
'spec' => $this->normalizer->normalize($openApi, 'json', []), | ||
'oauth' => [ | ||
'enabled' => $this->openApiOptions->getOAuthEnabled(), | ||
'type' => $this->openApiOptions->getOAuthType(), | ||
'flow' => $this->openApiOptions->getOAuthFlow(), | ||
'tokenUrl' => $this->openApiOptions->getOAuthTokenUrl(), | ||
'authorizationUrl' => $this->openApiOptions->getOAuthAuthorizationUrl(), | ||
'scopes' => $this->openApiOptions->getOAuthScopes(), | ||
'clientId' => $this->oauthClientId, | ||
'clientSecret' => $this->oauthClientSecret, | ||
], | ||
]; | ||
|
||
if ($request->isMethodSafe() && null !== $resourceClass = $request->attributes->get('_api_resource_class')) { | ||
$swaggerData['id'] = $request->attributes->get('id'); | ||
$swaggerData['queryParameters'] = $request->query->all(); | ||
|
||
$metadata = $this->resourceMetadataFactory->create($resourceClass); | ||
$swaggerData['shortName'] = $metadata->getShortName(); | ||
|
||
if (null !== $collectionOperationName = $request->attributes->get('_api_collection_operation_name')) { | ||
$swaggerData['operationId'] = sprintf('%s%sCollection', $collectionOperationName, ucfirst($swaggerData['shortName'])); | ||
} elseif (null !== $itemOperationName = $request->attributes->get('_api_item_operation_name')) { | ||
$swaggerData['operationId'] = sprintf('%s%sItem', $itemOperationName, ucfirst($swaggerData['shortName'])); | ||
} elseif (null !== $subresourceOperationContext = $request->attributes->get('_api_subresource_context')) { | ||
$swaggerData['operationId'] = $subresourceOperationContext['operationId']; | ||
} | ||
|
||
[$swaggerData['path'], $swaggerData['method']] = $this->getPathAndMethod($swaggerData); | ||
} | ||
|
||
return new Response($this->twig->render('@ApiPlatform/SwaggerUi/index.html.twig', $swaggerContext + ['swagger_data' => $swaggerData])); | ||
} | ||
|
||
private function getPathAndMethod(array $swaggerData): array | ||
{ | ||
foreach ($swaggerData['spec']['paths'] as $path => $operations) { | ||
foreach ($operations as $method => $operation) { | ||
if ($operation['operationId'] === $swaggerData['operationId']) { | ||
return [$path, $method]; | ||
} | ||
} | ||
} | ||
|
||
throw new RuntimeException(sprintf('The operation "%s" cannot be found in the Swagger specification.', $swaggerData['operationId'])); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the API Platform project. | ||
* | ||
* (c) Kévin Dunglas <dunglas@gmail.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace ApiPlatform\Core\Bridge\Symfony\Bundle\SwaggerUi; | ||
|
||
final class SwaggerUiContext | ||
{ | ||
private $swaggerUiEnabled; | ||
private $showWebby; | ||
private $reDocEnabled; | ||
private $graphQlEnabled; | ||
private $graphiQlEnabled; | ||
private $graphQlPlaygroundEnabled; | ||
|
||
public function __construct(bool $swaggerUiEnabled = false, bool $showWebby = true, bool $reDocEnabled = false, bool $graphQlEnabled = false, bool $graphiQlEnabled = false, bool $graphQlPlaygroundEnabled = false) | ||
{ | ||
$this->swaggerUiEnabled = $swaggerUiEnabled; | ||
$this->showWebby = $showWebby; | ||
$this->reDocEnabled = $reDocEnabled; | ||
$this->graphQlEnabled = $graphQlEnabled; | ||
$this->graphiQlEnabled = $graphiQlEnabled; | ||
$this->graphQlPlaygroundEnabled = $graphQlPlaygroundEnabled; | ||
} | ||
|
||
public function isSwaggerUiEnabled(): bool | ||
{ | ||
return $this->swaggerUiEnabled; | ||
} | ||
|
||
public function isWebbyShown(): bool | ||
{ | ||
return $this->showWebby; | ||
} | ||
|
||
public function isRedocEnabled(): bool | ||
{ | ||
return $this->reDocEnabled; | ||
} | ||
|
||
public function isGraphQlEnabled(): bool | ||
{ | ||
return $this->graphQlEnabled; | ||
} | ||
|
||
public function isGraphiQlEnabled(): bool | ||
{ | ||
return $this->graphiQlEnabled; | ||
} | ||
|
||
public function isGraphQlPlaygroundEnabled(): bool | ||
{ | ||
return $this->graphQlPlaygroundEnabled; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.