ContextAwarePluginBase.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <?php
  2. namespace Drupal\Component\Plugin;
  3. use Drupal\Component\Plugin\Context\ContextInterface;
  4. use Drupal\Component\Plugin\Exception\ContextException;
  5. use Drupal\Component\Plugin\Context\Context;
  6. use Symfony\Component\Validator\ConstraintViolationList;
  7. /**
  8. * Base class for plugins that are context aware.
  9. */
  10. abstract class ContextAwarePluginBase extends PluginBase implements ContextAwarePluginInterface {
  11. /**
  12. * The data objects representing the context of this plugin.
  13. *
  14. * @var \Drupal\Component\Plugin\Context\ContextInterface[]
  15. */
  16. protected $context = [];
  17. /**
  18. * Overrides \Drupal\Component\Plugin\PluginBase::__construct().
  19. *
  20. * Overrides the construction of context aware plugins to allow for
  21. * unvalidated constructor based injection of contexts.
  22. *
  23. * @param array $configuration
  24. * The plugin configuration, i.e. an array with configuration values keyed
  25. * by configuration option name. The special key 'context' may be used to
  26. * initialize the defined contexts by setting it to an array of context
  27. * values keyed by context names.
  28. * @param string $plugin_id
  29. * The plugin_id for the plugin instance.
  30. * @param mixed $plugin_definition
  31. * The plugin implementation definition.
  32. */
  33. public function __construct(array $configuration, $plugin_id, $plugin_definition) {
  34. $context_configuration = isset($configuration['context']) ? $configuration['context'] : [];
  35. unset($configuration['context']);
  36. parent::__construct($configuration, $plugin_id, $plugin_definition);
  37. $this->contexts = $this->createContextFromConfiguration($context_configuration);
  38. }
  39. /**
  40. * Creates context objects from any context mappings in configuration.
  41. *
  42. * @param array $context_configuration
  43. * An associative array of context names and values.
  44. *
  45. * @return \Drupal\Component\Plugin\Context\ContextInterface[]
  46. * An array of context objects.
  47. */
  48. protected function createContextFromConfiguration(array $context_configuration) {
  49. $contexts = [];
  50. foreach ($context_configuration as $key => $value) {
  51. $context_definition = $this->getContextDefinition($key);
  52. $contexts[$key] = new Context($context_definition, $value);
  53. }
  54. return $contexts;
  55. }
  56. /**
  57. * {@inheritdoc}
  58. */
  59. public function getContextDefinitions() {
  60. $definition = $this->getPluginDefinition();
  61. return !empty($definition['context']) ? $definition['context'] : [];
  62. }
  63. /**
  64. * {@inheritdoc}
  65. */
  66. public function getContextDefinition($name) {
  67. $definition = $this->getPluginDefinition();
  68. if (empty($definition['context'][$name])) {
  69. throw new ContextException(sprintf("The %s context is not a valid context.", $name));
  70. }
  71. return $definition['context'][$name];
  72. }
  73. /**
  74. * {@inheritdoc}
  75. */
  76. public function getContexts() {
  77. // Make sure all context objects are initialized.
  78. foreach ($this->getContextDefinitions() as $name => $definition) {
  79. $this->getContext($name);
  80. }
  81. return $this->context;
  82. }
  83. /**
  84. * {@inheritdoc}
  85. */
  86. public function getContext($name) {
  87. // Check for a valid context value.
  88. if (!isset($this->context[$name])) {
  89. $this->context[$name] = new Context($this->getContextDefinition($name));
  90. }
  91. return $this->context[$name];
  92. }
  93. /**
  94. * {@inheritdoc}
  95. */
  96. public function setContext($name, ContextInterface $context) {
  97. $this->context[$name] = $context;
  98. }
  99. /**
  100. * {@inheritdoc}
  101. */
  102. public function getContextValues() {
  103. $values = [];
  104. foreach ($this->getContextDefinitions() as $name => $definition) {
  105. $values[$name] = isset($this->context[$name]) ? $this->context[$name]->getContextValue() : NULL;
  106. }
  107. return $values;
  108. }
  109. /**
  110. * {@inheritdoc}
  111. */
  112. public function getContextValue($name) {
  113. return $this->getContext($name)->getContextValue();
  114. }
  115. /**
  116. * {@inheritdoc}
  117. */
  118. public function setContextValue($name, $value) {
  119. $this->context[$name] = new Context($this->getContextDefinition($name), $value);
  120. return $this;
  121. }
  122. /**
  123. * {@inheritdoc}
  124. */
  125. public function validateContexts() {
  126. $violations = new ConstraintViolationList();
  127. // @todo: Implement symfony validator API to let the validator traverse
  128. // and set property paths accordingly.
  129. foreach ($this->getContexts() as $context) {
  130. $violations->addAll($context->validate());
  131. }
  132. return $violations;
  133. }
  134. }