SwaggerUiLibraryDiscovery.php in Swagger UI Field Formatter 8.3
File
src/Service/SwaggerUiLibraryDiscovery.php
View source
<?php
declare (strict_types=1);
namespace Drupal\swagger_ui_formatter\Service;
use Drupal\Component\Serialization\Json;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Cache\CacheableDependencyInterface;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Drupal\Core\Extension\ThemeHandlerInterface;
use Drupal\Core\Theme\ThemeInitializationInterface;
use Drupal\Core\Theme\ThemeManagerInterface;
use Drupal\swagger_ui_formatter\Exception\SwaggerUiLibraryDiscoveryException;
use Symfony\Component\DependencyInjection\ContainerInterface;
final class SwaggerUiLibraryDiscovery implements SwaggerUiLibraryDiscoveryInterface, ContainerInjectionInterface, CacheableDependencyInterface {
private const LIBRARY_PATH_CID = 'swagger_ui_formatter:library_path';
public const MIN_SUPPORTED_LIBRARY_VERSION = '3.32.2';
private $cache;
private $themeHandler;
private $themeManager;
private $themeInitialization;
public function __construct(CacheBackendInterface $cache, ThemeHandlerInterface $theme_handler, ThemeManagerInterface $theme_manager, ThemeInitializationInterface $theme_initialization) {
$this->cache = $cache;
$this->themeHandler = $theme_handler;
$this->themeManager = $theme_manager;
$this->themeInitialization = $theme_initialization;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('cache.default'), $container
->get('theme_handler'), $container
->get('theme.manager'), $container
->get('theme.initialization'));
}
public function libraryDirectory() : string {
$cache = $this->cache
->get(self::LIBRARY_PATH_CID);
if ($cache) {
return $cache->data;
}
$library_dir = 'libraries/swagger-ui';
$default_theme = $this->themeInitialization
->getActiveThemeByName($this->themeHandler
->getDefault());
$this->themeInitialization
->loadActiveTheme($default_theme);
$this->themeManager
->alterForTheme($default_theme, 'swagger_ui_library_directory', $library_dir);
$library_dir = ltrim($library_dir, '/');
$this
->validateLibraryDirectory(DRUPAL_ROOT . '/' . $library_dir);
$this->cache
->set(self::LIBRARY_PATH_CID, $library_dir, $this
->getCacheMaxAge(), $this
->getCacheTags());
return $library_dir;
}
public function libraryVersion() : string {
$library_dir = $this
->libraryDirectory();
$package_json_path = DRUPAL_ROOT . '/' . $library_dir . '/package.json';
$package_json_content = file_get_contents($package_json_path);
if (!$package_json_content) {
throw SwaggerUiLibraryDiscoveryException::becauseCannotReadPackageJsonContent($package_json_path);
}
$data = Json::decode($package_json_content);
if (json_last_error() !== JSON_ERROR_NONE) {
throw SwaggerUiLibraryDiscoveryException::becausePackageJsonCannotBeDecoded($package_json_path, json_last_error_msg());
}
if (!isset($data['version'])) {
throw SwaggerUiLibraryDiscoveryException::becauseUnableToIdentifyLibraryVersion($package_json_path);
}
if (version_compare($data['version'], self::MIN_SUPPORTED_LIBRARY_VERSION, '<')) {
throw SwaggerUiLibraryDiscoveryException::becauseLibraryVersionIsNotSupported($data['version'], self::MIN_SUPPORTED_LIBRARY_VERSION);
}
return $data['version'];
}
private function validateLibraryDirectory(string $library_dir) : void {
if (!file_exists($library_dir)) {
throw SwaggerUiLibraryDiscoveryException::becauseLibraryDirectoryIsInvalid($library_dir);
}
$files_to_check = [
'package.json',
'dist/swagger-ui.css',
'dist/swagger-ui-bundle.js',
'dist/swagger-ui-standalone-preset.js',
'dist/oauth2-redirect.html',
];
foreach ($files_to_check as $file) {
$file_path = $library_dir . '/' . $file;
if (!file_exists($file_path)) {
throw SwaggerUiLibraryDiscoveryException::becauseRequiredLibraryFileIsNotFound($file_path);
}
}
}
public function reset() : void {
$this->cache
->delete(self::LIBRARY_PATH_CID);
}
public function getCacheContexts() : array {
return [];
}
public function getCacheTags() : array {
return [
self::LIBRARY_PATH_CID,
'config:system.theme',
];
}
public function getCacheMaxAge() : int {
return CacheBackendInterface::CACHE_PERMANENT;
}
}