forked from jacerider/real_favicon-archived
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a real favicon manager for performance reasons.
- Loading branch information
Showing
4 changed files
with
159 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
services: | ||
real_favicon.manager: | ||
class: Drupal\real_favicon\RealFaviconManager | ||
arguments: ['@entity_type.manager', '@config.factory', '@cache.data'] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
<?php | ||
|
||
namespace Drupal\real_favicon; | ||
|
||
use Drupal\Core\Entity\EntityTypeManagerInterface; | ||
use Drupal\Core\Config\ConfigFactoryInterface; | ||
use Drupal\Core\Cache\CacheBackendInterface; | ||
use Drupal\Core\Cache\Cache; | ||
|
||
/** | ||
* Class RealFaviconManager. | ||
*/ | ||
class RealFaviconManager implements RealFaviconManagerInterface { | ||
|
||
/** | ||
* The entity type manager. | ||
* | ||
* @var \Drupal\Core\Entity\EntityTypeManagerInterface | ||
*/ | ||
public $entityTypeManager; | ||
|
||
/** | ||
* The real faicon configuration. | ||
* | ||
* @var \Drupal\Core\Config\ImmutableConfig | ||
*/ | ||
protected $config; | ||
|
||
/** | ||
* The cache backend. | ||
* | ||
* @var \Drupal\Core\Cache\CacheBackendInterface | ||
*/ | ||
protected $cache; | ||
|
||
/** | ||
* The cache id. | ||
* | ||
* @var string | ||
*/ | ||
protected $cid = 'real_favicon.favicon'; | ||
|
||
/** | ||
* The cache tags. | ||
* | ||
* @var array | ||
*/ | ||
protected $cacheTags = [ | ||
'config:real_favicon.settings', | ||
'config:real_favicon_list', | ||
]; | ||
|
||
/** | ||
* Constructs a new RealFaviconManager object. | ||
* | ||
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager | ||
* The entity type manager. | ||
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory | ||
* The config factory. | ||
* @param \Drupal\Core\Cache\CacheBackendInterface $cache | ||
* The cache backend. | ||
*/ | ||
public function __construct(EntityTypeManagerInterface $entity_type_manager, ConfigFactoryInterface $config_factory, CacheBackendInterface $cache) { | ||
$this->entityTypeManager = $entity_type_manager; | ||
$this->config = $config_factory->get('real_favicon.settings'); | ||
$this->cache = $cache; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getTags($theme_id) { | ||
$tags = NULL; | ||
$enabled = $this->config->get('themes'); | ||
if (!empty($enabled[$theme_id])) { | ||
$cid = $this->cid . '.tags.' . $theme_id; | ||
if ($cache = $this->cache->get($cid)) { | ||
$tags = $cache->data; | ||
} | ||
else { | ||
if ($favicon = $this->loadFavicon($theme_id)) { | ||
$tags = $favicon->getValidTagsAsString(); | ||
} | ||
$this->cache->set($cid, $tags, Cache::PERMANENT, $this->cacheTags); | ||
} | ||
} | ||
return $tags; | ||
} | ||
|
||
/** | ||
* Load a favicon. | ||
*/ | ||
public function loadFavicon($theme_id) { | ||
$favicon = NULL; | ||
$enabled = $this->config->get('themes'); | ||
if (!empty($enabled[$theme_id])) { | ||
$favicon = $this->entityTypeManager->getStorage('real_favicon')->load($enabled[$theme_id]); | ||
} | ||
return $favicon; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getCacheTags() { | ||
return $this->cacheTags; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?php | ||
|
||
namespace Drupal\real_favicon; | ||
|
||
/** | ||
* Interface RealFaviconManagerInterface. | ||
*/ | ||
interface RealFaviconManagerInterface { | ||
|
||
/** | ||
* Get cache tags for a theme as a string. | ||
* | ||
* @param string $theme_id | ||
* The theme id. | ||
* | ||
* @return string|null | ||
* The tags as HTML ready for output. | ||
*/ | ||
public function getTags($theme_id); | ||
|
||
/** | ||
* Get the real favicon entity assiciated with a theme. | ||
* | ||
* @param string $theme_id | ||
* The theme id. | ||
* | ||
* @return \Drupal\real_favicon\Entity\RealFavicon|null | ||
* The real favicon entity. | ||
*/ | ||
public function loadFavicon($theme_id); | ||
|
||
/** | ||
* Get the cache tags for a theme favicon. | ||
*/ | ||
public function getCacheTags(); | ||
|
||
} |