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