Further modules included.
[yaffs-website] / web / modules / contrib / libraries / src / ExternalLibrary / Definition / FileDefinitionDiscoveryBase.php
1 <?php
2
3 namespace Drupal\libraries\ExternalLibrary\Definition;
4
5 use Drupal\Component\Serialization\SerializationInterface;
6 use Drupal\libraries\ExternalLibrary\Exception\LibraryDefinitionNotFoundException;
7
8 /**
9  * Provides a base implementation for file-based definition discoveries.
10  *
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.
15  */
16 abstract class FileDefinitionDiscoveryBase implements DefinitionDiscoveryInterface {
17
18   /**
19    * The serializer for the library definition files.
20    *
21    * @var \Drupal\Component\Serialization\SerializationInterface
22    */
23   protected $serializer;
24
25   /**
26    * The base URI for the library files.
27    *
28    * @var string
29    */
30   protected $baseUri;
31
32   /**
33    * Constructs a stream-based library definition discovery.
34    *
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.
39    */
40   public function __construct(SerializationInterface $serializer, $base_uri) {
41     $this->serializer = $serializer;
42     $this->baseUri = $base_uri;
43   }
44
45   /**
46    * {@inheritdoc}
47    */
48   public function getDefinition($id) {
49     if (!$this->hasDefinition($id)) {
50       throw new LibraryDefinitionNotFoundException($id);
51     }
52     return $this->serializer->decode($this->getSerializedDefinition($id));
53   }
54
55   /**
56    * Gets the contents of the library file.
57    *
58    * @param $id
59    *   The library ID to retrieve the serialized definition for.
60    *
61    * @return string
62    *   The serialized library definition.
63    *
64    * @throws \Drupal\libraries\ExternalLibrary\Exception\LibraryDefinitionNotFoundException
65    */
66   abstract protected function getSerializedDefinition($id);
67
68   /**
69    * Returns the file URI of the library definition file for a given library ID.
70    *
71    * @param $id
72    *   The ID of the external library.
73    *
74    * @return string
75    *   The file URI of the file the library definition resides in.
76    */
77   protected function getFileUri($id) {
78     $filename = $id . '.' . $this->serializer->getFileExtension();
79     return "$this->baseUri/$filename";
80   }
81
82 }