-
Notifications
You must be signed in to change notification settings - Fork 118
Expand file tree
/
Copy pathContextHandlerIntegrityTrait.php
More file actions
157 lines (143 loc) · 6.95 KB
/
ContextHandlerIntegrityTrait.php
File metadata and controls
157 lines (143 loc) · 6.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
<?php
namespace Drupal\rules\Context;
use Drupal\Core\Plugin\Context\ContextDefinitionInterface as CoreContextDefinitionInterface;
use Drupal\Core\Plugin\ContextAwarePluginInterface as CoreContextAwarePluginInterface;
use Drupal\Core\TypedData\DataDefinitionInterface;
//@codingStandardsIgnoreStart
use Drupal\rules\Context\ContextDefinitionInterface as RulesContextDefinitionInterface;
//@codingStandardsIgnoreEnd
use Drupal\rules\Engine\ExecutionMetadataStateInterface;
use Drupal\rules\Engine\IntegrityViolation;
use Drupal\rules\Engine\IntegrityViolationList;
use Drupal\rules\Exception\RulesIntegrityException;
/**
* Extends the context handler trait with support for checking integrity.
*/
trait ContextHandlerIntegrityTrait {
use ContextHandlerTrait;
/**
* Performs the integrity check.
*
* @param CoreContextAwarePluginInterface $plugin
* The plugin with its defined context.
* @param \Drupal\rules\Engine\ExecutionMetadataStateInterface $metadata_state
* The current configuration state with all defined variables that are
* available.
*
* @return \Drupal\rules\Engine\IntegrityViolationList
* The list of integrity violations.
*/
protected function checkContextConfigIntegrity(CoreContextAwarePluginInterface $plugin, ExecutionMetadataStateInterface $metadata_state) {
$violation_list = new IntegrityViolationList();
$context_definitions = $plugin->getContextDefinitions();
// Make sure that all provided variables by this plugin are added to the
// execution metadata state.
$this->addProvidedContextDefinitions($plugin, $metadata_state);
foreach ($context_definitions as $name => $context_definition) {
// Check if a data selector is configured that maps to the state.
if (isset($this->configuration['context_mapping'][$name])) {
try {
$data_definition = $this->getMappedDefinition($name, $metadata_state);
$this->checkDataTypeCompatible($context_definition, $data_definition, $name, $violation_list);
}
catch (RulesIntegrityException $e) {
$violation = new IntegrityViolation();
$violation->setMessage($this->t('Data selector %selector for context %context_name is invalid. @message', [
'%selector' => $this->configuration['context_mapping'][$name],
'%context_name' => $context_definition->getLabel(),
'@message' => $e->getMessage(),
]));
$violation->setContextName($name);
$violation->setUuid($this->getUuid());
$violation_list->add($violation);
}
if ($context_definition instanceof RulesContextDefinitionInterface
&& $context_definition->getAssignmentRestriction() === RulesContextDefinitionInterface::ASSIGNMENT_RESTRICTION_INPUT
) {
$violation = new IntegrityViolation();
$violation->setMessage($this->t('The context %context_name may not be configured using a selector.', [
'%context_name' => $context_definition->getLabel(),
]));
$violation->setContextName($name);
$violation->setUuid($this->getUuid());
$violation_list->add($violation);
}
}
elseif (isset($this->configuration['context_values'][$name])) {
if ($context_definition instanceof RulesContextDefinitionInterface
&& $context_definition->getAssignmentRestriction() === RulesContextDefinitionInterface::ASSIGNMENT_RESTRICTION_SELECTOR
) {
$violation = new IntegrityViolation();
$violation->setMessage($this->t('The context %context_name may only be configured using a selector.', [
'%context_name' => $context_definition->getLabel(),
]));
$violation->setContextName($name);
$violation->setUuid($this->getUuid());
$violation_list->add($violation);
}
}
elseif ($context_definition->isRequired() && $context_definition->getDefaultValue() === NULL) {
$violation = new IntegrityViolation();
$violation->setMessage($this->t('The required context %context_name is missing.', [
'%context_name' => $context_definition->getLabel(),
]));
$violation->setContextName($name);
$violation->setUuid($this->getUuid());
$violation_list->add($violation);
}
}
if ($plugin instanceof ContextProviderInterface) {
$provided_context_definitions = $plugin->getProvidedContextDefinitions();
foreach ($provided_context_definitions as $name => $context_definition) {
if (isset($this->configuration['provides_mapping'][$name])
&& !preg_match('/^[0-9a-zA-Z_]*$/', $this->configuration['provides_mapping'][$name])
) {
$violation = new IntegrityViolation();
$violation->setMessage($this->t('Provided variable name %name contains not allowed characters.', [
'%name' => $this->configuration['provides_mapping'][$name],
]));
$violation->setContextName($name);
$violation->setUuid($this->getUuid());
$violation_list->add($violation);
}
}
}
return $violation_list;
}
/**
* Checks that the data type of a mapped variable matches the expectation.
*
* @param \Drupal\Core\Plugin\Context\ContextDefinitionInterface $context_definition
* The context definition of the context on the plugin.
* @param \Drupal\Core\TypedData\DataDefinitionInterface $provided
* The data definition of the mapped variable to the context.
* @param string $context_name
* The name of the context on the plugin.
* @param \Drupal\rules\Engine\IntegrityViolationList $violation_list
* The list of violations where new ones will be added.
*/
protected function checkDataTypeCompatible(CoreContextDefinitionInterface $context_definition, DataDefinitionInterface $provided, $context_name, IntegrityViolationList $violation_list) {
// Compare data types. For now, fail if they are not equal.
// @todo: Add support for matching based upon type-inheritance.
$target_type = $context_definition->getDataDefinition()->getDataType();
// Special case any and entity target types for now.
if ($target_type == 'any' ||
($target_type == 'entity' && strpos($provided->getDataType(), 'entity:') !== FALSE) ||
($target_type == 'list' && $context_definition->isMultiple())
) {
return;
}
if ($target_type != $provided->getDataType()) {
$expected_type_problem = $context_definition->getDataDefinition()->getDataType();
$violation = new IntegrityViolation();
$violation->setMessage($this->t('Expected a @expected_type data type for context %context_name but got a @provided_type data type instead.', [
'@expected_type' => $expected_type_problem,
'%context_name' => $context_definition->getLabel(),
'@provided_type' => $provided->getDataType(),
]));
$violation->setContextName($context_name);
$violation->setUuid($this->getUuid());
$violation_list->add($violation);
}
}
}