Updated Drupal to 8.6. This goes with the following updates because it's possible...
[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|null
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         return $this->handlePluginNotFound($plugin_id, $configuration);
80       }
81     }
82     else {
83       return $this->getFactory()->createInstance($plugin_id, $configuration);
84     }
85   }
86
87   /**
88    * Allows plugin managers to specify custom behavior if a plugin is not found.
89    *
90    * @param string $plugin_id
91    *   The ID of the missing requested plugin.
92    * @param array $configuration
93    *   An array of configuration relevant to the plugin instance.
94    *
95    * @return object
96    *   A fallback plugin instance.
97    */
98   protected function handlePluginNotFound($plugin_id, array $configuration) {
99     $fallback_id = $this->getFallbackPluginId($plugin_id, $configuration);
100     return $this->getFactory()->createInstance($fallback_id, $configuration);
101   }
102
103   /**
104    * {@inheritdoc}
105    */
106   public function getInstance(array $options) {
107     if (!$this->mapper) {
108       throw new \BadMethodCallException(sprintf('%s does not support this method unless %s::$mapper is set.', static::class, static::class));
109     }
110     return $this->mapper->getInstance($options);
111   }
112
113 }