3 namespace Drupal\libraries\ExternalLibrary\Definition;
5 use Drupal\Component\Serialization\SerializationInterface;
6 use Drupal\libraries\ExternalLibrary\Exception\LibraryDefinitionNotFoundException;
9 * Provides a base implementation for file-based definition discoveries.
11 * This discovery assumes that library files contain the serialized library
12 * definition and are accessible under a common base URI. The expected library
13 * file URI will be constructed from this by appending '/$id.$extension' to
14 * this, where $id is the library ID and $extension is the serializer extension.
16 abstract class FileDefinitionDiscoveryBase implements DefinitionDiscoveryInterface {
19 * The serializer for the library definition files.
21 * @var \Drupal\Component\Serialization\SerializationInterface
23 protected $serializer;
26 * The base URI for the library files.
33 * Constructs a stream-based library definition discovery.
35 * @param \Drupal\Component\Serialization\SerializationInterface $serializer
36 * The serializer for the library definition files.
37 * @param string $base_uri
38 * The base URI for the library files.
40 public function __construct(SerializationInterface $serializer, $base_uri) {
41 $this->serializer = $serializer;
42 $this->baseUri = $base_uri;
48 public function getDefinition($id) {
49 if (!$this->hasDefinition($id)) {
50 throw new LibraryDefinitionNotFoundException($id);
52 return $this->serializer->decode($this->getSerializedDefinition($id));
56 * Gets the contents of the library file.
59 * The library ID to retrieve the serialized definition for.
62 * The serialized library definition.
64 * @throws \Drupal\libraries\ExternalLibrary\Exception\LibraryDefinitionNotFoundException
66 abstract protected function getSerializedDefinition($id);
69 * Returns the file URI of the library definition file for a given library ID.
72 * The ID of the external library.
75 * The file URI of the file the library definition resides in.
77 protected function getFileUri($id) {
78 $filename = $id . '.' . $this->serializer->getFileExtension();
79 return "$this->baseUri/$filename";