You are here

final class SwaggerUiLibraryDiscovery 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
  2. 8.3 tests/modules/swagger_ui_formatter_test/src/Service/SwaggerUiLibraryDiscovery.php \Drupal\swagger_ui_formatter_test\Service\SwaggerUiLibraryDiscovery

Decorator service for testing.

Hierarchy

Expanded class hierarchy of SwaggerUiLibraryDiscovery

1 string reference to 'SwaggerUiLibraryDiscovery'
swagger_ui_formatter_test.services.yml in tests/modules/swagger_ui_formatter_test/swagger_ui_formatter_test.services.yml
tests/modules/swagger_ui_formatter_test/swagger_ui_formatter_test.services.yml
1 service uses SwaggerUiLibraryDiscovery
swagger_ui_formatter_test.swagger_ui_library_discovery in tests/modules/swagger_ui_formatter_test/swagger_ui_formatter_test.services.yml
Drupal\swagger_ui_formatter_test\Service\SwaggerUiLibraryDiscovery

File

tests/modules/swagger_ui_formatter_test/src/Service/SwaggerUiLibraryDiscovery.php, line 18

Namespace

Drupal\swagger_ui_formatter_test\Service
View source
final class SwaggerUiLibraryDiscovery implements SwaggerUiLibraryDiscoveryInterface, CacheableDependencyInterface {

  /**
   * A state key for a boolean value to indicate a missing library path.
   */
  private const STATE_FAKE_MISSING_LIBRARY = 'swagger_ui_formatter_test_fake_missing_library';

  /**
   * A state key for a boolean value to indicate an unsupported library version.
   */
  private const STATE_FAKE_UNSUPPORTED_LIBRARY = 'swagger_ui_formatter_test_fake_unsupported_library';

  /**
   * The decorated service.
   *
   * @var \Drupal\swagger_ui_formatter\Service\SwaggerUiLibraryDiscovery
   */
  private $decorated;

  /**
   * The state service.
   *
   * @var \Drupal\Core\State\StateInterface
   */
  private $state;

  /**
   * The cache tags invalidator service.
   *
   * @var \Drupal\Core\Cache\CacheTagsInvalidatorInterface
   */
  private $cacheTagsInvalidator;

  /**
   * The library discovery.
   *
   * @var \Drupal\Core\Asset\LibraryDiscoveryInterface
   */
  private $libraryDiscovery;

  /**
   * Constructs a new object.
   *
   * @param \Drupal\swagger_ui_formatter\Service\SwaggerUiLibraryDiscoveryInterface $decorated
   *   The decorated service.
   * @param \Drupal\Core\State\StateInterface $state
   *   The state service.
   * @param \Drupal\Core\Cache\CacheTagsInvalidatorInterface $cache_tags_invalidator
   *   The cache tags invalidator service.
   * @param \Drupal\Core\Asset\LibraryDiscoveryInterface $library_discovery
   *   The library discovery.
   */
  public function __construct(SwaggerUiLibraryDiscoveryInterface $decorated, StateInterface $state, CacheTagsInvalidatorInterface $cache_tags_invalidator, LibraryDiscoveryInterface $library_discovery) {
    $this->decorated = $decorated;
    $this->state = $state;
    $this->cacheTagsInvalidator = $cache_tags_invalidator;
    $this->libraryDiscovery = $library_discovery;
  }

  /**
   * {@inheritdoc}
   */
  public function libraryDirectory() : string {
    if ($this->state
      ->get(self::STATE_FAKE_MISSING_LIBRARY)) {
      throw SwaggerUiLibraryDiscoveryException::becauseLibraryDirectoryIsInvalid('fake-missing/swagger-ui');
    }
    return $this->decorated
      ->libraryDirectory();
  }

  /**
   * {@inheritdoc}
   */
  public function libraryVersion() : string {
    if ($this->state
      ->get(self::STATE_FAKE_UNSUPPORTED_LIBRARY)) {
      throw SwaggerUiLibraryDiscoveryException::becauseLibraryVersionIsNotSupported('3.32.1', $this->decorated::MIN_SUPPORTED_LIBRARY_VERSION);
    }
    return $this->decorated
      ->libraryVersion();
  }

  /**
   * Helper function to fake a missing library path.
   *
   * @param bool $state
   *   Indicates whether the missing library path is faked or not.
   *
   * @see libraryDirectory()
   */
  public function fakeMissingLibrary(bool $state) : void {
    $this->state
      ->set(self::STATE_FAKE_MISSING_LIBRARY, $state);
    $this
      ->flushCaches();
  }

  /**
   * Helper function to fake an unsupported library version.
   *
   * @param bool $state
   *   Indicates whether the unsupported library version is faked or not.
   *
   * @see libraryVersion()
   */
  public function fakeUnsupportedLibrary(bool $state) : void {
    $this->state
      ->set(self::STATE_FAKE_UNSUPPORTED_LIBRARY, $state);
    $this
      ->flushCaches();
  }

  /**
   * {@inheritdoc}
   */
  public function getCacheContexts() : array {
    return $this->decorated
      ->getCacheContexts();
  }

  /**
   * {@inheritdoc}
   */
  public function getCacheTags() : array {
    return $this->decorated
      ->getCacheTags();
  }

  /**
   * {@inheritdoc}
   */
  public function getCacheMaxAge() : int {
    return $this->decorated
      ->getCacheMaxAge();
  }

  /**
   * Flushes caches for tests.
   */
  private function flushCaches() : void {

    // Invalidate the cache tags so the library path needs to be re-calculated.
    $this->cacheTagsInvalidator
      ->invalidateTags($this
      ->getCacheTags());

    // DrupalWTF, render cache should be invalidated automatically but it seems
    // there is a hidden static state somewhere...
    // @see \Drupal\Tests\swagger_ui_formatter\FunctionalJavascript\SwaggerUiFieldFormatterTest::validateSwaggerUiErrorMessage()
    // @see \Drupal\Core\Test\RefreshVariablesTrait::refreshVariables()
    if ($this->cacheTagsInvalidator instanceof CacheTagsInvalidator) {
      $this->cacheTagsInvalidator
        ->resetChecksums();
    }

    // Flush the static cache of the used service by the
    // SwaggerUIFormatterTrait, this warrants that valid library
    // gets registered when it is needed.
    $this->libraryDiscovery
      ->clearCachedDefinitions();
  }

}

Members

Namesort descending Modifiers Type Description Overrides
SwaggerUiLibraryDiscovery::$cacheTagsInvalidator private property The cache tags invalidator service.
SwaggerUiLibraryDiscovery::$decorated private property The decorated service.
SwaggerUiLibraryDiscovery::$libraryDiscovery private property The library discovery.
SwaggerUiLibraryDiscovery::$state private property The state service.
SwaggerUiLibraryDiscovery::fakeMissingLibrary public function Helper function to fake a missing library path.
SwaggerUiLibraryDiscovery::fakeUnsupportedLibrary public function Helper function to fake an unsupported library version.
SwaggerUiLibraryDiscovery::flushCaches private function Flushes caches for tests.
SwaggerUiLibraryDiscovery::getCacheContexts public function The cache contexts associated with this object. Overrides CacheableDependencyInterface::getCacheContexts
SwaggerUiLibraryDiscovery::getCacheMaxAge public function The maximum age for which this object may be cached. Overrides CacheableDependencyInterface::getCacheMaxAge
SwaggerUiLibraryDiscovery::getCacheTags public function The cache tags associated with this object. Overrides CacheableDependencyInterface::getCacheTags
SwaggerUiLibraryDiscovery::libraryDirectory public function Gets the Swagger UI library directory. Overrides SwaggerUiLibraryDiscoveryInterface::libraryDirectory
SwaggerUiLibraryDiscovery::libraryVersion public function Gets the Swagger UI library version. Overrides SwaggerUiLibraryDiscoveryInterface::libraryVersion
SwaggerUiLibraryDiscovery::STATE_FAKE_MISSING_LIBRARY private constant A state key for a boolean value to indicate a missing library path.
SwaggerUiLibraryDiscovery::STATE_FAKE_UNSUPPORTED_LIBRARY private constant A state key for a boolean value to indicate an unsupported library version.
SwaggerUiLibraryDiscovery::__construct public function Constructs a new object.