View source
<?php
namespace Drupal\rules\Plugin\RulesExpression;
use Drupal\Core\Logger\LoggerChannelInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\rules\Context\ContextConfig;
use Drupal\rules\Context\ExecutionMetadataStateInterface;
use Drupal\rules\Context\ExecutionStateInterface;
use Drupal\rules\Engine\ActionExpressionContainerInterface;
use Drupal\rules\Engine\ActionExpressionInterface;
use Drupal\rules\Engine\ConditionExpressionContainerInterface;
use Drupal\rules\Engine\ConditionExpressionInterface;
use Drupal\rules\Engine\ExpressionBase;
use Drupal\rules\Engine\ExpressionInterface;
use Drupal\rules\Engine\ExpressionManagerInterface;
use Drupal\rules\Engine\RuleExpressionInterface;
use Drupal\rules\Exception\InvalidExpressionException;
use Symfony\Component\DependencyInjection\ContainerInterface;
class RuleExpression extends ExpressionBase implements RuleExpressionInterface, ContainerFactoryPluginInterface {
protected $expressionManager;
protected $conditions;
protected $actions;
protected $rulesDebugLogger;
public function __construct(array $configuration, $plugin_id, array $plugin_definition, ExpressionManagerInterface $expression_manager, LoggerChannelInterface $logger) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$configuration += [
'conditions' => [],
'actions' => [],
];
$this->conditions = $expression_manager
->createInstance('rules_and', $configuration['conditions']);
$this->conditions
->setRoot($this
->getRoot());
$this->actions = $expression_manager
->createInstance('rules_action_set', $configuration['actions']);
$this->actions
->setRoot($this
->getRoot());
$this->expressionManager = $expression_manager;
$this->rulesDebugLogger = $logger;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('plugin.manager.rules_expression'), $container
->get('logger.channel.rules_debug'));
}
public function executeWithState(ExecutionStateInterface $state) {
$this->rulesDebugLogger
->info('Evaluating conditions of rule %label.', [
'%label' => $this
->getLabel(),
'element' => $this,
]);
if (!$this->conditions
->isEmpty() && !$this->conditions
->executeWithState($state)) {
return;
}
$this->rulesDebugLogger
->info('Rule %label fires.', [
'%label' => $this
->getLabel(),
'element' => $this,
'scope' => TRUE,
]);
$this->actions
->executeWithState($state);
$this->rulesDebugLogger
->info('Rule %label has fired.', [
'%label' => $this
->getLabel(),
'element' => $this,
'scope' => FALSE,
]);
}
public function addCondition($condition_id, ContextConfig $config = NULL) {
return $this->conditions
->addCondition($condition_id, $config);
}
public function getConditions() {
return $this->conditions;
}
public function setConditions(ConditionExpressionContainerInterface $conditions) {
$this->conditions = $conditions;
return $this;
}
public function addAction($action_id, ContextConfig $config = NULL) {
return $this->actions
->addAction($action_id, $config);
}
public function getActions() {
return $this->actions;
}
public function setActions(ActionExpressionContainerInterface $actions) {
$this->actions = $actions;
return $this;
}
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;
}
public function addExpression($plugin_id, ContextConfig $config = NULL) {
return $this
->addExpressionObject($this->expressionManager
->createInstance($plugin_id, $config ? $config
->toArray() : []));
}
public function getConfiguration() {
$configuration = parent::getConfiguration();
$configuration['conditions'] = $this->conditions
->getConfiguration();
$configuration['actions'] = $this->actions
->getConfiguration();
return $configuration;
}
public function getIterator() {
return $this->actions
->getIterator();
}
public function getExpression($uuid) {
$condition = $this->conditions
->getExpression($uuid);
if ($condition) {
return $condition;
}
return $this->actions
->getExpression($uuid);
}
public function deleteExpression($uuid) {
$deleted = $this->conditions
->deleteExpression($uuid);
if (!$deleted) {
$deleted = $this->actions
->deleteExpression($uuid);
}
return $deleted;
}
public function checkIntegrity(ExecutionMetadataStateInterface $metadata_state, $apply_assertions = TRUE) {
$violation_list = $this->conditions
->checkIntegrity($metadata_state, $apply_assertions);
$violation_list
->addAll($this->actions
->checkIntegrity($metadata_state, $apply_assertions));
return $violation_list;
}
public function prepareExecutionMetadataState(ExecutionMetadataStateInterface $metadata_state, ExpressionInterface $until = NULL, $apply_assertions = TRUE) {
$found = $this->conditions
->prepareExecutionMetadataState($metadata_state, $until, $apply_assertions);
if ($found) {
return TRUE;
}
return $this->actions
->prepareExecutionMetadataState($metadata_state, $until, $apply_assertions);
}
public function __clone() {
$this->actions = clone $this->actions;
$this->actions
->setRoot($this
->getRoot());
$this->conditions = clone $this->conditions;
$this->conditions
->setRoot($this
->getRoot());
}
}