Further modules included.
[yaffs-website] / web / modules / contrib / libraries / src / ExternalLibrary / Version / VersionedLibraryTrait.php
1 <?php
2
3 namespace Drupal\libraries\ExternalLibrary\Version;
4
5 use Drupal\Component\Plugin\Factory\FactoryInterface;
6 use Drupal\libraries\ExternalLibrary\Exception\UnknownLibraryVersionException;
7
8 /**
9  * Provides a trait for versioned libraries.
10  *
11  * @see \Drupal\libraries\ExternalLibrary\Version\VersionedLibraryInterface
12  */
13 trait VersionedLibraryTrait {
14
15   /**
16    * The library version.
17    *
18    * @var string
19    */
20   protected $version;
21
22   /**
23    * Information about the version detector to use fo rthis library.
24    *
25    * Contains the following keys:
26    * id: The plugin ID of the version detector.
27    * configuration: The plugin configuration of the version detector.
28    *
29    * @var array
30    */
31   protected $versionDetector = [
32     'id' => NULL,
33     'configuration' => [],
34   ];
35
36   /**
37    * Gets the version of the library.
38    *
39    * @return string
40    *   The version string, for example 1.0, 2.1.4, or 3.0.0-alpha5.
41    *
42    * @throws \Drupal\libraries\ExternalLibrary\Exception\UnknownLibraryVersionException
43    *
44    * @see \Drupal\libraries\ExternalLibrary\Version\VersionedLibraryInterface::getVersion()
45    */
46   public function getVersion() {
47     if (!isset($this->version)) {
48       throw new UnknownLibraryVersionException($this);
49     }
50     return $this->version;
51   }
52
53   /**
54    * Sets the version of the library.
55    *
56    * @param string $version
57    *   The version of the library.
58    *
59    * @return $this
60    *
61    * @see \Drupal\libraries\ExternalLibrary\Version\VersionedLibraryInterface::setVersion()
62    */
63   public function setVersion($version) {
64     $this->version = (string) $version;
65     return $this;
66   }
67
68   /**
69    * Gets the version detector of this library using the detector factory.
70    *
71    * Because determining the installation version of a library is not specific
72    * to any library or even any library type, this logic is offloaded to
73    * separate detector objects.
74    *
75    * @param \Drupal\Component\Plugin\Factory\FactoryInterface $detector_factory
76    *
77    * @return \Drupal\libraries\ExternalLibrary\Version\VersionDetectorInterface
78    *
79    * @see \Drupal\libraries\ExternalLibrary\Version\VersionDetectorInterface
80    */
81   public function getVersionDetector(FactoryInterface $detector_factory) {
82     return $detector_factory->createInstance($this->versionDetector['id'], $this->versionDetector['configuration']);
83   }
84
85 }