You are here

public function SwaggerUiLibraryDiscovery::libraryVersion 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::libraryVersion()
  2. 8.3 tests/modules/swagger_ui_formatter_test/src/Service/SwaggerUiLibraryDiscovery.php \Drupal\swagger_ui_formatter_test\Service\SwaggerUiLibraryDiscovery::libraryVersion()

Gets the Swagger UI library version.

Return value

string The Swagger UI library version.

Throws

\Drupal\swagger_ui_formatter\Exception\SwaggerUiLibraryDiscoveryExceptionInterface

Overrides SwaggerUiLibraryDiscoveryInterface::libraryVersion

File

src/Service/SwaggerUiLibraryDiscovery.php, line 125

Class

SwaggerUiLibraryDiscovery
Default Swagger UI library discovery service implementation.

Namespace

Drupal\swagger_ui_formatter\Service

Code

public function libraryVersion() : string {
  $library_dir = $this
    ->libraryDirectory();
  $package_json_path = DRUPAL_ROOT . '/' . $library_dir . '/package.json';
  $package_json_content = file_get_contents($package_json_path);
  if (!$package_json_content) {
    throw SwaggerUiLibraryDiscoveryException::becauseCannotReadPackageJsonContent($package_json_path);
  }
  $data = Json::decode($package_json_content);
  if (json_last_error() !== JSON_ERROR_NONE) {
    throw SwaggerUiLibraryDiscoveryException::becausePackageJsonCannotBeDecoded($package_json_path, json_last_error_msg());
  }
  if (!isset($data['version'])) {
    throw SwaggerUiLibraryDiscoveryException::becauseUnableToIdentifyLibraryVersion($package_json_path);
  }
  if (version_compare($data['version'], self::MIN_SUPPORTED_LIBRARY_VERSION, '<')) {
    throw SwaggerUiLibraryDiscoveryException::becauseLibraryVersionIsNotSupported($data['version'], self::MIN_SUPPORTED_LIBRARY_VERSION);
  }
  return $data['version'];
}