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