DefaultLazyPluginCollection.php 5.65 KB
Newer Older
1 2
<?php

3
namespace Drupal\Core\Plugin;
4

5
use Drupal\Component\Plugin\Exception\PluginNotFoundException;
6
use Drupal\Component\Plugin\LazyPluginCollection;
7 8
use Drupal\Component\Plugin\PluginManagerInterface;
use Drupal\Component\Plugin\ConfigurablePluginInterface;
9
use Drupal\Core\DependencyInjection\DependencySerializationTrait;
10 11

/**
12
 * Provides a default plugin collection for a plugin type.
13
 *
14 15 16 17
 * A plugin collection is used to contain plugins that will be lazily
 * instantiated. The configurations of each potential plugin are passed in, and
 * the configuration key containing the plugin ID is specified by
 * self::$pluginKey.
18
 */
19
class DefaultLazyPluginCollection extends LazyPluginCollection {
20
  use DependencySerializationTrait;
21 22 23 24 25 26 27 28 29

  /**
   * The manager used to instantiate the plugins.
   *
   * @var \Drupal\Component\Plugin\PluginManagerInterface
   */
  protected $manager;

  /**
30
   * The initial configuration for each plugin in the collection.
31 32 33
   *
   * @var array
   *   An associative array containing the initial configuration for each plugin
34
   *   in the collection, keyed by plugin instance ID.
35 36 37 38 39 40 41 42 43 44
   */
  protected $configurations = array();

  /**
   * The key within the plugin configuration that contains the plugin ID.
   *
   * @var string
   */
  protected $pluginKey = 'id';

45 46 47 48 49 50 51
  /**
   * The original order of the instances.
   *
   * @var array
   */
  protected $originalOrder = array();

52
  /**
53
   * Constructs a new DefaultLazyPluginCollection object.
54 55 56 57 58
   *
   * @param \Drupal\Component\Plugin\PluginManagerInterface $manager
   *   The manager to be used for instantiating plugins.
   * @param array $configurations
   *   (optional) An associative array containing the initial configuration for
59
   *   each plugin in the collection, keyed by plugin instance ID.
60 61 62 63 64 65
   */
  public function __construct(PluginManagerInterface $manager, array $configurations = array()) {
    $this->manager = $manager;
    $this->configurations = $configurations;

    if (!empty($configurations)) {
66 67
      $instance_ids = array_keys($configurations);
      $this->instanceIDs = array_combine($instance_ids, $instance_ids);
68 69
      // Store the original order of the instance IDs for export.
      $this->originalOrder = $this->instanceIDs;
70 71 72 73 74 75 76
    }
  }

  /**
   * {@inheritdoc}
   */
  protected function initializePlugin($instance_id) {
77
    $configuration = isset($this->configurations[$instance_id]) ? $this->configurations[$instance_id] : array();
78
    if (!isset($configuration[$this->pluginKey])) {
79
      throw new PluginNotFoundException($instance_id);
80
    }
81
    $this->set($instance_id, $this->manager->createInstance($configuration[$this->pluginKey], $configuration));
82 83 84
  }

  /**
85
   * Sorts all plugin instances in this collection.
86
   *
87
   * @return $this
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
   */
  public function sort() {
    uasort($this->instanceIDs, array($this, 'sortHelper'));
    return $this;
  }

  /**
   * Provides uasort() callback to sort plugins.
   */
  public function sortHelper($aID, $bID) {
    $a = $this->get($aID);
    $b = $this->get($bID);
    return strnatcasecmp($a->getPluginId(), $b->getPluginId());
  }

  /**
104
   * {@inheritdoc}
105 106 107
   */
  public function getConfiguration() {
    $instances = array();
108 109 110 111 112 113
    // Store the current order of the instances.
    $current_order = $this->instanceIDs;
    // Reorder the instances to match the original order, adding new instances
    // to the end.
    $this->instanceIDs = $this->originalOrder + $current_order;

114 115 116 117 118 119 120 121
    foreach ($this as $instance_id => $instance) {
      if ($instance instanceof ConfigurablePluginInterface) {
        $instances[$instance_id] = $instance->getConfiguration();
      }
      else {
        $instances[$instance_id] = $this->configurations[$instance_id];
      }
    }
122 123
    // Restore the current order.
    $this->instanceIDs = $current_order;
124 125 126
    return $instances;
  }

127 128 129 130
  /**
   * {@inheritdoc}
   */
  public function setConfiguration($configuration) {
131 132 133
    // Track each instance ID as it is updated.
    $unprocessed_instance_ids = $this->getInstanceIds();

134 135
    foreach ($configuration as $instance_id => $instance_configuration) {
      $this->setInstanceConfiguration($instance_id, $instance_configuration);
136 137 138 139 140 141 142
      // Remove this instance ID from the list being updated.
      unset($unprocessed_instance_ids[$instance_id]);
    }

    // Remove remaining instances that had no configuration specified for them.
    foreach ($unprocessed_instance_ids as $unprocessed_instance_id) {
      $this->removeInstanceId($unprocessed_instance_id);
143 144 145 146
    }
    return $this;
  }

147 148 149 150 151 152 153 154 155 156 157
  /**
   * Updates the configuration for a plugin instance.
   *
   * If there is no plugin instance yet, a new will be instantiated. Otherwise,
   * the existing instance is updated with the new configuration.
   *
   * @param string $instance_id
   *   The ID of a plugin to set the configuration for.
   * @param array $configuration
   *   The plugin configuration to set.
   */
158
  public function setInstanceConfiguration($instance_id, array $configuration) {
159 160 161 162 163 164 165
    $this->configurations[$instance_id] = $configuration;
    $instance = $this->get($instance_id);
    if ($instance instanceof ConfigurablePluginInterface) {
      $instance->setConfiguration($configuration);
    }
  }

166 167 168 169 170 171 172 173 174 175 176 177 178
  /**
   * {@inheritdoc}
   */
  public function addInstanceId($id, $configuration = NULL) {
    parent::addInstanceId($id);
    if ($configuration !== NULL) {
      $this->setInstanceConfiguration($id, $configuration);
    }
    if (!isset($this->originalOrder[$id])) {
      $this->originalOrder[$id] = $id;
    }
  }

179 180 181
  /**
   * {@inheritdoc}
   */
182 183
  public function removeInstanceId($instance_id) {
    parent::removeInstanceId($instance_id);
184
    unset($this->originalOrder[$instance_id]);
185 186 187
    unset($this->configurations[$instance_id]);
  }

188
}