-
Notifications
You must be signed in to change notification settings - Fork 4
/
PluginManagerBase.php
142 lines (126 loc) · 4.07 KB
/
PluginManagerBase.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
<?php
namespace Drupal\Component\Plugin;
use Drupal\Component\Plugin\Discovery\DiscoveryTrait;
use Drupal\Component\Plugin\Exception\PluginNotFoundException;
/**
* Base class for plugin managers.
*/
abstract class PluginManagerBase implements PluginManagerInterface {
use DiscoveryTrait;
/**
* The object that discovers plugins managed by this manager.
*
* @var \Drupal\Component\Plugin\Discovery\DiscoveryInterface
*/
protected $discovery;
/**
* The object that instantiates plugins managed by this manager.
*
* @var \Drupal\Component\Plugin\Factory\FactoryInterface
*/
protected $factory;
/**
* The object that returns the preconfigured plugin instance appropriate for a particular runtime condition.
*
* @var \Drupal\Component\Plugin\Mapper\MapperInterface|null
*/
protected $mapper;
/**
* Gets the plugin discovery.
*
* @return \Drupal\Component\Plugin\Discovery\DiscoveryInterface
*/
protected function getDiscovery() {
return $this->discovery;
}
/**
* Gets the plugin factory.
*
* @return \Drupal\Component\Plugin\Factory\FactoryInterface
*/
protected function getFactory() {
return $this->factory;
}
/**
* {@inheritdoc}
*/
public function getDefinition($plugin_id, $exception_on_invalid = TRUE) {
return $this->getDiscovery()->getDefinition($plugin_id, $exception_on_invalid);
}
/**
* {@inheritdoc}
*/
public function getDefinitions() {
return $this->getDiscovery()->getDefinitions();
}
/**
* {@inheritdoc}
*/
public function createInstance($plugin_id, array $configuration = []) {
// If this PluginManager has fallback capabilities catch
// PluginNotFoundExceptions.
if ($this instanceof FallbackPluginManagerInterface) {
try {
return $this->getFactory()->createInstance($plugin_id, $configuration);
}
catch (PluginNotFoundException) {
return $this->handlePluginNotFound($plugin_id, $configuration);
}
}
else {
return $this->getFactory()->createInstance($plugin_id, $configuration);
}
}
/**
* Allows plugin managers to specify custom behavior if a plugin is not found.
*
* @param string $plugin_id
* The ID of the missing requested plugin.
* @param array $configuration
* An array of configuration relevant to the plugin instance.
*
* @return object
* A fallback plugin instance.
*
* @throws \BadMethodCallException
* When ::getFallbackPluginId() is not implemented in the concrete plugin
* manager class.
*/
protected function handlePluginNotFound($plugin_id, array $configuration) {
$fallback_id = $this->getFallbackPluginId($plugin_id, $configuration);
return $this->getFactory()->createInstance($fallback_id, $configuration);
}
/**
* Gets a fallback id for a missing plugin.
*
* This method should be implemented in extending classes that also implement
* FallbackPluginManagerInterface. It is called by
* PluginManagerBase::handlePluginNotFound on the abstract class, and
* therefore should be defined as well on the abstract class to prevent static
* analysis errors.
*
* @param string $plugin_id
* The ID of the missing requested plugin.
* @param array $configuration
* An array of configuration relevant to the plugin instance.
*
* @return string
* The id of an existing plugin to use when the plugin does not exist.
*
* @throws \BadMethodCallException
* If the method is not implemented in the concrete plugin manager class.
*/
// phpcs:ignore Drupal.Commenting.FunctionComment.InvalidNoReturn
protected function getFallbackPluginId($plugin_id, array $configuration = []) {
throw new \BadMethodCallException(static::class . '::getFallbackPluginId() not implemented.');
}
/**
* {@inheritdoc}
*/
public function getInstance(array $options) {
if (!$this->mapper) {
throw new \BadMethodCallException(sprintf('%s does not support this method unless %s::$mapper is set.', static::class, static::class));
}
return $this->mapper->getInstance($options);
}
}