-
Notifications
You must be signed in to change notification settings - Fork 117
Expand file tree
/
Copy pathRule.php
More file actions
223 lines (199 loc) · 6.66 KB
/
Copy pathRule.php
File metadata and controls
223 lines (199 loc) · 6.66 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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
<?php
/**
* @file
* Contains \Drupal\rules\Plugin\Expression\Rule.
*/
namespace Drupal\rules\Plugin\Expression;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Plugin\Context\ContextDefinition;
use Drupal\rules\Core\RulesActionBase;
use Drupal\rules\Context\ContextConfig;
use Drupal\rules\Engine\ActionExpressionContainerInterface;
use Drupal\rules\Engine\ConditionExpressionContainerInterface;
use Drupal\rules\Engine\ActionExpressionInterface;
use Drupal\rules\Engine\ConditionExpressionInterface;
use Drupal\rules\Engine\ExpressionInterface;
use Drupal\rules\Engine\ExpressionTrait;
use Drupal\rules\Engine\RulesState;
use Drupal\rules\Exception\InvalidExpressionException;
use Drupal\rules\Engine\ExpressionPluginManager;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Provides a rule, executing actions when conditions are met.
*
* Actions added to a rule can also be rules themselves, so it is possible to
* nest several rules into one rule. This is the functionality of so called
* "rule sets" in Drupal 7.
*
* @Expression(
* id = "rules_rule",
* label = @Translation("A rule, executing actions when conditions are met.")
* )
*/
class Rule extends RulesActionBase implements RuleInterface, ContainerFactoryPluginInterface {
use ExpressionTrait;
/**
* List of conditions that must be met before actions are executed.
*
* @var \Drupal\rules\Engine\ConditionExpressionContainerInterface
*/
protected $conditions;
/**
* List of actions that get executed if the conditions are met.
*
* @var \Drupal\rules\Engine\ActionExpressionContainerInterface
*/
protected $actions;
/**
* Constructs a new class instance.
*
* @param array $configuration
* A configuration array containing information about the plugin instance.
* @param string $plugin_id
* The plugin_id for the plugin instance.
* @param array $plugin_definition
* The plugin implementation definition.
* @param \Drupal\rules\Engine\ExpressionPluginManager $expression_manager
* The rules expression plugin manager.
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, ExpressionPluginManager $expression_manager) {
// @todo: This needs to be removed again and we need to add proper derivative handling for Rules.
if (isset($configuration['context_definitions'])) {
$plugin_definition['context'] = $this->createContextDefinitions($configuration['context_definitions']);
}
parent::__construct($configuration, $plugin_id, $plugin_definition);
$configuration += ['conditions' => [], 'actions' => []];
// Per default the outer condition container of a rule is initialized as
// conjunction (AND), meaning that all conditions in it must evaluate to
// TRUE to fire the actions.
$this->conditions = $expression_manager->createInstance('rules_and', $configuration['conditions']);
$this->actions = $expression_manager->createInstance('rules_action_set', $configuration['actions']);
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static(
$configuration,
$plugin_id,
$plugin_definition,
$container->get('plugin.manager.expression')
);
}
/**
* Converts a context definition configuration array into an object.
*
* @todo This should be replaced by some convenience method on the
* ContextDefinition class in core?
*
* @param array $configuration
* The configuration properties for populating the context definition
* object.
*
* @return \Drupal\Core\Plugin\Context\ContextDefinitionInterface[]
* A list of context definitions keyed by the context name.
*/
protected function createContextDefinitions(array $configuration) {
$context_definitions = [];
foreach ($configuration as $context_name => $definition_array) {
$definition_array += [
'type' => 'any',
'label' => NULL,
'required' => TRUE,
'multiple' => FALSE,
'description' => NULL,
];
$context_definitions[$context_name] = new ContextDefinition(
$definition_array['type'], $definition_array['label'],
$definition_array['required'], $definition_array['multiple'],
$definition_array['description']
);
}
return $context_definitions;
}
/**
* {@inheritdoc}
*/
public function executeWithState(RulesState $state) {
// Evaluate the rule's conditions.
if (!$this->conditions->isEmpty() && !$this->conditions->executeWithState($state)) {
// Do not run the actions if the conditions are not met.
return;
}
$this->actions->executeWithState($state);
}
/**
* {@inheritdoc}
*/
public function addCondition($condition_id, ContextConfig $config = NULL) {
$this->conditions->addCondition($condition_id, $config);
return $this;
}
/**
* {@inheritdoc}
*/
public function getConditions() {
return $this->conditions;
}
/**
* {@inheritdoc}
*/
public function setConditions(ConditionExpressionContainerInterface $conditions) {
$this->conditions = $conditions;
return $this;
}
/**
* {@inheritdoc}
*/
public function addAction($action_id, ContextConfig $config = NULL) {
$this->actions->addAction($action_id, $config);
return $this;
}
/**
* {@inheritdoc}
*/
public function getActions() {
return $this->actions;
}
/**
* {@inheritdoc}
*/
public function setActions(ActionExpressionContainerInterface $actions) {
$this->actions = $actions;
return $this;
}
/**
* {@inheritdoc}
*/
public function addExpressionObject(ExpressionInterface $expression) {
if ($expression instanceof ConditionExpressionInterface) {
$this->conditions->addExpressionObject($expression);
}
elseif ($expression instanceof ActionExpressionInterface) {
$this->actions->addExpressionObject($expression);
}
else {
throw new InvalidExpressionException();
}
return $this;
}
/**
* {@inheritdoc}
*/
public function addExpression($plugin_id, ContextConfig $config = NULL) {
return $this->addExpressionObject(
$this->expressionManager->createInstance($plugin_id, $config ? $config->toArray() : [])
);
}
/**
* {@inheritdoc}
*/
public function getConfiguration() {
$configuration = parent::getConfiguration();
// We need to update the configuration in case actions/conditions have been
// added or changed.
$configuration['conditions'] = $this->conditions->getConfiguration();
$configuration['actions'] = $this->actions->getConfiguration();
return $configuration;
}
}