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