welcome back to dyb-tech
This commit is contained in:
@@ -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'],
|
||||
]
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user