View source
<?php
declare (strict_types=1);
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\swagger_ui_formatter\Exception\SwaggerUiLibraryDiscoveryExceptionInterface;
function swagger_ui_formatter_theme() : array {
return [
'swagger_ui_field_item' => [
'variables' => [
'field_name' => NULL,
'delta' => NULL,
],
],
];
}
function swagger_ui_formatter_library_info_build() : array {
$libraries = [];
$swagger_ui_library_discovery = \Drupal::service('swagger_ui_formatter.swagger_ui_library_discovery');
try {
$library_dir = $swagger_ui_library_discovery
->libraryDirectory();
$library_version = $swagger_ui_library_discovery
->libraryVersion();
} catch (SwaggerUiLibraryDiscoveryExceptionInterface $exception) {
\Drupal::logger('swagger_ui_formatter')
->error('Unable to register Swagger UI library: ' . $exception
->getMessage());
return $libraries;
}
$library_dir = '/' . $library_dir;
$libraries['swagger_ui_formatter.swagger_ui'] = [
'version' => $library_version,
'css' => [
'theme' => [
$library_dir . '/dist/swagger-ui.css' => [
'minified' => TRUE,
],
],
],
'js' => [
$library_dir . '/dist/swagger-ui-bundle.js' => [
'minified' => TRUE,
],
$library_dir . '/dist/swagger-ui-standalone-preset.js' => [
'minified' => TRUE,
],
],
];
$libraries['swagger_ui_formatter.swagger_ui_integration'] = [
'version' => '1.0',
'js' => [
'js/swagger-ui-formatter.js' => [],
],
'dependencies' => [
'core/drupal',
'core/jquery',
'core/drupalSettings',
'swagger_ui_formatter/swagger_ui_formatter.swagger_ui',
],
];
return $libraries;
}
function swagger_ui_formatter_help(string $route_name, RouteMatchInterface $route_match) : string {
$output = '';
switch ($route_name) {
case 'help.page.swagger_ui_formatter':
$readme = file_get_contents(__DIR__ . '/README.md');
if ($readme && \Drupal::moduleHandler()
->moduleExists('markdown') === TRUE) {
$filter_manager = \Drupal::service('plugin.manager.filter');
$settings = \Drupal::configFactory()
->get('markdown.settings')
->getRawData();
$filter = $filter_manager
->createInstance('markdown', [
'settings' => $settings,
]);
$output = $filter
->process($readme, 'en');
}
elseif ($readme) {
$output = '<pre>' . $readme . '</pre>';
}
$output .= '<p>';
$output .= t('Visit the <a href=":project_link">project page</a> on Drupal.org for more information.', [
':project_link' => 'https://www.drupal.org/project/swagger_ui_formatter',
]);
$output .= '</p>';
}
return $output;
}
function swagger_ui_formatter_cache_flush() : void {
$service_name = 'swagger_ui_formatter.swagger_ui_library_discovery';
if (\Drupal::hasService($service_name)) {
$swagger_ui_library_discovery = \Drupal::service($service_name);
if (method_exists($swagger_ui_library_discovery, 'reset')) {
$swagger_ui_library_discovery
->reset();
}
}
}