Version 1
[yaffs-website] / web / core / lib / Drupal / Component / Plugin / PluginManagerBase.php
1 <?php
2
3 namespace Drupal\Component\Plugin;
4
5 use Drupal\Component\Plugin\Discovery\DiscoveryTrait;
6 use Drupal\Component\Plugin\Exception\PluginNotFoundException;
7
8 /**
9  * Base class for plugin managers.
10  */
11 abstract class PluginManagerBase implements PluginManagerInterface {
12
13   use DiscoveryTrait;
14
15   /**
16    * The object that discovers plugins managed by this manager.
17    *
18    * @var \Drupal\Component\Plugin\Discovery\DiscoveryInterface
19    */
20   protected $discovery;
21
22   /**
23    * The object that instantiates plugins managed by this manager.
24    *
25    * @var \Drupal\Component\Plugin\Factory\FactoryInterface
26    */
27   protected $factory;
28
29   /**
30    * The object that returns the preconfigured plugin instance appropriate for a particular runtime condition.
31    *
32    * @var \Drupal\Component\Plugin\Mapper\MapperInterface
33    */
34   protected $mapper;
35
36   /**
37    * Gets the plugin discovery.
38    *
39    * @return \Drupal\Component\Plugin\Discovery\DiscoveryInterface
40    */
41   protected function getDiscovery() {
42     return $this->discovery;
43   }
44
45   /**
46    * Gets the plugin factory.
47    *
48    * @return \Drupal\Component\Plugin\Factory\FactoryInterface
49    */
50   protected function getFactory() {
51     return $this->factory;
52   }
53
54   /**
55    * {@inheritdoc}
56    */
57   public function getDefinition($plugin_id, $exception_on_invalid = TRUE) {
58     return $this->getDiscovery()->getDefinition($plugin_id, $exception_on_invalid);
59   }
60
61   /**
62    * {@inheritdoc}
63    */
64   public function getDefinitions() {
65     return $this->getDiscovery()->getDefinitions();
66   }
67
68   /**
69    * {@inheritdoc}
70    */
71   public function createInstance($plugin_id, array $configuration = []) {
72     // If this PluginManager has fallback capabilities catch
73     // PluginNotFoundExceptions.
74     if ($this instanceof FallbackPluginManagerInterface) {
75       try {
76         return $this->getFactory()->createInstance($plugin_id, $configuration);
77       }
78       catch (PluginNotFoundException $e) {
79         $fallback_id = $this->getFallbackPluginId($plugin_id, $configuration);
80         return $this->getFactory()->createInstance($fallback_id, $configuration);
81       }
82     }
83     else {
84       return $this->getFactory()->createInstance($plugin_id, $configuration);
85     }
86   }
87
88   /**
89    * {@inheritdoc}
90    */
91   public function getInstance(array $options) {
92     return $this->mapper->getInstance($options);
93   }
94
95 }