ExecutionState.php in Rules 8.3
File
src/Context/ExecutionState.php
View source
<?php
namespace Drupal\rules\Context;
use Drupal\Core\TypedData\Exception\MissingDataException;
use Drupal\Core\TypedData\TypedDataInterface;
use Drupal\Core\TypedData\TypedDataTrait;
use Drupal\rules\Exception\EvaluationException;
use Drupal\rules\Exception\InvalidArgumentException;
use Drupal\typed_data\DataFetcherTrait;
class ExecutionState implements ExecutionStateInterface {
use DataFetcherTrait;
use GlobalContextRepositoryTrait;
use TypedDataTrait;
protected static $blocked = [];
protected $variables = [];
protected $saveLater = [];
protected $currentlyBlocked;
public static function create(array $variables = []) {
return new static($variables);
}
protected function __construct(array $variables) {
$this->variables = $variables;
}
public function setVariable($name, ContextDefinitionInterface $definition, $value) {
$data = $this
->getTypedDataManager()
->create($definition
->getDataDefinition(), $value);
$this
->setVariableData($name, $data);
return $this;
}
public function setVariableData($name, TypedDataInterface $data) {
$this->variables[$name] = $data;
return $this;
}
public function getVariable($name) {
if (!$this
->hasVariable($name)) {
throw new EvaluationException("Unable to get variable '{$name}'; it is not defined.");
}
return $this->variables[$name];
}
public function getVariableValue($name) {
return $this
->getVariable($name)
->getValue();
}
public function hasVariable($name) {
if (!array_key_exists($name, $this->variables)) {
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;
}
public function removeVariable($name) {
if (array_key_exists($name, $this->variables)) {
unset($this->variables[$name]);
}
return $this;
}
public function fetchDataByPropertyPath($property_path, $langcode = NULL) {
try {
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) {
throw new EvaluationException($e
->getMessage(), 0, $e);
} catch (MissingDataException $e) {
throw new EvaluationException($e
->getMessage(), 0, $e);
}
}
public function saveChangesLater($selector) {
$this->saveLater[$selector] = TRUE;
return $this;
}
public function getAutoSaveSelectors() {
return array_keys($this->saveLater);
}
public function autoSave() {
foreach ($this->saveLater as $selector => $flag) {
$typed_data = $this
->fetchDataByPropertyPath($selector);
$typed_data
->getRoot()
->getValue()
->save();
}
return $this;
}
}