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