Further Drupal 8.6.4 changes. Some core files were not committed before a commit...
[yaffs-website] / web / core / lib / Drupal / Core / Plugin / DefaultLazyPluginCollection.php
1 <?php
2
3 namespace Drupal\Core\Plugin;
4
5 use Drupal\Component\Plugin\Exception\PluginNotFoundException;
6 use Drupal\Component\Plugin\LazyPluginCollection;
7 use Drupal\Component\Plugin\PluginManagerInterface;
8 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  * A plugin collection is used to contain plugins that will be lazily
15  * instantiated. The configurations of each potential plugin are passed in, and
16  * the configuration key containing the plugin ID is specified by
17  * self::$pluginKey.
18  */
19 class DefaultLazyPluginCollection extends LazyPluginCollection {
20   use DependencySerializationTrait;
21
22   /**
23    * The manager used to instantiate the plugins.
24    *
25    * @var \Drupal\Component\Plugin\PluginManagerInterface
26    */
27   protected $manager;
28
29   /**
30    * The initial configuration for each plugin in the collection.
31    *
32    * @var array
33    *   An associative array containing the initial configuration for each plugin
34    *   in the collection, keyed by plugin instance ID.
35    */
36   protected $configurations = [];
37
38   /**
39    * The key within the plugin configuration that contains the plugin ID.
40    *
41    * @var string
42    */
43   protected $pluginKey = 'id';
44
45   /**
46    * The original order of the instances.
47    *
48    * @var array
49    */
50   protected $originalOrder = [];
51
52   /**
53    * Constructs a new DefaultLazyPluginCollection object.
54    *
55    * @param \Drupal\Component\Plugin\PluginManagerInterface $manager
56    *   The manager to be used for instantiating plugins.
57    * @param array $configurations
58    *   (optional) An associative array containing the initial configuration for
59    *   each plugin in the collection, keyed by plugin instance ID.
60    */
61   public function __construct(PluginManagerInterface $manager, array $configurations = []) {
62     $this->manager = $manager;
63     $this->configurations = $configurations;
64
65     if (!empty($configurations)) {
66       $instance_ids = array_keys($configurations);
67       $this->instanceIDs = array_combine($instance_ids, $instance_ids);
68       // Store the original order of the instance IDs for export.
69       $this->originalOrder = $this->instanceIDs;
70     }
71   }
72
73   /**
74    * {@inheritdoc}
75    */
76   protected function initializePlugin($instance_id) {
77     $configuration = isset($this->configurations[$instance_id]) ? $this->configurations[$instance_id] : [];
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   public function sort() {
90     uasort($this->instanceIDs, [$this, 'sortHelper']);
91     return $this;
92   }
93
94   /**
95    * Provides uasort() callback to sort plugins.
96    */
97   public function sortHelper($aID, $bID) {
98     $a = $this->get($aID);
99     $b = $this->get($bID);
100     return strnatcasecmp($a->getPluginId(), $b->getPluginId());
101   }
102
103   /**
104    * {@inheritdoc}
105    */
106   public function getConfiguration() {
107     $instances = [];
108     // Store the current order of the instances.
109     $current_order = $this->instanceIDs;
110     // Reorder the instances to match the original order, adding new instances
111     // to the end.
112     $this->instanceIDs = $this->originalOrder + $current_order;
113
114     foreach ($this as $instance_id => $instance) {
115       if ($instance instanceof ConfigurablePluginInterface) {
116         $instances[$instance_id] = $instance->getConfiguration();
117       }
118       else {
119         $instances[$instance_id] = $this->configurations[$instance_id];
120       }
121     }
122     // Restore the current order.
123     $this->instanceIDs = $current_order;
124     return $instances;
125   }
126
127   /**
128    * {@inheritdoc}
129    */
130   public function setConfiguration($configuration) {
131     // Track each instance ID as it is updated.
132     $unprocessed_instance_ids = $this->getInstanceIds();
133
134     foreach ($configuration as $instance_id => $instance_configuration) {
135       $this->setInstanceConfiguration($instance_id, $instance_configuration);
136       // Remove this instance ID from the list being updated.
137       unset($unprocessed_instance_ids[$instance_id]);
138     }
139
140     // Remove remaining instances that had no configuration specified for them.
141     foreach ($unprocessed_instance_ids as $unprocessed_instance_id) {
142       $this->removeInstanceId($unprocessed_instance_id);
143     }
144     return $this;
145   }
146
147   /**
148    * Updates the configuration for a plugin instance.
149    *
150    * If there is no plugin instance yet, a new will be instantiated. Otherwise,
151    * the existing instance is updated with the new configuration.
152    *
153    * @param string $instance_id
154    *   The ID of a plugin to set the configuration for.
155    * @param array $configuration
156    *   The plugin configuration to set.
157    */
158   public function setInstanceConfiguration($instance_id, array $configuration) {
159     $this->configurations[$instance_id] = $configuration;
160     $instance = $this->get($instance_id);
161     if ($instance instanceof ConfigurablePluginInterface) {
162       $instance->setConfiguration($configuration);
163     }
164   }
165
166   /**
167    * {@inheritdoc}
168    */
169   public function addInstanceId($id, $configuration = NULL) {
170     parent::addInstanceId($id);
171     if ($configuration !== NULL) {
172       $this->setInstanceConfiguration($id, $configuration);
173     }
174     if (!isset($this->originalOrder[$id])) {
175       $this->originalOrder[$id] = $id;
176     }
177   }
178
179   /**
180    * {@inheritdoc}
181    */
182   public function removeInstanceId($instance_id) {
183     parent::removeInstanceId($instance_id);
184     unset($this->originalOrder[$instance_id]);
185     unset($this->configurations[$instance_id]);
186   }
187
188 }