class OpenApiListController in OpenAPI 8
Same name and namespace in other branches
- 8.2 src/Controller/OpenApiListController.php \Drupal\openapi\Controller\OpenApiListController
Lists OpenAPI links.
Hierarchy
- class \Drupal\Core\Controller\ControllerBase implements ContainerInjectionInterface uses LoggerChannelTrait, MessengerTrait, LinkGeneratorTrait, RedirectDestinationTrait, UrlGeneratorTrait, StringTranslationTrait
- class \Drupal\openapi\Controller\OpenApiListController
Expanded class hierarchy of OpenApiListController
File
- src/
Controller/ OpenApiListController.php, line 14
Namespace
Drupal\openapi\ControllerView source
class OpenApiListController extends ControllerBase {
/**
* Current Generator plugin manager.
*
* @var \Drupal\Component\Plugin\PluginManagerInterface
*/
public $openapiGeneratorManager;
/**
* Creates a new OpenApiListController.
*
* @param \Drupal\Component\Plugin\PluginManagerInterface $openapi_generator_manager
* The current openapi generator plugin manager instance.
* @param \Drupal\Component\Plugin\PluginManagerInterface $openapi_ui_manager
* ui library plugin manager instance. NULL if the module is not enabled.
*/
public function __construct(PluginManagerInterface $openapi_generator_manager, PluginManagerInterface $openapi_ui_manager) {
$this->openapiGeneratorManager = $openapi_generator_manager;
$this->openapiUiManager = $openapi_ui_manager;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
$ui_manager = NULL;
$module_handler = $container
->get('module_handler');
if ($module_handler
->moduleExists('openapi_ui')) {
$ui_manager = $container
->get('plugin.manager.openapi_ui.ui');
}
return new static($container
->get('plugin.manager.openapi.generator'), $ui_manager);
}
/**
* List all doc plugins and link to the ui views if they are available.
*/
public function downloadsList() {
$links = [
':openapi_spec' => 'https://github.com/OAI/OpenAPI-Specification/tree/OpenAPI.next',
':swagger_editor' => 'http://editor.swagger.io/',
':swagger_codegen' => 'https://swagger.io/tools/swagger-codegen/',
];
$message = '<p>' . $this
->t("The specifications provide the documentation on some of Drupal's apis following <a href=':openapi_spec'>OpenAPI (aka Swagger)</a> standards.", $links) . ' ';
$message .= $this
->t('These json files can be used in tools such as the <a href=":swagger_editor">Swagger Editor</a> to provide a more detailed version of the API documentation or <a href=":swagger_codegen">Swagger Codegen</a> to create an api client.', $links) . '</p>';
$build['direct_download'] = [
'#type' => 'item',
'#markup' => $message,
];
// Build a table with links to spec files and uis.
$plugins = $this->openapiGeneratorManager
->getDefinitions();
if (count($plugins)) {
$build['documentation'] = [
'#type' => 'table',
'#header' => [
'module' => $this
->t('Module'),
'specification' => $this
->t('Specification'),
],
];
// Construct a message giving user information on docs uis.
$openapi_ui_context = [
':openapi_ui_link' => 'https://drupal.org/project/openapi_ui#libraries',
];
$ui_message = $this
->t("Please visit the <a href=':openapi_ui_link'>OpenAPI UI module</a> for information on these interfaces and to discover others.", $openapi_ui_context) . '</p>';
$ui_plugins = [];
if ($this->openapiUiManager !== NULL && ($ui_plugins = $this->openapiUiManager
->getDefinitions()) && count($ui_plugins)) {
// Add a column for links to the docs uis.
$build['documentation']['#header']['explore'] = $this
->t('Explore') . '*';
$ui_message = '<strong>*</strong> ' . $ui_message;
}
else {
// If we don't have openapi_ui plugins, give the user info on them.
$build['ui']['#title'] = $this
->t('No UI plugins available');
$no_ui_message = $this
->t('There are no plugins available for exploring the OpenAPI documentation.') . ' ';
$no_ui_message = $this
->t('You can install one of the below projects to view the API Specifications from with your site.') . ' ';
$ui_message = $no_ui_message . $ui_message;
}
$build['ui'] = [
'#type' => 'item',
'#markup' => '<p>' . $ui_message . '</p>',
];
$json_format = [
'query' => [
'_format' => 'json',
],
];
$open_api_links = [];
foreach ($plugins as $generator_id => $generator) {
$link_args = [
'openapi_generator' => $generator_id,
];
$link_context = [
'%generator' => $generator['label'],
];
$row = [
'module' => [
'#type' => 'item',
'#markup' => $generator['label'],
],
'specification' => [
'#type' => 'dropbutton',
'#links' => [
[
'title' => $this
->t('View/Download', $link_context),
'url' => Url::fromRoute('openapi.download', $link_args, $json_format),
],
],
],
];
// If there are UI plugins, add them to the table.
if (count($ui_plugins)) {
$row['explore'] = [
'#type' => 'dropbutton',
'#links' => [],
];
// Foreach ui, add a link to view the docs.
foreach ($ui_plugins as $ui_plugin_id => $ui_plugin) {
$interface_args = [
'openapi_generator' => $generator_id,
'openapi_ui' => $ui_plugin_id,
];
$ui_context = [
'%interface' => $ui_plugin['label'],
];
$row['explore']['#links'][$ui_plugin_id] = [
'url' => Url::fromRoute('openapi.documentation', $interface_args),
'title' => $this
->t('Explore with %interface', $ui_context),
];
}
}
// Add row to table.
$build['documentation'][] = $row;
}
}
else {
// If there are no doc plugins, give info on getting a plugin.
$links = [
':rest_link' => 'https://www.drupal.org/docs/8/core/modules/rest',
':jsonapi_link' => 'https://www.drupal.org/project/jsonapi',
];
$no_plugins_message = '<strong>' . $this
->t('No OpenApi generator plugins are currently available.') . '</strong> ';
$no_plugins_message .= $this
->t('You must enable a REST or API module which supports OpenApi Downloads, such as the <a href=":rest_link">Core Rest</a> and <a href=":jsonapi_link">Json API</a> modules.', $links);
$this
->messenger()
->addWarning($no_plugins_message);
}
return $build;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
ControllerBase:: |
protected | property | The configuration factory. | |
ControllerBase:: |
protected | property | The current user service. | 1 |
ControllerBase:: |
protected | property | The entity form builder. | |
ControllerBase:: |
protected | property | The entity manager. | |
ControllerBase:: |
protected | property | The entity type manager. | |
ControllerBase:: |
protected | property | The form builder. | 2 |
ControllerBase:: |
protected | property | The key-value storage. | 1 |
ControllerBase:: |
protected | property | The language manager. | 1 |
ControllerBase:: |
protected | property | The module handler. | 2 |
ControllerBase:: |
protected | property | The state service. | |
ControllerBase:: |
protected | function | Returns the requested cache bin. | |
ControllerBase:: |
protected | function | Retrieves a configuration object. | |
ControllerBase:: |
private | function | Returns the service container. | |
ControllerBase:: |
protected | function | Returns the current user. | 1 |
ControllerBase:: |
protected | function | Retrieves the entity form builder. | |
ControllerBase:: |
protected | function | Retrieves the entity manager service. | |
ControllerBase:: |
protected | function | Retrieves the entity type manager. | |
ControllerBase:: |
protected | function | Returns the form builder service. | 2 |
ControllerBase:: |
protected | function | Returns a key/value storage collection. | 1 |
ControllerBase:: |
protected | function | Returns the language manager service. | 1 |
ControllerBase:: |
protected | function | Returns the module handler. | 2 |
ControllerBase:: |
protected | function |
Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait:: |
|
ControllerBase:: |
protected | function | Returns the state storage service. | |
LinkGeneratorTrait:: |
protected | property | The link generator. | 1 |
LinkGeneratorTrait:: |
protected | function | Returns the link generator. | |
LinkGeneratorTrait:: |
protected | function | Renders a link to a route given a route name and its parameters. | |
LinkGeneratorTrait:: |
public | function | Sets the link generator service. | |
LoggerChannelTrait:: |
protected | property | The logger channel factory service. | |
LoggerChannelTrait:: |
protected | function | Gets the logger for a specific channel. | |
LoggerChannelTrait:: |
public | function | Injects the logger channel factory. | |
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
OpenApiListController:: |
public | property | Current Generator plugin manager. | |
OpenApiListController:: |
public static | function |
Instantiates a new instance of this class. Overrides ControllerBase:: |
|
OpenApiListController:: |
public | function | List all doc plugins and link to the ui views if they are available. | |
OpenApiListController:: |
public | function | Creates a new OpenApiListController. | |
RedirectDestinationTrait:: |
protected | property | The redirect destination service. | 1 |
RedirectDestinationTrait:: |
protected | function | Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url. | |
RedirectDestinationTrait:: |
protected | function | Returns the redirect destination service. | |
RedirectDestinationTrait:: |
public | function | Sets the redirect destination service. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. | |
UrlGeneratorTrait:: |
protected | property | The url generator. | |
UrlGeneratorTrait:: |
protected | function | Returns the URL generator service. | |
UrlGeneratorTrait:: |
public | function | Sets the URL generator service. | |
UrlGeneratorTrait:: |
protected | function | Generates a URL or path for a specific route based on the given parameters. |