class MiconIconManager in Micon 2.x
Same name and namespace in other branches
- 8 src/MiconIconManager.php \Drupal\micon\MiconIconManager
Class MiconIconManager.
@package Drupal\micon
Hierarchy
- class \Drupal\micon\MiconIconManager uses UseCacheBackendTrait
Expanded class hierarchy of MiconIconManager
1 file declares its use of MiconIconManager
- MiconCommands.php in src/
Commands/ MiconCommands.php
1 string reference to 'MiconIconManager'
1 service uses MiconIconManager
File
- src/
MiconIconManager.php, line 17
Namespace
Drupal\miconView 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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
MiconIconManager:: |
protected | property | Drupal\Core\Entity\EntityTypeManager definition. | |
MiconIconManager:: |
protected | property | Cached icons array. | |
MiconIconManager:: |
protected | function | Returns the cached Micon icons. | |
MiconIconManager:: |
public | function | Get a flat list of all icons. | |
MiconIconManager:: |
public | function | Match an icon_id against the Micon package icon definitions. | |
MiconIconManager:: |
public | function | Get all available icons. | |
MiconIconManager:: |
protected | function | Load all available micon icons. | |
MiconIconManager:: |
public | function | Initialize the cache backend. | |
MiconIconManager:: |
protected | function | Sets a cache of Micon icons. | |
MiconIconManager:: |
public | function | Constructor. | |
UseCacheBackendTrait:: |
protected | property | Cache backend instance. | |
UseCacheBackendTrait:: |
protected | property | Flag whether caches should be used or skipped. | |
UseCacheBackendTrait:: |
protected | function | Fetches from the cache backend, respecting the use caches flag. | |
UseCacheBackendTrait:: |
protected | function | Stores data in the persistent cache, respecting the use caches flag. |