123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113 |
- <?php
- namespace Drupal\Component\Plugin;
- use Drupal\Component\Plugin\Discovery\DiscoveryTrait;
- use Drupal\Component\Plugin\Exception\PluginNotFoundException;
- /**
- * Base class for plugin managers.
- */
- abstract class PluginManagerBase implements PluginManagerInterface {
- use DiscoveryTrait;
- /**
- * The object that discovers plugins managed by this manager.
- *
- * @var \Drupal\Component\Plugin\Discovery\DiscoveryInterface
- */
- protected $discovery;
- /**
- * The object that instantiates plugins managed by this manager.
- *
- * @var \Drupal\Component\Plugin\Factory\FactoryInterface
- */
- protected $factory;
- /**
- * The object that returns the preconfigured plugin instance appropriate for a particular runtime condition.
- *
- * @var \Drupal\Component\Plugin\Mapper\MapperInterface|null
- */
- protected $mapper;
- /**
- * Gets the plugin discovery.
- *
- * @return \Drupal\Component\Plugin\Discovery\DiscoveryInterface
- */
- protected function getDiscovery() {
- return $this->discovery;
- }
- /**
- * Gets the plugin factory.
- *
- * @return \Drupal\Component\Plugin\Factory\FactoryInterface
- */
- protected function getFactory() {
- return $this->factory;
- }
- /**
- * {@inheritdoc}
- */
- public function getDefinition($plugin_id, $exception_on_invalid = TRUE) {
- return $this->getDiscovery()->getDefinition($plugin_id, $exception_on_invalid);
- }
- /**
- * {@inheritdoc}
- */
- public function getDefinitions() {
- return $this->getDiscovery()->getDefinitions();
- }
- /**
- * {@inheritdoc}
- */
- public function createInstance($plugin_id, array $configuration = []) {
- // If this PluginManager has fallback capabilities catch
- // PluginNotFoundExceptions.
- if ($this instanceof FallbackPluginManagerInterface) {
- try {
- return $this->getFactory()->createInstance($plugin_id, $configuration);
- }
- catch (PluginNotFoundException $e) {
- return $this->handlePluginNotFound($plugin_id, $configuration);
- }
- }
- else {
- return $this->getFactory()->createInstance($plugin_id, $configuration);
- }
- }
- /**
- * Allows plugin managers to specify custom behavior if a plugin is not found.
- *
- * @param string $plugin_id
- * The ID of the missing requested plugin.
- * @param array $configuration
- * An array of configuration relevant to the plugin instance.
- *
- * @return object
- * A fallback plugin instance.
- */
- protected function handlePluginNotFound($plugin_id, array $configuration) {
- $fallback_id = $this->getFallbackPluginId($plugin_id, $configuration);
- return $this->getFactory()->createInstance($fallback_id, $configuration);
- }
- /**
- * {@inheritdoc}
- */
- public function getInstance(array $options) {
- if (!$this->mapper) {
- throw new \BadMethodCallException(sprintf('%s does not support this method unless %s::$mapper is set.', static::class, static::class));
- }
- return $this->mapper->getInstance($options);
- }
- }
|