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@0
|
32 * @var \Drupal\Component\Plugin\Mapper\MapperInterface
|
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@0
|
79 $fallback_id = $this->getFallbackPluginId($plugin_id, $configuration);
|
Chris@0
|
80 return $this->getFactory()->createInstance($fallback_id, $configuration);
|
Chris@0
|
81 }
|
Chris@0
|
82 }
|
Chris@0
|
83 else {
|
Chris@0
|
84 return $this->getFactory()->createInstance($plugin_id, $configuration);
|
Chris@0
|
85 }
|
Chris@0
|
86 }
|
Chris@0
|
87
|
Chris@0
|
88 /**
|
Chris@0
|
89 * {@inheritdoc}
|
Chris@0
|
90 */
|
Chris@0
|
91 public function getInstance(array $options) {
|
Chris@0
|
92 return $this->mapper->getInstance($options);
|
Chris@0
|
93 }
|
Chris@0
|
94
|
Chris@0
|
95 }
|