class IconCollector

A CacheCollector implementation for building icons info.

Hierarchy

Expanded class hierarchy of IconCollector

1 file declares its use of IconCollector
IconPackManager.php in core/lib/Drupal/Core/Theme/Icon/Plugin/IconPackManager.php

File

core/lib/Drupal/Core/Theme/Icon/IconCollector.php, line 14

Namespace

Drupal\Core\Theme\Icon
View source
class IconCollector extends CacheCollector {
  
  /**
   * Constructs a IconCollector instance.
   *
   * @param \Drupal\Core\Theme\Icon\IconExtractorPluginManager $iconPackExtractorManager
   *   The icon plugin extractor service.
   * @param \Drupal\Core\Cache\CacheBackendInterface $cache
   *   The cache backend.
   * @param \Drupal\Core\Lock\LockBackendInterface $lock
   *   The lock backend.
   */
  public function __construct(protected readonly IconExtractorPluginManager $iconPackExtractorManager, CacheBackendInterface $cache, LockBackendInterface $lock) {
    parent::__construct('icon_info', $cache, $lock, [
      'icon_pack_collector',
    ]);
  }
  
  /**
   * {@inheritdoc}
   */
  public function set($key, $value) : void {
    $this->lazyLoadCache();
    $this->storage[$key] = $value;
    $this->persist($key);
    // Chances are very small but the key might have been marked for deletion.
    unset($this->keysToRemove[$key]);
  }
  
  /**
   * {@inheritdoc}
   */
  public function get($key, array $definition = []) : ?IconDefinitionInterface {
    $this->lazyLoadCache();
    if (\array_key_exists($key, $this->storage)) {
      return $this->storage[$key];
    }
    else {
      return $this->resolveCacheMiss($key, $definition);
    }
  }
  
  /**
   * {@inheritdoc}
   */
  public function resolveCacheMiss($key, array $definition = []) : ?IconDefinitionInterface {
    $icon = $this->getIconFromExtractor($key, $definition);
    $this->storage[$key] = $icon;
    $this->persist($key);
    return $icon;
  }
  
  /**
   * Returns the icon from an icon id and icon pack definition.
   *
   * @param string $icon_full_id
   *   The icon full id as pack_id:icon_id.
   * @param array $definitions
   *   The icon pack definitions.
   *
   * @return \Drupal\Core\Theme\Icon\IconDefinitionInterface|null
   *   The icon loaded.
   */
  private function getIconFromExtractor(string $icon_full_id, array $definitions) : ?IconDefinitionInterface {
    $icon_data = IconDefinition::getIconDataFromId($icon_full_id);
    if (!isset($icon_data['pack_id'])) {
      return NULL;
    }
    $definition = $definitions[$icon_data['pack_id']] ?? NULL;
    if (NULL === $definition) {
      return NULL;
    }
    $icon_definition = $definition['icons'][$icon_full_id] ?? NULL;
    if (NULL === $icon_definition) {
      return NULL;
    }
    $icon_definition['icon_id'] = $icon_data['icon_id'];
    // Clean to data to reduce the array passed to createInstance().
    unset($definition['icons']);
    /** @var \Drupal\Core\Theme\Icon\IconExtractorInterface $extractor */
    $extractor = $this->iconPackExtractorManager
      ->createInstance($definition['extractor'], $definition);
    return $extractor->loadIcon($icon_definition);
  }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title Overrides
CacheCollector::$cache protected property The cache backend that should be used. 1
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::$lock protected property The lock backend that should be used. 1
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 1
CacheCollector::destruct public function Performs destruct operations. Overrides DestructableInterface::destruct
CacheCollector::getCid protected function Gets the cache ID. 3
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::persist protected function Flags an offset value to be written to the persistent cache.
CacheCollector::reset public function Resets the local cache. Overrides CacheCollectorInterface::reset 1
CacheCollector::updateCache protected function Writes a value to the persistent cache immediately. 1
IconCollector::get public function Gets value from the cache. Overrides CacheCollector::get
IconCollector::getIconFromExtractor private function Returns the icon from an icon id and icon pack definition.
IconCollector::resolveCacheMiss public function Resolves a cache miss. Overrides CacheCollector::resolveCacheMiss
IconCollector::set public function Implements \Drupal\Core\Cache\CacheCollectorInterface::set(). Overrides CacheCollector::set
IconCollector::__construct public function Constructs a IconCollector instance. Overrides CacheCollector::__construct

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.