-
Notifications
You must be signed in to change notification settings - Fork 13
/
swagger_ui_formatter.module
123 lines (113 loc) · 4.2 KB
/
swagger_ui_formatter.module
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
<?php
/**
* @file
* Main module file for Swagger UI Field Formatter.
*/
declare(strict_types = 1);
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\swagger_ui_formatter\Exception\SwaggerUiLibraryDiscoveryExceptionInterface;
/**
* Implements hook_theme().
*/
function swagger_ui_formatter_theme(): array {
return [
'swagger_ui_field_item' => [
'variables' => [
'field_name' => NULL,
'delta' => NULL,
],
],
];
}
/**
* Implements hook_library_info_build().
*/
function swagger_ui_formatter_library_info_build(): array {
$libraries = [];
/** @var \Drupal\swagger_ui_formatter\Service\SwaggerUiLibraryDiscoveryInterface $swagger_ui_library_discovery */
$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;
}
// Add a leading slash to the library directory path to indicate in
// $libraries[] that it's relative to DRUPAL_ROOT. Otherwise, it's considered
// as a relative path from the current module.
$library_dir = '/' . $library_dir;
// Library definition for the required Swagger UI files.
$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],
],
];
// Library definition for the Swagger UI integration files.
$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;
}
/**
* Implements hook_help().
*/
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 the Markdown module is installed, use it to render the README.
if ($readme && (\Drupal::moduleHandler()->moduleExists('markdown') === TRUE)) {
$filter_manager = \Drupal::service('plugin.manager.filter');
$settings = \Drupal::configFactory()->get('markdown.settings')->getRawData();
/** @var \Drupal\filter\Plugin\FilterInterface $filter */
$filter = $filter_manager->createInstance('markdown', ['settings' => $settings]);
$output = $filter->process($readme, 'en');
}
// Else the Markdown module is not installed output the README as text.
elseif ($readme) {
$output = '<pre>' . $readme . '</pre>';
}
// Add a link to the Drupal.org project.
$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;
}
/**
* Implements hook_cache_flush().
*/
function swagger_ui_formatter_cache_flush(): void {
// The library discovery service might not exist in certain situations (like
// when someone upgrades the module from 8.x-2.x) so we check its existence.
// @todo The condition can be removed in 4.x.
$service_name = 'swagger_ui_formatter.swagger_ui_library_discovery';
if (\Drupal::hasService($service_name)) {
/** @var \Drupal\swagger_ui_formatter\Service\SwaggerUiLibraryDiscoveryInterface $swagger_ui_library_discovery */
$swagger_ui_library_discovery = \Drupal::service($service_name);
if (method_exists($swagger_ui_library_discovery, 'reset')) {
$swagger_ui_library_discovery->reset();
}
}
}