3 namespace Drupal\libraries\ExternalLibrary\Local;
4 use Drupal\libraries\ExternalLibrary\Exception\LibraryNotInstalledException;
7 * Provides a trait for local libraries utilizing a stream wrapper.
9 * It assumes that the library files can be accessed using a specified stream
10 * wrapper and that the first component of the file URIs are the library IDs.
11 * Thus, file URIs are of the form:
12 * stream-wrapper-scheme://library-id/path/to/file/within/the/library/filename
14 * This trait should only be used by classes implementing LocalLibraryInterface.
16 * @see \Drupal\libraries\ExternalLibrary\Local\LocalLibraryInterface
18 trait LocalLibraryTrait {
21 * Whether or not the library is installed.
23 * A library being installed means its files can be found on the filesystem.
27 protected $installed = FALSE;
30 * The local path to the library relative to the app root.
37 * Checks whether the library is installed.
40 * TRUE if the library is installed; FALSE otherwise;
42 * @see \Drupal\libraries\ExternalLibrary\Local\LocalLibraryInterface::isInstalled()
44 public function isInstalled() {
45 return $this->installed;
49 * Marks the library as uninstalled.
51 * A corresponding method to mark the library as installed is not provided as
52 * an installed library should have a library path, so that
53 * LocalLibraryInterface::setLibraryPath() can be used instead.
57 * @see \Drupal\libraries\ExternalLibrary\Local\LocalLibraryInterface::setUninstalled()
59 public function setUninstalled() {
60 $this->installed = FALSE;
65 * Gets the path to the library.
68 * The path to the library relative to the app root.
70 * @throws \Drupal\libraries\ExternalLibrary\Exception\LibraryNotInstalledException
72 * @see \Drupal\libraries\ExternalLibrary\Local\LocalLibraryInterface::getLocalPath()
74 public function getLocalPath() {
75 if (!$this->isInstalled()) {
76 /** @var \Drupal\libraries\ExternalLibrary\Local\LocalLibraryInterface $this */
77 throw new LibraryNotInstalledException($this);
80 return $this->localPath;
84 * Sets the library path of the library.
87 * The path to the library.
89 * @see \Drupal\libraries\ExternalLibrary\Local\LocalLibraryInterface::getLocalPath()
91 public function setLocalPath($path) {
92 $this->installed = TRUE;
93 $this->localPath = (string) $path;
95 assert('$this->localPath !== ""');
96 assert('$this->localPath[0] !== "/"');