You are here

trait LocalLibraryTrait in Libraries API 8.3

Provides a trait for local libraries utilizing a stream wrapper.

It assumes that the library files can be accessed using a specified stream wrapper and that the first component of the file URIs are the library IDs. Thus, file URIs are of the form: stream-wrapper-scheme://library-id/path/to/file/within/the/library/filename

This trait should only be used by classes implementing LocalLibraryInterface.

Hierarchy

See also

\Drupal\libraries\ExternalLibrary\Local\LocalLibraryInterface

3 files declare their use of LocalLibraryTrait
AssetLibrary.php in src/ExternalLibrary/Asset/AssetLibrary.php
MultipleAssetLibrary.php in src/ExternalLibrary/Asset/MultipleAssetLibrary.php
PhpFileLibrary.php in src/ExternalLibrary/PhpFile/PhpFileLibrary.php

File

src/ExternalLibrary/Local/LocalLibraryTrait.php, line 18

Namespace

Drupal\libraries\ExternalLibrary\Local
View source
trait LocalLibraryTrait {

  /**
   * Whether or not the library is installed.
   *
   * A library being installed means its files can be found on the filesystem.
   *
   * @var bool
   */
  protected $installed = FALSE;

  /**
   * The local path to the library relative to the app root.
   *
   * @var string
   */
  protected $localPath;

  /**
   * Checks whether the library is installed.
   *
   * @return bool
   *   TRUE if the library is installed; FALSE otherwise;
   *
   * @see \Drupal\libraries\ExternalLibrary\Local\LocalLibraryInterface::isInstalled()
   */
  public function isInstalled() {
    return $this->installed;
  }

  /**
   * Marks the library as uninstalled.
   *
   * A corresponding method to mark the library as installed is not provided as
   * an installed library should have a library path, so that
   * LocalLibraryInterface::setLibraryPath() can be used instead.
   *
   * @return $this
   *
   * @see \Drupal\libraries\ExternalLibrary\Local\LocalLibraryInterface::setUninstalled()
   */
  public function setUninstalled() {
    $this->installed = FALSE;
    return $this;
  }

  /**
   * Gets the path to the library.
   *
   * @return string
   *   The path to the library relative to the app root.
   *
   * @throws \Drupal\libraries\ExternalLibrary\Exception\LibraryNotInstalledException
   *
   * @see \Drupal\libraries\ExternalLibrary\Local\LocalLibraryInterface::getLocalPath()
   */
  public function getLocalPath() {
    if (!$this
      ->isInstalled()) {

      /** @var \Drupal\libraries\ExternalLibrary\Local\LocalLibraryInterface $this */
      throw new LibraryNotInstalledException($this);
    }
    return $this->localPath;
  }

  /**
   * Sets the library path of the library.
   *
   * @param string $path
   *   The path to the library.
   *
   * @see \Drupal\libraries\ExternalLibrary\Local\LocalLibraryInterface::getLocalPath()
   */
  public function setLocalPath($path) {
    $this->installed = TRUE;
    $this->localPath = (string) $path;
    assert($this->localPath !== "");
    assert($this->localPath[0] !== "/");
  }

}

Members

Namesort descending Modifiers Type Description Overrides
LocalLibraryTrait::$installed protected property Whether or not the library is installed.
LocalLibraryTrait::$localPath protected property The local path to the library relative to the app root.
LocalLibraryTrait::getLocalPath public function Gets the path to the library.
LocalLibraryTrait::isInstalled public function Checks whether the library is installed.
LocalLibraryTrait::setLocalPath public function Sets the library path of the library.
LocalLibraryTrait::setUninstalled public function Marks the library as uninstalled.