1f203fb2073cac8940a9da06a1fe6a75bdd7b5ef
[yaffs-website] / web / core / lib / Drupal / Component / Plugin / Factory / DefaultFactory.php
1 <?php
2
3 namespace Drupal\Component\Plugin\Factory;
4
5 use Drupal\Component\Plugin\Definition\PluginDefinitionInterface;
6 use Drupal\Component\Plugin\Discovery\DiscoveryInterface;
7 use Drupal\Component\Plugin\Exception\PluginException;
8
9 /**
10  * Default plugin factory.
11  *
12  * Instantiates plugin instances by passing the full configuration array as a
13  * single constructor argument. Plugin types wanting to support plugin classes
14  * with more flexible constructor signatures can do so by using an alternate
15  * factory such as Drupal\Component\Plugin\Factory\ReflectionFactory.
16  */
17 class DefaultFactory implements FactoryInterface {
18
19   /**
20    * The object that retrieves the definitions of the plugins that this factory instantiates.
21    *
22    * The plugin definition includes the plugin class and possibly other
23    * information necessary for proper instantiation.
24    *
25    * @var \Drupal\Component\Plugin\Discovery\DiscoveryInterface
26    */
27   protected $discovery;
28
29   /**
30    * Defines an interface each plugin should implement.
31    *
32    * @var string|null
33    */
34   protected $interface;
35
36   /**
37    * Constructs a Drupal\Component\Plugin\Factory\DefaultFactory object.
38    *
39    * @param \Drupal\Component\Plugin\Discovery\DiscoveryInterface $discovery
40    *   The plugin discovery.
41    * @param string|null $plugin_interface
42    *   (optional) The interface each plugin should implement.
43    */
44   public function __construct(DiscoveryInterface $discovery, $plugin_interface = NULL) {
45     $this->discovery = $discovery;
46     $this->interface = $plugin_interface;
47   }
48
49   /**
50    * {@inheritdoc}
51    */
52   public function createInstance($plugin_id, array $configuration = []) {
53     $plugin_definition = $this->discovery->getDefinition($plugin_id);
54     $plugin_class = static::getPluginClass($plugin_id, $plugin_definition, $this->interface);
55     return new $plugin_class($configuration, $plugin_id, $plugin_definition);
56   }
57
58   /**
59    * Finds the class relevant for a given plugin.
60    *
61    * @param string $plugin_id
62    *   The id of a plugin.
63    * @param \Drupal\Component\Plugin\Definition\PluginDefinitionInterface|mixed[] $plugin_definition
64    *   The plugin definition associated with the plugin ID.
65    * @param string $required_interface
66    *   (optional) THe required plugin interface.
67    *
68    * @return string
69    *   The appropriate class name.
70    *
71    * @throws \Drupal\Component\Plugin\Exception\PluginException
72    *   Thrown when there is no class specified, the class doesn't exist, or
73    *   the class does not implement the specified required interface.
74    */
75   public static function getPluginClass($plugin_id, $plugin_definition = NULL, $required_interface = NULL) {
76     $missing_class_message = sprintf('The plugin (%s) did not specify an instance class.', $plugin_id);
77     if (is_array($plugin_definition)) {
78       if (empty($plugin_definition['class'])) {
79         throw new PluginException($missing_class_message);
80       }
81
82       $class = $plugin_definition['class'];
83     }
84     elseif ($plugin_definition instanceof PluginDefinitionInterface) {
85       if (!$plugin_definition->getClass()) {
86         throw new PluginException($missing_class_message);
87       }
88
89       $class = $plugin_definition->getClass();
90     }
91     else {
92       $plugin_definition_type = is_object($plugin_definition) ? get_class($plugin_definition) : gettype($plugin_definition);
93       throw new PluginException(sprintf('%s can only handle plugin definitions that are arrays or that implement %s, but %s given.', __CLASS__, PluginDefinitionInterface::class, $plugin_definition_type));
94     }
95
96     if (!class_exists($class)) {
97       throw new PluginException(sprintf('Plugin (%s) instance class "%s" does not exist.', $plugin_id, $class));
98     }
99
100     if ($required_interface && !is_subclass_of($class, $required_interface)) {
101       throw new PluginException(sprintf('Plugin "%s" (%s) must implement interface %s.', $plugin_id, $class, $required_interface));
102     }
103
104     return $class;
105   }
106
107 }