You are here

class LibraryDiscoveryCollector in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/lib/Drupal/Core/Asset/LibraryDiscoveryCollector.php \Drupal\Core\Asset\LibraryDiscoveryCollector

A CacheCollector implementation for building library extension info.

Hierarchy

Expanded class hierarchy of LibraryDiscoveryCollector

1 file declares its use of LibraryDiscoveryCollector
LibraryDiscoveryCollectorTest.php in core/tests/Drupal/Tests/Core/Asset/LibraryDiscoveryCollectorTest.php
Contains \Drupal\Tests\Core\Asset\LibraryDiscoveryCollectorTest.
1 string reference to 'LibraryDiscoveryCollector'
core.services.yml in core/core.services.yml
core/core.services.yml
1 service uses LibraryDiscoveryCollector
library.discovery.collector in core/core.services.yml
Drupal\Core\Asset\LibraryDiscoveryCollector

File

core/lib/Drupal/Core/Asset/LibraryDiscoveryCollector.php, line 21
Contains \Drupal\Core\Asset\LibraryDiscoveryCollector.

Namespace

Drupal\Core\Asset
View source
class LibraryDiscoveryCollector extends CacheCollector {

  /**
   * The cache backend.
   *
   * @var \Drupal\Core\Cache\CacheBackendInterface
   */
  protected $cache;

  /**
   * The lock backend.
   *
   * @var \Drupal\Core\Lock\LockBackendInterface
   */
  protected $lock;

  /**
   * The library discovery parser.
   *
   * @var \Drupal\Core\Asset\LibraryDiscoveryParser
   */
  protected $discoveryParser;

  /**
   * The theme manager.
   *
   * @var \Drupal\Core\Theme\ThemeManagerInterface
   */
  protected $themeManager;

  /**
   * Constructs a CacheCollector object.
   *
   * @param string $cid
   *   The cid for the array being cached.
   * @param \Drupal\Core\Cache\CacheBackendInterface $cache
   *   The cache backend.
   * @param \Drupal\Core\Lock\LockBackendInterface $lock
   *   The lock backend.
   * @param \Drupal\Core\Asset\LibraryDiscoveryParser $discovery_parser
   *   The library discovery parser.
   */
  public function __construct(CacheBackendInterface $cache, LockBackendInterface $lock, LibraryDiscoveryParser $discovery_parser, ThemeManagerInterface $theme_manager) {
    $this->themeManager = $theme_manager;
    parent::__construct(NULL, $cache, $lock, [
      'library_info',
    ]);
    $this->discoveryParser = $discovery_parser;
  }

  /**
   * {@inheritdoc}
   */
  protected function getCid() {
    if (!isset($this->cid)) {
      $this->cid = 'library_info:' . $this->themeManager
        ->getActiveTheme()
        ->getName();
    }
    return $this->cid;
  }

  /**
   * {@inheritdoc}
   */
  protected function resolveCacheMiss($key) {
    $this->storage[$key] = $this
      ->getLibraryDefinitions($key);
    $this
      ->persist($key);
    return $this->storage[$key];
  }

  /**
   * Returns the library definitions for a given extension.
   *
   * This also implements libraries-overrides for entire libraries that have
   * been specified by the LibraryDiscoveryParser.
   *
   * @param string $extension
   *   The name of the extension for which library definitions will be returned.
   *
   * @return array
   *   The library definitions for $extension with overrides applied.
   *
   * @throws \Drupal\Core\Asset\Exception\InvalidLibrariesOverrideSpecificationException
   */
  protected function getLibraryDefinitions($extension) {
    $libraries = $this->discoveryParser
      ->buildByExtension($extension);
    foreach ($libraries as $name => $definition) {

      // Handle libraries that are marked for override or removal.
      // @see \Drupal\Core\Asset\LibraryDiscoveryParser::applyLibrariesOverride()
      if (isset($definition['override'])) {
        if ($definition['override'] === FALSE) {

          // Remove the library definition if FALSE is given.
          unset($libraries[$name]);
        }
        else {

          // Otherwise replace with existing library definition if it exists.
          // Throw an exception if it doesn't.
          list($replacement_extension, $replacement_name) = explode('/', $definition['override']);
          $replacement_definition = $this
            ->get($replacement_extension);
          if (isset($replacement_definition[$replacement_name])) {
            $libraries[$name] = $replacement_definition[$replacement_name];
          }
          else {
            throw new InvalidLibrariesOverrideSpecificationException(sprintf('The specified library %s does not exist.', $definition['override']));
          }
        }
      }
      else {

        // If libraries are not overridden, then apply libraries-extend.
        $libraries[$name] = $this
          ->applyLibrariesExtend($extension, $name, $definition);
      }
    }
    return $libraries;
  }

  /**
   * Applies the libraries-extend specified by the active theme.
   *
   * This extends the library definitions with the those specified by the
   * libraries-extend specifications for the active theme.
   *
   * @param string $extension
   *   The name of the extension for which library definitions will be extended.
   * @param string $library_name
   *   The name of the library whose definitions is to be extended.
   * @param $library_definition
   *   The library definition to be extended.
   *
   * @return array
   *   The library definition extended as specified by libraries-extend.
   *
   * @throws \Drupal\Core\Asset\Exception\InvalidLibrariesExtendSpecificationException
   */
  protected function applyLibrariesExtend($extension, $library_name, $library_definition) {
    $libraries_extend = $this->themeManager
      ->getActiveTheme()
      ->getLibrariesExtend();
    if (!empty($libraries_extend["{$extension}/{$library_name}"])) {
      foreach ($libraries_extend["{$extension}/{$library_name}"] as $library_extend_name) {
        if (!is_string($library_extend_name)) {

          // Only string library names are allowed.
          throw new InvalidLibrariesExtendSpecificationException('The libraries-extend specification for each library must be a list of strings.');
        }
        list($new_extension, $new_library_name) = explode('/', $library_extend_name, 2);
        $new_libraries = $this
          ->get($new_extension);
        if (isset($new_libraries[$new_library_name])) {
          $library_definition = NestedArray::mergeDeep($library_definition, $new_libraries[$new_library_name]);
        }
        else {
          throw new InvalidLibrariesExtendSpecificationException(sprintf('The specified library "%s" does not exist.', $library_extend_name));
        }
      }
    }
    return $library_definition;
  }

  /**
   * {@inheritdoc}
   */
  public function reset() {
    parent::reset();
    $this->cid = NULL;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
CacheCollector::$cacheCreated protected property Stores the cache creation time.
CacheCollector::$cacheInvalidated protected property Flag that indicates of the cache has been invalidated.
CacheCollector::$cacheLoaded protected property Indicates if the collected cache was already loaded.
CacheCollector::$cid protected property The cache id that is used for the cache entry.
CacheCollector::$keysToPersist protected property An array of keys to add to the cache on service termination.
CacheCollector::$keysToRemove protected property An array of keys to remove from the cache on service termination.
CacheCollector::$storage protected property Storage for the data itself.
CacheCollector::$tags protected property A list of tags that are used for the cache entry.
CacheCollector::clear public function Clears the collected cache entry. Overrides CacheCollectorInterface::clear 1
CacheCollector::delete public function Deletes the element. Overrides CacheCollectorInterface::delete
CacheCollector::destruct public function Performs destruct operations. Overrides DestructableInterface::destruct
CacheCollector::get public function Gets value from the cache. Overrides CacheCollectorInterface::get 2
CacheCollector::has public function Returns whether data exists for this key. Overrides CacheCollectorInterface::has 1
CacheCollector::invalidateCache protected function Invalidate the cache.
CacheCollector::lazyLoadCache protected function Loads the cache if not already done. 1
CacheCollector::normalizeLockName protected function Normalizes a cache ID in order to comply with database limitations.
CacheCollector::persist protected function Flags an offset value to be written to the persistent cache.
CacheCollector::set public function Implements \Drupal\Core\Cache\CacheCollectorInterface::set(). Overrides CacheCollectorInterface::set 1
CacheCollector::updateCache protected function Writes a value to the persistent cache immediately. 1
LibraryDiscoveryCollector::$cache protected property The cache backend. Overrides CacheCollector::$cache
LibraryDiscoveryCollector::$discoveryParser protected property The library discovery parser.
LibraryDiscoveryCollector::$lock protected property The lock backend. Overrides CacheCollector::$lock
LibraryDiscoveryCollector::$themeManager protected property The theme manager.
LibraryDiscoveryCollector::applyLibrariesExtend protected function Applies the libraries-extend specified by the active theme.
LibraryDiscoveryCollector::getCid protected function Gets the cache ID. Overrides CacheCollector::getCid
LibraryDiscoveryCollector::getLibraryDefinitions protected function Returns the library definitions for a given extension.
LibraryDiscoveryCollector::reset public function Resets the local cache. Overrides CacheCollector::reset
LibraryDiscoveryCollector::resolveCacheMiss protected function Resolves a cache miss. Overrides CacheCollector::resolveCacheMiss
LibraryDiscoveryCollector::__construct public function Constructs a CacheCollector object. Overrides CacheCollector::__construct