View source
<?php
namespace Drupal\gridstack;
use Drupal\Component\Plugin\Mapper\MapperInterface;
use Drupal\Component\Utility\NestedArray;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Plugin\DefaultPluginManager;
use Drupal\Core\StringTranslation\StringTranslationTrait;
abstract class GridStackPluginManagerBase extends DefaultPluginManager implements GridStackPluginManagerInterface, MapperInterface {
use StringTranslationTrait;
protected $config;
protected static $path = 'Plugin/gridstack';
protected static $interface = 'Drupal\\gridstack\\GridStackSkinPluginInterface';
protected static $annotation = 'Drupal\\gridstack\\Annotation\\GridStackSkin';
protected static $key = 'gridstack_skin';
protected $attachments;
protected $implementors;
public function __construct(\Traversable $namespaces, CacheBackendInterface $cache_backend, ModuleHandlerInterface $module_handler, ConfigFactoryInterface $config) {
parent::__construct(static::$path, $namespaces, $module_handler, static::$interface, static::$annotation);
$this->config = $config;
$this
->alterInfo(static::$key . '_info');
$this
->setCacheBackend($cache_backend, static::$key . '_plugins');
}
public function config($key = '', $settings = 'gridstack.settings') {
return $this->config
->get($settings)
->get($key);
}
public function getCache() {
return $this->cacheBackend;
}
public function load($plugin_id, array $configuration = []) {
return $plugin_id && $this
->hasDefinition($plugin_id) ? $this
->createInstance($plugin_id, $configuration) : NULL;
}
public function loadMultiple(array $configuration = []) {
$plugins = [];
foreach ($this
->getDefinitions() as $definition) {
array_push($plugins, $this
->createInstance($definition['id'], $configuration));
}
return $plugins;
}
protected function setAttachments(array &$load, array $attach = []) {
if (!$this->attachments) {
if ($implementors = $this
->getImplementors('attach')) {
foreach ($implementors as $id) {
$this
->load($id, $attach)
->attach($load, $attach);
}
}
$this->attachments = TRUE;
}
}
protected function getImplementors($method) {
if (!isset($this->implementors[$method])) {
$cid = static::$key . '_' . $method;
if ($cache = $this->cacheBackend
->get($cid)) {
$this->implementors[$method] = $cache->data;
}
else {
$data = [];
foreach ($this
->loadMultiple() as $plugin) {
$class = $plugin
->get('class');
$reflection = new \ReflectionClass($class);
if ($reflection
->getMethod($method)->class == $class) {
$data[$plugin
->getPluginId()] = $plugin
->getPluginId();
}
}
ksort($data);
$count = count($data);
$tags = Cache::buildTags($cid, [
'count:' . $count,
]);
$this->cacheBackend
->set($cid, $data, Cache::PERMANENT, $tags);
$this->implementors[$method] = $data;
}
}
return $this->implementors[$method];
}
protected function getData(array $methods, $flatten = FALSE, array $configuration = []) {
$cid = static::$key . 's_data';
if ($cache = $this->cacheBackend
->get($cid)) {
return $cache->data;
}
else {
$data = $items = [];
foreach ($this
->loadMultiple($configuration) as $plugin) {
if ($flatten) {
foreach ($methods as $method) {
$data = NestedArray::mergeDeep($data, $plugin
->{$method}());
}
}
else {
foreach ($methods as $method) {
$items[$method] = $plugin
->{$method}();
}
$data = NestedArray::mergeDeep($data, $items);
}
}
ksort($data);
$count = count($data);
$tags = Cache::buildTags($cid, [
'count:' . $count,
]);
$this->cacheBackend
->set($cid, $data, Cache::PERMANENT, $tags);
return $data;
}
}
public function getDataOptions(array $data) {
$options = [];
foreach ($data as $key => $properties) {
$options[$key] = isset($properties['name']) ? $properties['name'] : $key;
}
ksort($options);
return $options;
}
}