Further modules included.
[yaffs-website] / web / modules / contrib / libraries / src / ExternalLibrary / Local / LocalLibraryTrait.php
1 <?php
2
3 namespace Drupal\libraries\ExternalLibrary\Local;
4 use Drupal\libraries\ExternalLibrary\Exception\LibraryNotInstalledException;
5
6 /**
7  * Provides a trait for local libraries utilizing a stream wrapper.
8  *
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
13  *
14  * This trait should only be used by classes implementing LocalLibraryInterface.
15  *
16  * @see \Drupal\libraries\ExternalLibrary\Local\LocalLibraryInterface
17  */
18 trait LocalLibraryTrait {
19
20   /**
21    * Whether or not the library is installed.
22    *
23    * A library being installed means its files can be found on the filesystem.
24    *
25    * @var bool
26    */
27   protected $installed = FALSE;
28
29   /**
30    * The local path to the library relative to the app root.
31    *
32    * @var string
33    */
34   protected $localPath;
35
36   /**
37    * Checks whether the library is installed.
38    *
39    * @return bool
40    *   TRUE if the library is installed; FALSE otherwise;
41    *
42    * @see \Drupal\libraries\ExternalLibrary\Local\LocalLibraryInterface::isInstalled()
43    */
44   public function isInstalled() {
45     return $this->installed;
46   }
47
48   /**
49    * Marks the library as uninstalled.
50    *
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.
54    *
55    * @return $this
56    *
57    * @see \Drupal\libraries\ExternalLibrary\Local\LocalLibraryInterface::setUninstalled()
58    */
59   public function setUninstalled() {
60     $this->installed = FALSE;
61     return $this;
62   }
63
64   /**
65    * Gets the path to the library.
66    *
67    * @return string
68    *   The path to the library relative to the app root.
69    *
70    * @throws \Drupal\libraries\ExternalLibrary\Exception\LibraryNotInstalledException
71    *
72    * @see \Drupal\libraries\ExternalLibrary\Local\LocalLibraryInterface::getLocalPath()
73    */
74   public function getLocalPath() {
75     if (!$this->isInstalled()) {
76       /** @var \Drupal\libraries\ExternalLibrary\Local\LocalLibraryInterface $this */
77       throw new LibraryNotInstalledException($this);
78     }
79
80     return $this->localPath;
81   }
82
83   /**
84    * Sets the library path of the library.
85    *
86    * @param string $path
87    *   The path to the library.
88    *
89    * @see \Drupal\libraries\ExternalLibrary\Local\LocalLibraryInterface::getLocalPath()
90    */
91   public function setLocalPath($path) {
92     $this->installed = TRUE;
93     $this->localPath = (string) $path;
94
95     assert('$this->localPath !== ""');
96     assert('$this->localPath[0] !== "/"');
97   }
98
99 }