You are here

class OpenApiListController in OpenAPI 8

Same name and namespace in other branches
  1. 8.2 src/Controller/OpenApiListController.php \Drupal\openapi\Controller\OpenApiListController

Lists OpenAPI links.

Hierarchy

Expanded class hierarchy of OpenApiListController

File

src/Controller/OpenApiListController.php, line 14

Namespace

Drupal\openapi\Controller
View 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

Namesort descending Modifiers Type Description Overrides
ControllerBase::$configFactory protected property The configuration factory.
ControllerBase::$currentUser protected property The current user service. 1
ControllerBase::$entityFormBuilder protected property The entity form builder.
ControllerBase::$entityManager protected property The entity manager.
ControllerBase::$entityTypeManager protected property The entity type manager.
ControllerBase::$formBuilder protected property The form builder. 2
ControllerBase::$keyValue protected property The key-value storage. 1
ControllerBase::$languageManager protected property The language manager. 1
ControllerBase::$moduleHandler protected property The module handler. 2
ControllerBase::$stateService protected property The state service.
ControllerBase::cache protected function Returns the requested cache bin.
ControllerBase::config protected function Retrieves a configuration object.
ControllerBase::container private function Returns the service container.
ControllerBase::currentUser protected function Returns the current user. 1
ControllerBase::entityFormBuilder protected function Retrieves the entity form builder.
ControllerBase::entityManager Deprecated protected function Retrieves the entity manager service.
ControllerBase::entityTypeManager protected function Retrieves the entity type manager.
ControllerBase::formBuilder protected function Returns the form builder service. 2
ControllerBase::keyValue protected function Returns a key/value storage collection. 1
ControllerBase::languageManager protected function Returns the language manager service. 1
ControllerBase::moduleHandler protected function Returns the module handler. 2
ControllerBase::redirect protected function Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait::redirect
ControllerBase::state protected function Returns the state storage service.
LinkGeneratorTrait::$linkGenerator protected property The link generator. 1
LinkGeneratorTrait::getLinkGenerator Deprecated protected function Returns the link generator.
LinkGeneratorTrait::l Deprecated protected function Renders a link to a route given a route name and its parameters.
LinkGeneratorTrait::setLinkGenerator Deprecated public function Sets the link generator service.
LoggerChannelTrait::$loggerFactory protected property The logger channel factory service.
LoggerChannelTrait::getLogger protected function Gets the logger for a specific channel.
LoggerChannelTrait::setLoggerFactory public function Injects the logger channel factory.
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
OpenApiListController::$openapiGeneratorManager public property Current Generator plugin manager.
OpenApiListController::create public static function Instantiates a new instance of this class. Overrides ControllerBase::create
OpenApiListController::downloadsList public function List all doc plugins and link to the ui views if they are available.
OpenApiListController::__construct public function Creates a new OpenApiListController.
RedirectDestinationTrait::$redirectDestination protected property The redirect destination service. 1
RedirectDestinationTrait::getDestinationArray protected function Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url.
RedirectDestinationTrait::getRedirectDestination protected function Returns the redirect destination service.
RedirectDestinationTrait::setRedirectDestination public function Sets the redirect destination service.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
UrlGeneratorTrait::$urlGenerator protected property The url generator.
UrlGeneratorTrait::getUrlGenerator Deprecated protected function Returns the URL generator service.
UrlGeneratorTrait::setUrlGenerator Deprecated public function Sets the URL generator service.
UrlGeneratorTrait::url Deprecated protected function Generates a URL or path for a specific route based on the given parameters.