comparison core/lib/Drupal/Core/Plugin/DefaultSingleLazyPluginCollection.php @ 0:4c8ae668cc8c

Initial import (non-working)
author Chris Cannam
date Wed, 29 Nov 2017 16:09:58 +0000
parents
children af1871eacc83
comparison
equal deleted inserted replaced
-1:000000000000 0:4c8ae668cc8c
1 <?php
2
3 namespace Drupal\Core\Plugin;
4
5 use Drupal\Component\Plugin\PluginManagerInterface;
6 use Drupal\Component\Plugin\LazyPluginCollection;
7 use Drupal\Component\Plugin\ConfigurablePluginInterface;
8 use Drupal\Core\DependencyInjection\DependencySerializationTrait;
9
10 /**
11 * Provides a default plugin collection for a plugin type.
12 *
13 * A plugin collection usually stores multiple plugins, and is used to lazily
14 * instantiate them. When only one plugin is needed, it is still best practice
15 * to encapsulate all of the instantiation logic in a plugin collection. This
16 * class can be used directly, or subclassed to add further exception handling
17 * in self::initializePlugin().
18 */
19 class DefaultSingleLazyPluginCollection 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 * An array of configuration to instantiate the plugin with.
31 *
32 * @var array
33 */
34 protected $configuration;
35
36 /**
37 * The instance ID used for this plugin collection.
38 *
39 * @var string
40 */
41 protected $instanceId;
42
43 /**
44 * Constructs a new DefaultSingleLazyPluginCollection object.
45 *
46 * @param \Drupal\Component\Plugin\PluginManagerInterface $manager
47 * The manager to be used for instantiating plugins.
48 * @param string $instance_id
49 * The ID of the plugin instance.
50 * @param array $configuration
51 * An array of configuration.
52 */
53 public function __construct(PluginManagerInterface $manager, $instance_id, array $configuration) {
54 $this->manager = $manager;
55 $this->addInstanceId($instance_id, $configuration);
56 }
57
58 /**
59 * {@inheritdoc}
60 */
61 protected function initializePlugin($instance_id) {
62 $this->set($instance_id, $this->manager->createInstance($instance_id, $this->configuration));
63 }
64
65 /**
66 * {@inheritdoc}
67 */
68 public function getConfiguration() {
69 $plugin = $this->get($this->instanceId);
70 if ($plugin instanceof ConfigurablePluginInterface) {
71 return $plugin->getConfiguration();
72 }
73 else {
74 return $this->configuration;
75 }
76 }
77
78 /**
79 * {@inheritdoc}
80 */
81 public function setConfiguration($configuration) {
82 $this->configuration = $configuration;
83 $plugin = $this->get($this->instanceId);
84 if ($plugin instanceof ConfigurablePluginInterface) {
85 $plugin->setConfiguration($configuration);
86 }
87 return $this;
88 }
89
90 /**
91 * {@inheritdoc}
92 */
93 public function addInstanceId($id, $configuration = NULL) {
94 $this->instanceId = $id;
95 // Reset the list of instance IDs since there can be only one.
96 $this->instanceIDs = [];
97 parent::addInstanceId($id, $configuration);
98 if ($configuration !== NULL) {
99 $this->setConfiguration($configuration);
100 }
101 }
102
103 }