View source
<?php
namespace Drupal\rules\Plugin\RulesExpression;
use Drupal\Core\Logger\LoggerChannelInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\rules\Context\DataProcessorManager;
use Drupal\rules\Context\ExecutionMetadataStateInterface;
use Drupal\rules\Context\ExecutionStateInterface;
use Drupal\rules\Core\ConditionManager;
use Drupal\rules\Engine\ConditionExpressionInterface;
use Drupal\rules\Engine\ExpressionBase;
use Drupal\rules\Engine\ExpressionInterface;
use Drupal\rules\Context\ContextHandlerIntegrityTrait;
use Drupal\rules\Engine\IntegrityViolationList;
use Symfony\Component\DependencyInjection\ContainerInterface;
class ConditionExpression extends ExpressionBase implements ConditionExpressionInterface, ContainerFactoryPluginInterface {
use ContextHandlerIntegrityTrait;
protected $conditionManager;
protected $rulesDebugLogger;
public function __construct(array $configuration, $plugin_id, $plugin_definition, ConditionManager $condition_manager, DataProcessorManager $processor_manager, LoggerChannelInterface $logger) {
$configuration += $this
->defaultConfiguration();
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->conditionManager = $condition_manager;
$this->processorManager = $processor_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.condition'), $container
->get('plugin.manager.rules_data_processor'), $container
->get('logger.channel.rules_debug'));
}
public function defaultConfiguration() {
return [
'negate' => FALSE,
];
}
public function setConfiguration(array $configuration) {
if (isset($this->configuration['condition_id'])) {
$configuration += [
'condition_id' => $this->configuration['condition_id'],
];
}
return parent::setConfiguration($configuration);
}
public function executeWithState(ExecutionStateInterface $state) {
$condition = $this->conditionManager
->createInstance($this->configuration['condition_id'], [
'negate' => $this->configuration['negate'],
]);
$this
->prepareContext($condition, $state);
$result = $condition
->evaluate();
if ($this
->isNegated()) {
$result = !$result;
}
$this->rulesDebugLogger
->info('The condition %name evaluated to %bool.', [
'%name' => $this
->getLabel(),
'%bool' => $result ? 'TRUE' : 'FALSE',
'element' => $this,
]);
$this
->addProvidedContext($condition, $state);
return $result;
}
public function negate($negate = TRUE) {
$this->configuration['negate'] = $negate;
return $this;
}
public function isNegated() {
return !empty($this->configuration['negate']);
}
public function getLabel() {
if (!empty($this->configuration['condition_id'])) {
$definition = $this->conditionManager
->getDefinition($this->configuration['condition_id']);
if ($this
->isNegated()) {
return $this
->t('@not @label', [
'@not' => $this
->t('NOT'),
'@label' => $definition['label'],
]);
}
else {
return $definition['label'];
}
}
return parent::getLabel();
}
public function getFormHandler() {
if (isset($this->pluginDefinition['form_class'])) {
$class_name = $this->pluginDefinition['form_class'];
return new $class_name($this, $this->conditionManager);
}
}
public function checkIntegrity(ExecutionMetadataStateInterface $metadata_state, $apply_assertions = TRUE) {
$violation_list = new IntegrityViolationList();
if (empty($this->configuration['condition_id'])) {
$violation_list
->addViolationWithMessage($this
->t('Condition plugin ID is missing'), $this
->getUuid());
return $violation_list;
}
if (!$this->conditionManager
->hasDefinition($this->configuration['condition_id'])) {
$violation_list
->addViolationWithMessage($this
->t('Condition plugin %plugin_id does not exist', [
'%plugin_id' => $this->configuration['condition_id'],
]), $this
->getUuid());
return $violation_list;
}
$condition = $this->conditionManager
->createInstance($this->configuration['condition_id'], [
'negate' => $this->configuration['negate'],
]);
$this
->prepareContextWithMetadata($condition, $metadata_state);
$result = $this
->checkContextConfigIntegrity($condition, $metadata_state);
$this
->prepareExecutionMetadataState($metadata_state, NULL, $apply_assertions);
return $result;
}
public function prepareExecutionMetadataState(ExecutionMetadataStateInterface $metadata_state, ExpressionInterface $until = NULL, $apply_assertions = TRUE) {
if ($until && $this
->getUuid() === $until
->getUuid()) {
return TRUE;
}
$condition = $this->conditionManager
->createInstance($this->configuration['condition_id'], [
'negate' => $this->configuration['negate'],
]);
$this
->prepareContextWithMetadata($condition, $metadata_state);
$this
->addProvidedContextDefinitions($condition, $metadata_state);
if ($apply_assertions && !$this
->isNegated()) {
$this
->assertMetadata($condition, $metadata_state);
}
}
}