MatcherBase.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php
  2. namespace Drupal\linkit;
  3. use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
  4. use Drupal\Core\Plugin\PluginBase;
  5. use Symfony\Component\DependencyInjection\ContainerInterface;
  6. /**
  7. * Provides a base class for matchers.
  8. *
  9. * @see plugin_api
  10. */
  11. abstract class MatcherBase extends PluginBase implements MatcherInterface, ContainerFactoryPluginInterface {
  12. /**
  13. * The matcher ID.
  14. *
  15. * @var string
  16. */
  17. protected $uuid;
  18. /**
  19. * The weight of the matcher compared to others in a matcher collection.
  20. *
  21. * @var int
  22. */
  23. protected $weight = 0;
  24. /**
  25. * {@inheritdoc}
  26. */
  27. public function __construct(array $configuration, $plugin_id, $plugin_definition) {
  28. parent::__construct($configuration, $plugin_id, $plugin_definition);
  29. $this->setConfiguration($configuration);
  30. }
  31. /**
  32. * {@inheritdoc}
  33. */
  34. public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
  35. return new static(
  36. $configuration,
  37. $plugin_id,
  38. $plugin_definition
  39. );
  40. }
  41. /**
  42. * {@inheritdoc}
  43. */
  44. public function getUuid() {
  45. return $this->uuid;
  46. }
  47. /**
  48. * {@inheritdoc}
  49. */
  50. public function getLabel() {
  51. return $this->pluginDefinition['label'];
  52. }
  53. /**
  54. * {@inheritdoc}
  55. */
  56. public function getSummary() {
  57. return [];
  58. }
  59. /**
  60. * {@inheritdoc}
  61. */
  62. public function getWeight() {
  63. return $this->weight;
  64. }
  65. /**
  66. * {@inheritdoc}
  67. */
  68. public function setWeight($weight) {
  69. return $this->weight = $weight;
  70. }
  71. /**
  72. * {@inheritdoc}
  73. */
  74. public function getConfiguration() {
  75. return [
  76. 'uuid' => $this->getUuid(),
  77. 'id' => $this->getPluginId(),
  78. 'weight' => $this->getWeight(),
  79. 'settings' => $this->configuration,
  80. ];
  81. }
  82. /**
  83. * {@inheritdoc}
  84. */
  85. public function setConfiguration(array $configuration) {
  86. $configuration += [
  87. 'uuid' => '',
  88. 'weight' => '0',
  89. 'settings' => [],
  90. ];
  91. $this->configuration = $configuration['settings'] + $this->defaultConfiguration();
  92. $this->uuid = $configuration['uuid'];
  93. $this->weight = $configuration['weight'];
  94. return $this;
  95. }
  96. /**
  97. * {@inheritdoc}
  98. */
  99. public function defaultConfiguration() {
  100. return [];
  101. }
  102. /**
  103. * {@inheritdoc}
  104. */
  105. public function calculateDependencies() {
  106. return [];
  107. }
  108. }