You are here

public function SwaggerUiLibraryDiscovery::libraryDirectory in Swagger UI Field Formatter 8.3

Same name in this branch
  1. 8.3 src/Service/SwaggerUiLibraryDiscovery.php \Drupal\swagger_ui_formatter\Service\SwaggerUiLibraryDiscovery::libraryDirectory()
  2. 8.3 tests/modules/swagger_ui_formatter_test/src/Service/SwaggerUiLibraryDiscovery.php \Drupal\swagger_ui_formatter_test\Service\SwaggerUiLibraryDiscovery::libraryDirectory()

Gets the Swagger UI library directory.

This is a relative path from the DRUPAL_ROOT. No leading slash should be included in the returned path.

Return value

string The path of the Swagger UI library directory relative to DRUPAL_ROOT.

Throws

\Drupal\swagger_ui_formatter\Exception\SwaggerUiLibraryDiscoveryExceptionInterface

Overrides SwaggerUiLibraryDiscoveryInterface::libraryDirectory

1 call to SwaggerUiLibraryDiscovery::libraryDirectory()
SwaggerUiLibraryDiscovery::libraryVersion in src/Service/SwaggerUiLibraryDiscovery.php
Gets the Swagger UI library version.

File

src/Service/SwaggerUiLibraryDiscovery.php, line 102

Class

SwaggerUiLibraryDiscovery
Default Swagger UI library discovery service implementation.

Namespace

Drupal\swagger_ui_formatter\Service

Code

public function libraryDirectory() : string {
  $cache = $this->cache
    ->get(self::LIBRARY_PATH_CID);
  if ($cache) {
    return $cache->data;
  }

  // The default library directory (relative to DRUPAL_ROOT).
  $library_dir = 'libraries/swagger-ui';

  // Allow the default theme to alter the default library directory.
  $default_theme = $this->themeInitialization
    ->getActiveThemeByName($this->themeHandler
    ->getDefault());
  $this->themeInitialization
    ->loadActiveTheme($default_theme);

  // The hook is only invoked for the default theme (and its base themes).
  $this->themeManager
    ->alterForTheme($default_theme, 'swagger_ui_library_directory', $library_dir);

  // Make sure that the directory path is relative (to DRUPAL ROOT).
  $library_dir = ltrim($library_dir, '/');
  $this
    ->validateLibraryDirectory(DRUPAL_ROOT . '/' . $library_dir);

  // Save the library directory to cache so we can save some computation time.
  $this->cache
    ->set(self::LIBRARY_PATH_CID, $library_dir, $this
    ->getCacheMaxAge(), $this
    ->getCacheTags());
  return $library_dir;
}