You are here

class UserProtectionPluginCollection in User protect 8

A collection of protection rules.

Hierarchy

Expanded class hierarchy of UserProtectionPluginCollection

2 files declare their use of UserProtectionPluginCollection
ProtectionRule.php in src/Entity/ProtectionRule.php
ProtectionRuleUnitTest.php in tests/src/Kernel/Entity/ProtectionRuleUnitTest.php

File

src/Plugin/UserProtection/UserProtectionPluginCollection.php, line 11

Namespace

Drupal\userprotect\Plugin\UserProtection
View source
class UserProtectionPluginCollection extends DefaultLazyPluginCollection {

  /**
   * All possible user protection plugin IDs.
   *
   * @var array
   */
  protected $definitions;

  /**
   * Retrieves all user protection plugin instances.
   *
   * @return array
   *   An array of user protection plugin instances.
   */
  public function getAll() {

    // Retrieve all available user protection plugin definitions.
    if (!$this->definitions) {
      $this->definitions = $this->manager
        ->getDefinitions();
    }

    // Ensure that there is an instance of all available plugins.
    foreach ($this->definitions as $plugin_id => $definition) {
      if (!isset($this->pluginInstances[$plugin_id])) {
        $this
          ->initializePlugin($plugin_id);
      }
    }

    // Sort plugins.
    uasort($this->pluginInstances, [
      $this,
      'pluginInstancesSort',
    ]);
    return $this->pluginInstances;
  }

  /**
   * Retrieves enabled user protection plugin instances.
   *
   * @return array
   *   An array of active user protection plugin instances.
   */
  public function getEnabledPlugins() {
    $instances = $this
      ->getAll();
    $enabled = [];
    foreach ($this->configurations as $instance_id => $configuration) {
      if ($configuration['status']) {
        $enabled[] = $instances[$instance_id];
      }
    }

    // Sort plugins.
    uasort($enabled, [
      $this,
      'pluginInstancesSort',
    ]);
    return $enabled;
  }

  /**
   * {@inheritdoc}
   */
  protected function initializePlugin($instance_id) {
    $configuration = $this->manager
      ->getDefinition($instance_id);

    // Merge the actual configuration into the default configuration.
    if (isset($this->configurations[$instance_id])) {
      $configuration = NestedArray::mergeDeep($configuration, $this->configurations[$instance_id]);
    }
    $this->configurations[$instance_id] = $configuration;
    parent::initializePlugin($instance_id);
  }

  /**
   * Sorts plugin instances based on weight, label, provider or id.
   *
   * @param \Drupal\userprotect\Plugin\UserProtection\UserProtectionInterface $a
   *   The first plugin in the comparison.
   * @param \Drupal\userprotect\Plugin\UserProtection\UserProtectionInterface $b
   *   The second plugin in the comparison.
   *
   * @return int
   *   -1 if $a should go first.
   *   1 if $b should go first.
   *   0 if it's unknown which should go first.
   */
  public function pluginInstancesSort(UserProtectionInterface $a, UserProtectionInterface $b) {
    if ($a
      ->getWeight() != $b
      ->getWeight()) {
      return $a
        ->getWeight() < $b
        ->getWeight() ? -1 : 1;
    }
    if ($a
      ->label() != $b
      ->label()) {
      return strnatcasecmp($a
        ->label(), $b
        ->label());
    }
    if ($a->provider != $b->provider) {
      return strnatcasecmp($a->provider, $b->provider);
    }
    return strnatcasecmp($a
      ->getPluginId(), $b
      ->getPluginId());
  }

  /**
   * {@inheritdoc}
   */
  public function getConfiguration() {
    $configuration = parent::getConfiguration();

    // Remove disabled protections.
    foreach ($configuration as $instance_id => $instance_config) {
      if (empty($instance_config['status'])) {
        unset($configuration[$instance_id]);
      }
    }
    return $configuration;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DefaultLazyPluginCollection::$configurations protected property The initial configuration for each plugin in the collection.
DefaultLazyPluginCollection::$manager protected property The manager used to instantiate the plugins.
DefaultLazyPluginCollection::$originalOrder protected property The original order of the instances.
DefaultLazyPluginCollection::$pluginKey protected property The key within the plugin configuration that contains the plugin ID. 3
DefaultLazyPluginCollection::addInstanceId public function Adds an instance ID to the available instance IDs. Overrides LazyPluginCollection::addInstanceId
DefaultLazyPluginCollection::removeInstanceId public function Removes an instance ID. Overrides LazyPluginCollection::removeInstanceId
DefaultLazyPluginCollection::setConfiguration public function Sets the configuration for all plugins in this collection. Overrides LazyPluginCollection::setConfiguration
DefaultLazyPluginCollection::setInstanceConfiguration public function Updates the configuration for a plugin instance.
DefaultLazyPluginCollection::sort public function Sorts all plugin instances in this collection. 1
DefaultLazyPluginCollection::sortHelper public function Provides uasort() callback to sort plugins. 2
DefaultLazyPluginCollection::__construct public function Constructs a new DefaultLazyPluginCollection object. 1
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
LazyPluginCollection::$instanceIds protected property Stores the IDs of all potential plugin instances.
LazyPluginCollection::$pluginInstances protected property Stores all instantiated plugins.
LazyPluginCollection::clear public function Clears all instantiated plugins.
LazyPluginCollection::count public function
LazyPluginCollection::get public function Gets a plugin instance, initializing it if necessary. 4
LazyPluginCollection::getInstanceIds public function Gets all instance IDs.
LazyPluginCollection::getIterator public function
LazyPluginCollection::has public function Determines if a plugin instance exists.
LazyPluginCollection::remove public function Removes an initialized plugin.
LazyPluginCollection::set public function Stores an initialized plugin.
UserProtectionPluginCollection::$definitions protected property All possible user protection plugin IDs.
UserProtectionPluginCollection::getAll public function Retrieves all user protection plugin instances.
UserProtectionPluginCollection::getConfiguration public function Gets the current configuration of all plugins in this collection. Overrides DefaultLazyPluginCollection::getConfiguration
UserProtectionPluginCollection::getEnabledPlugins public function Retrieves enabled user protection plugin instances.
UserProtectionPluginCollection::initializePlugin protected function Initializes and stores a plugin. Overrides DefaultLazyPluginCollection::initializePlugin
UserProtectionPluginCollection::pluginInstancesSort public function Sorts plugin instances based on weight, label, provider or id.