You are here

GridStackPluginManagerBase.php in GridStack 8.2

Namespace

Drupal\gridstack

File

src/GridStackPluginManagerBase.php
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;

/**
 * Implements GridStackPluginManagerInterface.
 */
abstract class GridStackPluginManagerBase extends DefaultPluginManager implements GridStackPluginManagerInterface, MapperInterface {
  use StringTranslationTrait;

  /**
   * The config factory.
   *
   * @var \Drupal\Core\Config\ConfigFactoryInterface
   */
  protected $config;

  /**
   * The plugin path.
   *
   * @var string
   */
  protected static $path = 'Plugin/gridstack';

  /**
   * The plugin interface.
   *
   * @var string
   */
  protected static $interface = 'Drupal\\gridstack\\GridStackSkinPluginInterface';

  /**
   * The plugin annotation.
   *
   * @var string
   */
  protected static $annotation = 'Drupal\\gridstack\\Annotation\\GridStackSkin';

  /**
   * The plugin key.
   *
   * @var string
   */
  protected static $key = 'gridstack_skin';

  /**
   * The plugin attachments.
   *
   * @var bool
   */
  protected $attachments;

  /**
   * The implementors of a method.
   *
   * @var array
   */
  protected $implementors;

  /**
   * {@inheritdoc}
   */
  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');
  }

  /**
   * Returns gridstack config shortcut.
   */
  public function config($key = '', $settings = 'gridstack.settings') {
    return $this->config
      ->get($settings)
      ->get($key);
  }

  /**
   * Returns cache backend service.
   */
  public function getCache() {
    return $this->cacheBackend;
  }

  /**
   * {@inheritdoc}
   */
  public function load($plugin_id, array $configuration = []) {
    return $plugin_id && $this
      ->hasDefinition($plugin_id) ? $this
      ->createInstance($plugin_id, $configuration) : NULL;
  }

  /**
   * Returns all available plugins.
   */
  public function loadMultiple(array $configuration = []) {
    $plugins = [];
    foreach ($this
      ->getDefinitions() as $definition) {
      array_push($plugins, $this
        ->createInstance($definition['id'], $configuration));
    }
    return $plugins;
  }

  /**
   * Collects attachments from 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;
    }
  }

  /**
   * Returns plugins names implementing a method.
   */
  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];
  }

  /**
   * Returns gridstack plugin data.
   */
  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;
    }
  }

  /**
   * Returns available data for select options.
   */
  public function getDataOptions(array $data) {
    $options = [];
    foreach ($data as $key => $properties) {
      $options[$key] = isset($properties['name']) ? $properties['name'] : $key;
    }
    ksort($options);
    return $options;
  }

}

Classes

Namesort descending Description
GridStackPluginManagerBase Implements GridStackPluginManagerInterface.