3 namespace Drupal\libraries\ExternalLibrary\Version;
5 use Drupal\Component\Plugin\Factory\FactoryInterface;
6 use Drupal\libraries\ExternalLibrary\Exception\UnknownLibraryVersionException;
9 * Provides a trait for versioned libraries.
11 * @see \Drupal\libraries\ExternalLibrary\Version\VersionedLibraryInterface
13 trait VersionedLibraryTrait {
16 * The library version.
23 * Information about the version detector to use fo rthis library.
25 * Contains the following keys:
26 * id: The plugin ID of the version detector.
27 * configuration: The plugin configuration of the version detector.
31 protected $versionDetector = [
33 'configuration' => [],
37 * Gets the version of the library.
40 * The version string, for example 1.0, 2.1.4, or 3.0.0-alpha5.
42 * @throws \Drupal\libraries\ExternalLibrary\Exception\UnknownLibraryVersionException
44 * @see \Drupal\libraries\ExternalLibrary\Version\VersionedLibraryInterface::getVersion()
46 public function getVersion() {
47 if (!isset($this->version)) {
48 throw new UnknownLibraryVersionException($this);
50 return $this->version;
54 * Sets the version of the library.
56 * @param string $version
57 * The version of the library.
61 * @see \Drupal\libraries\ExternalLibrary\Version\VersionedLibraryInterface::setVersion()
63 public function setVersion($version) {
64 $this->version = (string) $version;
69 * Gets the version detector of this library using the detector factory.
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.
75 * @param \Drupal\Component\Plugin\Factory\FactoryInterface $detector_factory
77 * @return \Drupal\libraries\ExternalLibrary\Version\VersionDetectorInterface
79 * @see \Drupal\libraries\ExternalLibrary\Version\VersionDetectorInterface
81 public function getVersionDetector(FactoryInterface $detector_factory) {
82 return $detector_factory->createInstance($this->versionDetector['id'], $this->versionDetector['configuration']);