Skip to content

Instantly share code, notes, and snippets.

@EclipseGc
Last active December 11, 2015 23:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save EclipseGc/4674513 to your computer and use it in GitHub Desktop.
Save EclipseGc/4674513 to your computer and use it in GitHub Desktop.
<?php
/**
* @file
* Definition of Drupal\Core\Plugin\DrupalPluginManagerBase
*/
namespace Drupal\Core\Plugin;
use Drupal\Component\Utility\NestedArray;
use Drupal\Component\Plugin\PluginManagerInterface;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Component\Plugin\Discovery\CachedDiscoveryInterface;
/**
* Base class for plugin managers.
*/
abstract class DrupalPluginManagerBase implements PluginManagerInterface CachedDiscoveryInterface {
/**
* The object that discovers plugins managed by this manager.
*
* @var Drupal\Component\Plugin\Discovery\DiscoveryInterface
*/
protected $discovery;
/**
* The object that instantiates plugins managed by this manager.
*
* @var Drupal\Component\Plugin\Factory\FactoryInterface
*/
protected $factory;
/**
* The object that returns the preconfigured plugin instance appropriate for a particular runtime condition.
*
* @var Drupal\Component\Plugin\Mapper\MapperInterface
*/
protected $mapper;
/**
* A set of defaults to be referenced by $this->processDefinition() if
* additional processing of plugins is necessary or helpful for development
* purposes.
*
* @var array
*/
protected $defaults = array();
/**
* The alter hook to invoke via drupal_alter().
*
* @var string
*/
protected $alter_hook;
/**
* The cache key used to store the definition list.
*
* @var string
*/
protected $cacheKey;
/**
* The cache bin used to store the definition list.
*
* @var string
*/
protected $cacheBin;
/**
* The timestamp indicating when the definition list cache expires.
*
* @var int
*/
protected $cacheExpire;
/**
* The cache tags associated with the definition list.
*
* @var array
*/
protected $cacheTags;
/**
* Constructs a Drupal\Core\Plugin\DrupalPluginManagerBase object.
*
* Provides a Drupal ready plugin manager base class that implements caching
* appropriately and simplifies developer experience for creating new plugin
* managers.
*
* @param string $alter_hook
* An optional string to define the alter hook name that will be available
* for altering plugin definitions of this type.
* @param string $cache_key
* The cache identifier used for storage of the definition list.
*/
public function __construct($alter_hook = '', $cache_key, $cache_bin = 'cache', $cache_expire = NULL, $cache_tags = array()) {
$this->alter_hook = $alter_hook ? $alter_hook : $owner . '_' . $type;
$this->cacheKey = $cache_key . ':' . language(LANGUAGE_TYPE_INTERFACE)->langcode;
$this->cacheBin = $cache_bin;
if (empty($cache_expire)) {
$this->cacheExpire = CacheBackendInterface::CACHE_PERMANENT;
}
$this->cacheTags = $cache_tags;
}
/**
* Implements Drupal\Component\Plugin\Discovery\DicoveryInterface::getDefinition().
*/
public function getDefinition($plugin_id) {
// Optimize for fast access to definitions if they are already in memory.
if (isset($this->definitions)) {
// Avoid using a ternary that would create a copy of the array.
if (isset($this->definitions[$plugin_id])) {
return $this->definitions[$plugin_id];
}
else {
return;
}
}
$definitions = $this->getDefinitions();
// Avoid using a ternary that would create a copy of the array.
if (isset($definitions[$plugin_id])) {
return $definitions[$plugin_id];
}
}
/**
* Implements Drupal\Component\Plugin\Discovery\DicoveryInterface::getDefinitions().
*/
public function getDefinitions() {
// Optimize for fast access to definitions if they are already in memory.
if (isset($this->definitions)) {
return $this->definitions;
}
$definitions = $this->getCachedDefinitions();
if (!isset($definitions)) {
$definitions = $this->discovery->getDefinitions();
foreach ($definitions as $plugin_id => &$definition) {
$this->processDefinition($definition, $plugin_id);
}
drupal_alter($this->alter_hook, $definitions);
$this->setCachedDefinitions($definitions);
}
return $definitions;
}
/**
* Implements Drupal\Component\Plugin\PluginManagerInterface::createInstance().
*/
public function createInstance($plugin_id, array $configuration = array()) {
return $this->factory->createInstance($plugin_id, $configuration);
}
/**
* Implements Drupal\Component\Plugin\PluginManagerInterface::getInstance().
*/
public function getInstance(array $options) {
if (!empty($this->mapper)) {
return $this->mapper->getInstance($options);
}
}
/**
* Performs extra processing on plugin definitions.
*
* By default we add defaults for the type to the definition. If a type has
* additional processing logic they can do that by replacing or extending the
* method.
*/
public function processDefinition(&$definition, $plugin_id) {
$definition = NestedArray::mergeDeep($this->defaults, $definition);
}
/**
* Returns the cached plugin definitions of the discovery class.
*
* @return mixed
* On success this will return an array of plugin definitions. 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 getDefinitions() method.
*/
protected function getCachedDefinitions() {
if (!isset($this->definitions) && isset($this->cacheKey) && $cache = cache($this->cacheBin)->get($this->cacheKey)) {
$this->definitions = $cache->data;
}
return $this->definitions;
}
/**
* Sets a cache of plugin definitions from the discovery class.
*
* @param array $definitions
* List of definitions to store in cache.
*/
protected function setCachedDefinitions($definitions) {
if (isset($this->cacheKey)) {
cache($this->cacheBin)->set($this->cacheKey, $definitions, $this->cacheExpire, $this->cacheTags);
}
$this->definitions = $definitions;
}
/**
* Implements \Drupal\Component\Plugin\Discovery\CachedDiscoveryInterface::clearCachedDefinitions().
*/
public function clearCachedDefinitions() {
// If there are any cache tags, clear cache based on those.
if (!empty($this->cacheTags)) {
cache($this->cacheBin)->deleteTags($this->cacheTags);
}
// Otherwise, just delete the specified cache key.
else if (isset($this->cacheKey)) {
cache($this->cacheBin)->delete($this->cacheKey);
}
$this->definitions = NULL;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment