DefaultSingleLazyPluginCollection.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. namespace Drupal\Core\Plugin;
  3. use Drupal\Component\Plugin\PluginHelper;
  4. use Drupal\Component\Plugin\PluginManagerInterface;
  5. use Drupal\Component\Plugin\LazyPluginCollection;
  6. use Drupal\Core\DependencyInjection\DependencySerializationTrait;
  7. /**
  8. * Provides a default plugin collection for a plugin type.
  9. *
  10. * A plugin collection usually stores multiple plugins, and is used to lazily
  11. * instantiate them. When only one plugin is needed, it is still best practice
  12. * to encapsulate all of the instantiation logic in a plugin collection. This
  13. * class can be used directly, or subclassed to add further exception handling
  14. * in self::initializePlugin().
  15. */
  16. class DefaultSingleLazyPluginCollection extends LazyPluginCollection {
  17. use DependencySerializationTrait;
  18. /**
  19. * The manager used to instantiate the plugins.
  20. *
  21. * @var \Drupal\Component\Plugin\PluginManagerInterface
  22. */
  23. protected $manager;
  24. /**
  25. * An array of configuration to instantiate the plugin with.
  26. *
  27. * @var array
  28. */
  29. protected $configuration;
  30. /**
  31. * The instance ID used for this plugin collection.
  32. *
  33. * @var string
  34. */
  35. protected $instanceId;
  36. /**
  37. * Constructs a new DefaultSingleLazyPluginCollection object.
  38. *
  39. * @param \Drupal\Component\Plugin\PluginManagerInterface $manager
  40. * The manager to be used for instantiating plugins.
  41. * @param string $instance_id
  42. * The ID of the plugin instance.
  43. * @param array $configuration
  44. * An array of configuration.
  45. */
  46. public function __construct(PluginManagerInterface $manager, $instance_id, array $configuration) {
  47. $this->manager = $manager;
  48. $this->addInstanceId($instance_id, $configuration);
  49. }
  50. /**
  51. * {@inheritdoc}
  52. */
  53. protected function initializePlugin($instance_id) {
  54. $this->set($instance_id, $this->manager->createInstance($instance_id, $this->configuration));
  55. }
  56. /**
  57. * {@inheritdoc}
  58. */
  59. public function getConfiguration() {
  60. $plugin = $this->get($this->instanceId);
  61. if (PluginHelper::isConfigurable($plugin)) {
  62. return $plugin->getConfiguration();
  63. }
  64. else {
  65. return $this->configuration;
  66. }
  67. }
  68. /**
  69. * {@inheritdoc}
  70. */
  71. public function setConfiguration($configuration) {
  72. $this->configuration = $configuration;
  73. $plugin = $this->get($this->instanceId);
  74. if (PluginHelper::isConfigurable($plugin)) {
  75. $plugin->setConfiguration($configuration);
  76. }
  77. return $this;
  78. }
  79. /**
  80. * {@inheritdoc}
  81. */
  82. public function addInstanceId($id, $configuration = NULL) {
  83. $this->instanceId = $id;
  84. // Reset the list of instance IDs since there can be only one.
  85. $this->instanceIds = [];
  86. parent::addInstanceId($id, $configuration);
  87. if ($configuration !== NULL) {
  88. $this->setConfiguration($configuration);
  89. }
  90. }
  91. }