You are here

abstract class FileDefinitionDiscoveryBase in Libraries API 8.3

Provides a base implementation for file-based definition discoveries.

This discovery assumes that library files contain the serialized library definition and are accessible under a common base URI. The expected library file URI will be constructed from this by appending '/$id.$extension' to this, where $id is the library ID and $extension is the serializer extension.

Hierarchy

Expanded class hierarchy of FileDefinitionDiscoveryBase

File

src/ExternalLibrary/Definition/FileDefinitionDiscoveryBase.php, line 16

Namespace

Drupal\libraries\ExternalLibrary\Definition
View source
abstract class FileDefinitionDiscoveryBase implements DefinitionDiscoveryInterface {

  /**
   * The serializer for the library definition files.
   *
   * @var \Drupal\Component\Serialization\SerializationInterface
   */
  protected $serializer;

  /**
   * The base URI for the library files.
   *
   * @var string
   */
  protected $baseUri;

  /**
   * Constructs a stream-based library definition discovery.
   *
   * @param \Drupal\Component\Serialization\SerializationInterface $serializer
   *   The serializer for the library definition files.
   * @param string $base_uri
   *   The base URI for the library files.
   */
  public function __construct(SerializationInterface $serializer, $base_uri) {
    $this->serializer = $serializer;
    $this->baseUri = $base_uri;
  }

  /**
   * {@inheritdoc}
   */
  public function getDefinition($id) {
    if (!$this
      ->hasDefinition($id)) {
      throw new LibraryDefinitionNotFoundException($id);
    }
    return $this->serializer
      ->decode($this
      ->getSerializedDefinition($id));
  }

  /**
   * Gets the contents of the library file.
   *
   * @param $id
   *   The library ID to retrieve the serialized definition for.
   *
   * @return string
   *   The serialized library definition.
   *
   * @throws \Drupal\libraries\ExternalLibrary\Exception\LibraryDefinitionNotFoundException
   */
  protected abstract function getSerializedDefinition($id);

  /**
   * Returns the file URI of the library definition file for a given library ID.
   *
   * @param $id
   *   The ID of the external library.
   *
   * @return string
   *   The file URI of the file the library definition resides in.
   */
  protected function getFileUri($id) {
    $filename = $id . '.' . $this->serializer
      ->getFileExtension();
    return "{$this->baseUri}/{$filename}";
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DefinitionDiscoveryInterface::hasDefinition public function Checks whether a library definition exists. 3
FileDefinitionDiscoveryBase::$baseUri protected property The base URI for the library files.
FileDefinitionDiscoveryBase::$serializer protected property The serializer for the library definition files.
FileDefinitionDiscoveryBase::getDefinition public function Gets a library definition by its ID. Overrides DefinitionDiscoveryInterface::getDefinition
FileDefinitionDiscoveryBase::getFileUri protected function Returns the file URI of the library definition file for a given library ID.
FileDefinitionDiscoveryBase::getSerializedDefinition abstract protected function Gets the contents of the library file. 2
FileDefinitionDiscoveryBase::__construct public function Constructs a stream-based library definition discovery. 1