Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\Component\Plugin;
|
Chris@0
|
4
|
Chris@0
|
5 use Drupal\Component\Plugin\Discovery\DiscoveryTrait;
|
Chris@0
|
6 use Drupal\Component\Plugin\Exception\PluginNotFoundException;
|
Chris@0
|
7
|
Chris@0
|
8 /**
|
Chris@0
|
9 * Base class for plugin managers.
|
Chris@0
|
10 */
|
Chris@0
|
11 abstract class PluginManagerBase implements PluginManagerInterface {
|
Chris@0
|
12
|
Chris@0
|
13 use DiscoveryTrait;
|
Chris@0
|
14
|
Chris@0
|
15 /**
|
Chris@0
|
16 * The object that discovers plugins managed by this manager.
|
Chris@0
|
17 *
|
Chris@0
|
18 * @var \Drupal\Component\Plugin\Discovery\DiscoveryInterface
|
Chris@0
|
19 */
|
Chris@0
|
20 protected $discovery;
|
Chris@0
|
21
|
Chris@0
|
22 /**
|
Chris@0
|
23 * The object that instantiates plugins managed by this manager.
|
Chris@0
|
24 *
|
Chris@0
|
25 * @var \Drupal\Component\Plugin\Factory\FactoryInterface
|
Chris@0
|
26 */
|
Chris@0
|
27 protected $factory;
|
Chris@0
|
28
|
Chris@0
|
29 /**
|
Chris@0
|
30 * The object that returns the preconfigured plugin instance appropriate for a particular runtime condition.
|
Chris@0
|
31 *
|
Chris@17
|
32 * @var \Drupal\Component\Plugin\Mapper\MapperInterface|null
|
Chris@0
|
33 */
|
Chris@0
|
34 protected $mapper;
|
Chris@0
|
35
|
Chris@0
|
36 /**
|
Chris@0
|
37 * Gets the plugin discovery.
|
Chris@0
|
38 *
|
Chris@0
|
39 * @return \Drupal\Component\Plugin\Discovery\DiscoveryInterface
|
Chris@0
|
40 */
|
Chris@0
|
41 protected function getDiscovery() {
|
Chris@0
|
42 return $this->discovery;
|
Chris@0
|
43 }
|
Chris@0
|
44
|
Chris@0
|
45 /**
|
Chris@0
|
46 * Gets the plugin factory.
|
Chris@0
|
47 *
|
Chris@0
|
48 * @return \Drupal\Component\Plugin\Factory\FactoryInterface
|
Chris@0
|
49 */
|
Chris@0
|
50 protected function getFactory() {
|
Chris@0
|
51 return $this->factory;
|
Chris@0
|
52 }
|
Chris@0
|
53
|
Chris@0
|
54 /**
|
Chris@0
|
55 * {@inheritdoc}
|
Chris@0
|
56 */
|
Chris@0
|
57 public function getDefinition($plugin_id, $exception_on_invalid = TRUE) {
|
Chris@0
|
58 return $this->getDiscovery()->getDefinition($plugin_id, $exception_on_invalid);
|
Chris@0
|
59 }
|
Chris@0
|
60
|
Chris@0
|
61 /**
|
Chris@0
|
62 * {@inheritdoc}
|
Chris@0
|
63 */
|
Chris@0
|
64 public function getDefinitions() {
|
Chris@0
|
65 return $this->getDiscovery()->getDefinitions();
|
Chris@0
|
66 }
|
Chris@0
|
67
|
Chris@0
|
68 /**
|
Chris@0
|
69 * {@inheritdoc}
|
Chris@0
|
70 */
|
Chris@0
|
71 public function createInstance($plugin_id, array $configuration = []) {
|
Chris@0
|
72 // If this PluginManager has fallback capabilities catch
|
Chris@0
|
73 // PluginNotFoundExceptions.
|
Chris@0
|
74 if ($this instanceof FallbackPluginManagerInterface) {
|
Chris@0
|
75 try {
|
Chris@0
|
76 return $this->getFactory()->createInstance($plugin_id, $configuration);
|
Chris@0
|
77 }
|
Chris@0
|
78 catch (PluginNotFoundException $e) {
|
Chris@17
|
79 return $this->handlePluginNotFound($plugin_id, $configuration);
|
Chris@0
|
80 }
|
Chris@0
|
81 }
|
Chris@0
|
82 else {
|
Chris@0
|
83 return $this->getFactory()->createInstance($plugin_id, $configuration);
|
Chris@0
|
84 }
|
Chris@0
|
85 }
|
Chris@0
|
86
|
Chris@0
|
87 /**
|
Chris@17
|
88 * Allows plugin managers to specify custom behavior if a plugin is not found.
|
Chris@17
|
89 *
|
Chris@17
|
90 * @param string $plugin_id
|
Chris@17
|
91 * The ID of the missing requested plugin.
|
Chris@17
|
92 * @param array $configuration
|
Chris@17
|
93 * An array of configuration relevant to the plugin instance.
|
Chris@17
|
94 *
|
Chris@17
|
95 * @return object
|
Chris@17
|
96 * A fallback plugin instance.
|
Chris@17
|
97 */
|
Chris@17
|
98 protected function handlePluginNotFound($plugin_id, array $configuration) {
|
Chris@17
|
99 $fallback_id = $this->getFallbackPluginId($plugin_id, $configuration);
|
Chris@17
|
100 return $this->getFactory()->createInstance($fallback_id, $configuration);
|
Chris@17
|
101 }
|
Chris@17
|
102
|
Chris@17
|
103 /**
|
Chris@0
|
104 * {@inheritdoc}
|
Chris@0
|
105 */
|
Chris@0
|
106 public function getInstance(array $options) {
|
Chris@17
|
107 if (!$this->mapper) {
|
Chris@17
|
108 throw new \BadMethodCallException(sprintf('%s does not support this method unless %s::$mapper is set.', static::class, static::class));
|
Chris@17
|
109 }
|
Chris@0
|
110 return $this->mapper->getInstance($options);
|
Chris@0
|
111 }
|
Chris@0
|
112
|
Chris@0
|
113 }
|