PluginManagerBase.php 2.9 KB

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