You are here

class MiconIconManager in Micon 2.x

Same name and namespace in other branches
  1. 8 src/MiconIconManager.php \Drupal\micon\MiconIconManager

Class MiconIconManager.

@package Drupal\micon

Hierarchy

Expanded class hierarchy of MiconIconManager

1 file declares its use of MiconIconManager
MiconCommands.php in src/Commands/MiconCommands.php
1 string reference to 'MiconIconManager'
micon.services.yml in ./micon.services.yml
micon.services.yml
1 service uses MiconIconManager
micon.icon.manager in ./micon.services.yml
Drupal\micon\MiconIconManager

File

src/MiconIconManager.php, line 17

Namespace

Drupal\micon
View source
class MiconIconManager {
  use UseCacheBackendTrait;

  /**
   * Drupal\Core\Entity\EntityTypeManager definition.
   *
   * @var \Drupal\Core\Entity\EntityTypeManager
   */
  protected $entityManager;

  /**
   * Cached icons array.
   *
   * @var array
   */
  protected $icons;

  /**
   * Constructor.
   */
  public function __construct(EntityTypeManagerInterface $entity_manager, CacheBackendInterface $cache_backend) {
    $this->entityManager = $entity_manager;
    $this
      ->setCacheBackend($cache_backend, 'micon.icons', [
      'micon.icons',
    ]);
  }

  /**
   * Match an icon_id against the Micon package icon definitions.
   *
   * @param string $icon_id
   *   The icon id as specified within the IcoMoon selection.json file.
   *
   * @return \Drupal\micon\MiconIcon|null
   *   The found MiconIcon.
   */
  public function getIconMatch($icon_id) {
    $icons = $this
      ->getFlattenedIcons();
    return isset($icons[$icon_id]) ? $icons[$icon_id] : NULL;
  }

  /**
   * Get all available icons.
   *
   * @return array
   *   Nested list of \Drupal\micon\Entity\Micon entities grouped by
   *   package id.
   */
  public function getIcons() {
    $icons = $this
      ->getCachedDefinitions();
    if (!isset($icons)) {
      $icons = $this
        ->loadIcons();
      $this
        ->setCachedPackages($icons);
    }
    return $icons;
  }

  /**
   * Get a flat list of all icons.
   *
   * @return array
   *   Flat list of \Drupal\micon\Entity\Micon entities.
   */
  public function getFlattenedIcons() {
    $icons = [];
    foreach ($this
      ->getIcons() as $package_icons) {
      $icons += $package_icons;
    }
    return $icons;
  }

  /**
   * Load all available micon icons.
   *
   * @return array
   *   List of \Drupal\micon\Entity\Micon entities to store in cache.
   */
  protected function loadIcons() {
    $definitions = [];
    foreach (Micon::loadActive() as $micon) {
      $definitions[$micon
        ->id()] = $micon
        ->getIcons();
    }
    return $definitions;
  }

  /**
   * Initialize the cache backend.
   *
   * Plugin icons are cached using the provided cache backend. The
   * interface language is added as a suffix to the cache key.
   *
   * @param \Drupal\Core\Cache\CacheBackendInterface $cache_backend
   *   Cache backend instance to use.
   * @param string $cache_key
   *   Cache key prefix to use, the language code will be appended
   *   automatically.
   * @param array $cache_tags
   *   (optional) When providing a list of cache tags, the cached Micon
   *   icons are tagged with the provided cache tags. These cache tags can
   *   then be used to clear the corresponding cached Micon icons. Note
   *   that this should be used with care! For clearing all cached Micon
   *   icons of a Micon manager, call that Micon manager's
   *   clearCachedDefinitions() method. Only use cache tags when cached Micon
   *   icons should be cleared along with other, related cache entries.
   */
  public function setCacheBackend(CacheBackendInterface $cache_backend, $cache_key, array $cache_tags = []) {
    assert(Inspector::assertAllStrings($cache_tags), 'Cache Tags must be strings.');
    $this->cacheBackend = $cache_backend;
    $this->cacheKey = $cache_key;
    $this->cacheTags = $cache_tags;
  }

  /**
   * Returns the cached Micon icons.
   *
   * @return array|null
   *   On success this will return an array of Micon icons. On failure
   *   this should return NULL, indicating to other methods that this has not
   *   yet been defined. Success with no values should return as an empty array
   *   and would actually be returned by the getIcons() method.
   */
  protected function getCachedDefinitions() {
    if (!isset($this->icons) && ($cache = $this
      ->cacheGet($this->cacheKey))) {
      $this->icons = $cache->data;
    }
    return $this->icons;
  }

  /**
   * Sets a cache of Micon icons.
   *
   * @param array $icons
   *   List of icons to store in cache.
   */
  protected function setCachedPackages(array $icons) {
    $this
      ->cacheSet($this->cacheKey, $icons, Cache::PERMANENT, $this->cacheTags);
    $this->icons = $icons;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
MiconIconManager::$entityManager protected property Drupal\Core\Entity\EntityTypeManager definition.
MiconIconManager::$icons protected property Cached icons array.
MiconIconManager::getCachedDefinitions protected function Returns the cached Micon icons.
MiconIconManager::getFlattenedIcons public function Get a flat list of all icons.
MiconIconManager::getIconMatch public function Match an icon_id against the Micon package icon definitions.
MiconIconManager::getIcons public function Get all available icons.
MiconIconManager::loadIcons protected function Load all available micon icons.
MiconIconManager::setCacheBackend public function Initialize the cache backend.
MiconIconManager::setCachedPackages protected function Sets a cache of Micon icons.
MiconIconManager::__construct public function Constructor.
UseCacheBackendTrait::$cacheBackend protected property Cache backend instance.
UseCacheBackendTrait::$useCaches protected property Flag whether caches should be used or skipped.
UseCacheBackendTrait::cacheGet protected function Fetches from the cache backend, respecting the use caches flag.
UseCacheBackendTrait::cacheSet protected function Stores data in the persistent cache, respecting the use caches flag.