welcome back to dyb-tech

This commit is contained in:
Daniel Guzman
2024-05-18 02:28:01 +02:00
parent 9513cdba09
commit 9f30bc98c7
6149 changed files with 668407 additions and 0 deletions
+19
View File
@@ -0,0 +1,19 @@
<?php
/*
* This file is part of the NelmioApiDocBundle package.
*
* (c) Nelmio
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Nelmio\ApiDocBundle\Render\Html;
class AssetsMode
{
public const BUNDLE = 'bundle';
public const CDN = 'cdn';
public const OFFLINE = 'offline';
}
@@ -0,0 +1,96 @@
<?php
/*
* This file is part of the NelmioApiDocBundle package.
*
* (c) Nelmio
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Nelmio\ApiDocBundle\Render\Html;
use Symfony\Bridge\Twig\Extension\AssetExtension;
use Twig\Extension\AbstractExtension;
use Twig\TwigFunction;
/**
* @internal
*/
class GetNelmioAsset extends AbstractExtension
{
private $assetExtension;
private $resourcesDir;
private $cdnUrl;
public function __construct(AssetExtension $assetExtension)
{
$this->assetExtension = $assetExtension;
$this->cdnUrl = 'https://cdn.jsdelivr.net/gh/nelmio/NelmioApiDocBundle/Resources/public';
$this->resourcesDir = __DIR__.'/../../Resources/public';
}
public function getFunctions(): array
{
return [
new TwigFunction('nelmioAsset', $this, ['is_safe' => ['html']]),
];
}
public function __invoke($defaultAssetsMode, $asset)
{
[$extension, $mode] = $this->getExtension($defaultAssetsMode, $asset);
[$resource, $isInline] = $this->getResource($asset, $mode);
if ('js' == $extension) {
return $this->renderJavascript($resource, $isInline);
} elseif ('css' == $extension) {
return $this->renderCss($resource, $isInline);
} else {
return $resource;
}
}
private function getExtension($assetsMode, $asset)
{
$extension = mb_substr($asset, -3, 3, 'utf-8');
if ('.js' === $extension) {
return ['js', $assetsMode];
} elseif ('png' === $extension) {
return ['png', AssetsMode::OFFLINE == $assetsMode ? AssetsMode::CDN : $assetsMode];
} else {
return ['css', $assetsMode];
}
}
private function getResource($asset, $mode)
{
if (filter_var($asset, FILTER_VALIDATE_URL)) {
return [$asset, false];
} elseif (AssetsMode::OFFLINE === $mode) {
return [file_get_contents($this->resourcesDir.'/'.$asset), true];
} elseif (AssetsMode::CDN === $mode) {
return [$this->cdnUrl.'/'.$asset, false];
} else {
return [$this->assetExtension->getAssetUrl(sprintf('bundles/nelmioapidoc/%s', $asset)), false];
}
}
private function renderJavascript(string $script, bool $isInline)
{
if ($isInline) {
return sprintf('<script>%s</script>', $script);
} else {
return sprintf('<script src="%s"></script>', $script);
}
}
private function renderCss(string $stylesheet, bool $isInline)
{
if ($isInline) {
return sprintf('<style>%s</style>', $stylesheet);
} else {
return sprintf('<link rel="stylesheet" href="%s">', $stylesheet);
}
}
}
@@ -0,0 +1,57 @@
<?php
/*
* This file is part of the NelmioApiDocBundle package.
*
* (c) Nelmio
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Nelmio\ApiDocBundle\Render\Html;
use InvalidArgumentException;
use Nelmio\ApiDocBundle\Render\OpenApiRenderer;
use Nelmio\ApiDocBundle\Render\RenderOpenApi;
use OpenApi\Annotations\OpenApi;
use Twig\Environment;
/**
* @internal
*/
class HtmlOpenApiRenderer implements OpenApiRenderer
{
/** @var Environment|\Twig_Environment */
private $twig;
public function __construct($twig)
{
if (!$twig instanceof \Twig_Environment && !$twig instanceof Environment) {
throw new InvalidArgumentException(sprintf('Providing an instance of "%s" as twig is not supported.', get_class($twig)));
}
$this->twig = $twig;
}
public function getFormat(): string
{
return RenderOpenApi::HTML;
}
public function render(OpenApi $spec, array $options = []): string
{
$options += [
'assets_mode' => AssetsMode::CDN,
'swagger_ui_config' => [],
];
return $this->twig->render(
'@NelmioApiDoc/SwaggerUi/index.html.twig',
[
'swagger_data' => ['spec' => json_decode($spec->toJson(), true)],
'assets_mode' => $options['assets_mode'],
'swagger_ui_config' => $options['swagger_ui_config'],
]
);
}
}
@@ -0,0 +1,37 @@
<?php
/*
* This file is part of the NelmioApiDocBundle package.
*
* (c) Nelmio
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Nelmio\ApiDocBundle\Render\Json;
use Nelmio\ApiDocBundle\Render\OpenApiRenderer;
use Nelmio\ApiDocBundle\Render\RenderOpenApi;
use OpenApi\Annotations\OpenApi;
/**
* @internal
*/
class JsonOpenApiRenderer implements OpenApiRenderer
{
public function getFormat(): string
{
return RenderOpenApi::JSON;
}
public function render(OpenApi $spec, array $options = []): string
{
$options += [
'no-pretty' => false,
];
$flags = $options['no-pretty'] ? 0 : JSON_PRETTY_PRINT;
return json_encode($spec, $flags | JSON_UNESCAPED_SLASHES);
}
}
+24
View File
@@ -0,0 +1,24 @@
<?php
/*
* This file is part of the NelmioApiDocBundle package.
*
* (c) Nelmio
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Nelmio\ApiDocBundle\Render;
use OpenApi\Annotations\OpenApi;
/**
* @internal
*/
interface OpenApiRenderer
{
public function getFormat(): string;
public function render(OpenApi $spec, array $options = []): string;
}
+103
View File
@@ -0,0 +1,103 @@
<?php
/*
* This file is part of the NelmioApiDocBundle package.
*
* (c) Nelmio
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Nelmio\ApiDocBundle\Render;
use Nelmio\ApiDocBundle\Exception\RenderInvalidArgumentException;
use OpenApi\Annotations\OpenApi;
use OpenApi\Annotations\Server;
use OpenApi\Context;
use OpenApi\Generator;
use Psr\Container\ContainerInterface;
use Symfony\Component\HttpFoundation\Request;
class RenderOpenApi
{
public const HTML = 'html';
public const JSON = 'json';
public const YAML = 'yaml';
/** @var ContainerInterface */
private $generatorLocator;
/** @var array<string, OpenApiRenderer|null> */
private $openApiRenderers = [];
public function __construct(ContainerInterface $generatorLocator, ?OpenApiRenderer ...$openApiRenderers)
{
$this->generatorLocator = $generatorLocator;
foreach ($openApiRenderers as $openApiRenderer) {
if (null === $openApiRenderer) {
continue;
}
$this->openApiRenderers[$openApiRenderer->getFormat()] = $openApiRenderer;
}
}
public function getAvailableFormats(): array
{
return array_keys($this->openApiRenderers);
}
public function renderFromRequest(Request $request, string $format, $area, array $extraOptions = [])
{
$options = [];
if ('' !== $request->getBaseUrl()) {
$options += [
'fallback_url' => $request->getSchemeAndHttpHost().$request->getBaseUrl(),
];
}
$options += $extraOptions;
return $this->render($format, $area, $options);
}
/**
* @throws InvalidArgumentException If the area to dump is not valid
*/
public function render(string $format, string $area, array $options = []): string
{
if (!$this->generatorLocator->has($area)) {
throw new RenderInvalidArgumentException(sprintf('Area "%s" is not supported.', $area));
} elseif (!array_key_exists($format, $this->openApiRenderers)) {
throw new RenderInvalidArgumentException(sprintf('Format "%s" is not supported.', $format));
}
/** @var OpenApi $spec */
$spec = $this->generatorLocator->get($area)->generate();
$tmpServers = $spec->servers;
try {
$spec->servers = $this->getServersFromOptions($spec, $options);
return $this->openApiRenderers[$format]->render($spec, $options);
} finally {
$spec->servers = $tmpServers; // Restore original value as we should not modify OpenApi object from the generator
}
}
private function getServersFromOptions(OpenApi $spec, array $options)
{
if (array_key_exists('server_url', $options) && $options['server_url']) {
return [new Server(['url' => $options['server_url'], '_context' => new Context()])];
}
if (Generator::UNDEFINED !== $spec->servers) {
return $spec->servers;
}
if (array_key_exists('fallback_url', $options) && $options['fallback_url']) {
return [new Server(['url' => $options['fallback_url'], '_context' => new Context()])];
}
return Generator::UNDEFINED;
}
}
@@ -0,0 +1,32 @@
<?php
/*
* This file is part of the NelmioApiDocBundle package.
*
* (c) Nelmio
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Nelmio\ApiDocBundle\Render\Yaml;
use Nelmio\ApiDocBundle\Render\OpenApiRenderer;
use Nelmio\ApiDocBundle\Render\RenderOpenApi;
use OpenApi\Annotations\OpenApi;
/**
* @internal
*/
class YamlOpenApiRenderer implements OpenApiRenderer
{
public function getFormat(): string
{
return RenderOpenApi::YAML;
}
public function render(OpenApi $spec, array $options = []): string
{
return $spec->toYaml();
}
}