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