-
Notifications
You must be signed in to change notification settings - Fork 118
Expand file tree
/
Copy pathExecutionState.php
More file actions
205 lines (184 loc) · 5.16 KB
/
ExecutionState.php
File metadata and controls
205 lines (184 loc) · 5.16 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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
<?php
namespace Drupal\rules\Engine;
use Drupal\Core\TypedData\Exception\MissingDataException;
use Drupal\Core\TypedData\TypedDataInterface;
use Drupal\Core\TypedData\TypedDataTrait;
use Drupal\rules\Context\ContextDefinitionInterface;
use Drupal\rules\Context\GlobalContextRepositoryTrait;
use Drupal\rules\Exception\EvaluationException;
use Drupal\rules\Exception\InvalidArgumentException;
use Drupal\typed_data\DataFetcherTrait;
/**
* The rules execution state.
*
* A rule element may clone the state, so any added variables are only visible
* for elements in the current PHP-variable-scope.
*/
class ExecutionState implements ExecutionStateInterface {
use DataFetcherTrait;
use GlobalContextRepositoryTrait;
use TypedDataTrait;
/**
* Globally keeps the ids of rules blocked due to recursion prevention.
*
* @var array
*
* @todo Implement recursion prevention from D7.
*/
static protected $blocked = [];
/**
* The known variables.
*
* @var \Drupal\Core\TypedData\TypedDataInterface[]
*/
protected $variables = [];
/**
* Holds variables for auto-saving later.
*
* @var array
*/
protected $saveLater = [];
/**
* Variable for saving currently blocked configs for serialization.
*
* @var array
*/
protected $currentlyBlocked;
/**
* Creates the object.
*
* @param \Drupal\Core\TypedData\TypedDataInterface[] $variables
* (optional) Variables to initialize this state with.
*
* @return static
*/
public static function create($variables = []) {
return new static($variables);
}
/**
* Constructs the object.
*
* @param \Drupal\Core\TypedData\TypedDataInterface[] $variables
* (optional) Variables to initialize this state with.
*/
protected function __construct($variables) {
$this->variables = $variables;
}
/**
* {@inheritdoc}
*/
public function setVariable($name, ContextDefinitionInterface $definition, $value) {
$data = $this->getTypedDataManager()->create(
$definition->getDataDefinition(),
$value
);
$this->setVariableData($name, $data);
return $this;
}
/**
* {@inheritdoc}
*/
public function setVariableData($name, TypedDataInterface $data) {
$this->variables[$name] = $data;
return $this;
}
/**
* {@inheritdoc}
*/
public function getVariable($name) {
try {
return $this->variables[$name];
}
catch (InvalidArgumentException $e) {
throw new EvaluationException($e->getMessage(), 0, $e);
}
}
/**
* {@inheritdoc}
*/
public function getVariableValue($name) {
return $this->getVariable($name)->getValue();
}
/**
* {@inheritdoc}
*/
public function hasVariable($name) {
if (!array_key_exists($name, $this->variables)) {
// If there is no such variable, lazy-add global context variables. That
// way can safe time fetching global context if its not needed.
if (!($name[0] === '@' && strpos($name, ':') !== FALSE)) {
return FALSE;
}
$contexts = $this->getGlobalContextRepository()->getRuntimeContexts([$name]);
if (!array_key_exists($name, $contexts)) {
return FALSE;
}
$this->setVariableData($name, $contexts[$name]->getContextData());
}
return TRUE;
}
/**
* {@inheritdoc}
*/
public function removeVariable($name) {
if (array_key_exists($name, $this->variables)) {
unset($this->variables[$name]);
}
return $this;
}
/**
* {@inheritdoc}
*/
public function fetchDataByPropertyPath($property_path, $langcode = NULL) {
try {
// Support global context names as variable name by ignoring points in
// the service name; e.g. @user.current_user_context:current_user.name.
if ($property_path[0] == '@') {
list($service, $property_path) = explode(':', $property_path, 2);
}
$parts = explode('.', $property_path);
$var_name = array_shift($parts);
if (isset($service)) {
$var_name = $service . ':' . $var_name;
}
return $this
->getDataFetcher()
->fetchDataBySubPaths($this->getVariable($var_name), $parts, $langcode);
}
catch (InvalidArgumentException $e) {
// Pass on the original exception in the exception trace.
throw new EvaluationException($e->getMessage(), 0, $e);
}
catch (MissingDataException $e) {
// Pass on the original exception in the exception trace.
throw new EvaluationException($e->getMessage(), 0, $e);
}
}
/**
* {@inheritdoc}
*/
public function saveChangesLater($selector) {
$this->saveLater[$selector] = TRUE;
return $this;
}
/**
* {@inheritdoc}
*/
public function getAutoSaveSelectors() {
return array_keys($this->saveLater);
}
/**
* {@inheritdoc}
*/
public function autoSave() {
// Make changes permanent.
foreach ($this->saveLater as $selector => $flag) {
$typed_data = $this->fetchDataByPropertyPath($selector);
// Things that can be saved must have a save() method, right?
// Saving is always done at the root of the typed data tree, for example
// on the entity level.
$typed_data->getRoot()->getValue()->save();
}
return $this;
}
}