PluginBase.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <?php
  2. namespace Drupal\Component\Plugin;
  3. /**
  4. * Base class for plugins wishing to support metadata inspection.
  5. */
  6. abstract class PluginBase implements PluginInspectionInterface, DerivativeInspectionInterface {
  7. /**
  8. * A string which is used to separate base plugin IDs from the derivative ID.
  9. */
  10. const DERIVATIVE_SEPARATOR = ':';
  11. /**
  12. * The plugin_id.
  13. *
  14. * @var string
  15. */
  16. protected $pluginId;
  17. /**
  18. * The plugin implementation definition.
  19. *
  20. * @var array
  21. */
  22. protected $pluginDefinition;
  23. /**
  24. * Configuration information passed into the plugin.
  25. *
  26. * When using an interface like
  27. * \Drupal\Component\Plugin\ConfigurablePluginInterface, this is where the
  28. * configuration should be stored.
  29. *
  30. * Plugin configuration is optional, so plugin implementations must provide
  31. * their own setters and getters.
  32. *
  33. * @var array
  34. */
  35. protected $configuration;
  36. /**
  37. * Constructs a \Drupal\Component\Plugin\PluginBase object.
  38. *
  39. * @param array $configuration
  40. * A configuration array containing information about the plugin instance.
  41. * @param string $plugin_id
  42. * The plugin_id for the plugin instance.
  43. * @param mixed $plugin_definition
  44. * The plugin implementation definition.
  45. */
  46. public function __construct(array $configuration, $plugin_id, $plugin_definition) {
  47. $this->configuration = $configuration;
  48. $this->pluginId = $plugin_id;
  49. $this->pluginDefinition = $plugin_definition;
  50. }
  51. /**
  52. * {@inheritdoc}
  53. */
  54. public function getPluginId() {
  55. return $this->pluginId;
  56. }
  57. /**
  58. * {@inheritdoc}
  59. */
  60. public function getBaseId() {
  61. $plugin_id = $this->getPluginId();
  62. if (strpos($plugin_id, static::DERIVATIVE_SEPARATOR)) {
  63. list($plugin_id) = explode(static::DERIVATIVE_SEPARATOR, $plugin_id, 2);
  64. }
  65. return $plugin_id;
  66. }
  67. /**
  68. * {@inheritdoc}
  69. */
  70. public function getDerivativeId() {
  71. $plugin_id = $this->getPluginId();
  72. $derivative_id = NULL;
  73. if (strpos($plugin_id, static::DERIVATIVE_SEPARATOR)) {
  74. list(, $derivative_id) = explode(static::DERIVATIVE_SEPARATOR, $plugin_id, 2);
  75. }
  76. return $derivative_id;
  77. }
  78. /**
  79. * {@inheritdoc}
  80. */
  81. public function getPluginDefinition() {
  82. return $this->pluginDefinition;
  83. }
  84. }