Version 1
[yaffs-website] / web / core / lib / Drupal / Core / Plugin / DefaultSingleLazyPluginCollection.php
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->instanceId = $instance_id;
56     // This is still needed by the parent LazyPluginCollection class.
57     $this->instanceIDs = [$instance_id => $instance_id];
58     $this->configuration = $configuration;
59   }
60
61   /**
62    * {@inheritdoc}
63    */
64   protected function initializePlugin($instance_id) {
65     $this->set($instance_id, $this->manager->createInstance($instance_id, $this->configuration));
66   }
67
68   /**
69    * {@inheritdoc}
70    */
71   public function getConfiguration() {
72     $plugin = $this->get($this->instanceId);
73     if ($plugin instanceof ConfigurablePluginInterface) {
74       return $plugin->getConfiguration();
75     }
76     else {
77       return $this->configuration;
78     }
79   }
80
81   /**
82    * {@inheritdoc}
83    */
84   public function setConfiguration($configuration) {
85     $this->configuration = $configuration;
86     $plugin = $this->get($this->instanceId);
87     if ($plugin instanceof ConfigurablePluginInterface) {
88       $plugin->setConfiguration($configuration);
89     }
90     return $this;
91   }
92
93   /**
94    * {@inheritdoc}
95    */
96   public function addInstanceId($id, $configuration = NULL) {
97     $this->instanceId = $id;
98     parent::addInstanceId($id, $configuration);
99     if ($configuration !== NULL) {
100       $this->setConfiguration($configuration);
101     }
102   }
103
104 }