ExecutionMetadataState.php in Rules 8.3
File
src/Context/ExecutionMetadataState.php
View source
<?php
namespace Drupal\rules\Context;
use Drupal\Core\Language\LanguageInterface;
use Drupal\Core\TypedData\DataDefinitionInterface;
use Drupal\rules\Exception\IntegrityException;
use Drupal\typed_data\DataFetcherTrait;
use Drupal\typed_data\Exception\TypedDataException;
class ExecutionMetadataState implements ExecutionMetadataStateInterface {
use DataFetcherTrait;
use GlobalContextRepositoryTrait;
protected $dataDefinitions = [];
public static function create(array $data_definitions = []) {
return new static($data_definitions);
}
protected function __construct(array $data_definitions) {
$this->dataDefinitions = $data_definitions;
$contexts = $this
->getGlobalContextRepository()
->getAvailableContexts();
foreach ($contexts as $name => $context) {
$this
->setDataDefinition($name, $context
->getContextDefinition()
->getDataDefinition());
}
}
public function setDataDefinition($name, DataDefinitionInterface $definition) {
$this->dataDefinitions[$name] = $definition;
return $this;
}
public function getDataDefinition($name) {
if (!array_key_exists($name, $this->dataDefinitions)) {
throw new IntegrityException("Unable to get variable '{$name}'; it is not defined.");
}
return $this->dataDefinitions[$name];
}
public function hasDataDefinition($name) {
return array_key_exists($name, $this->dataDefinitions);
}
public function removeDataDefinition($name) {
if (array_key_exists($name, $this->dataDefinitions)) {
unset($this->dataDefinitions[$name]);
}
return $this;
}
public function fetchDefinitionByPropertyPath($property_path, $langcode = LanguageInterface::LANGCODE_NOT_SPECIFIED) {
try {
if (isset($property_path[0]) && $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()
->fetchDefinitionBySubPaths($this
->getDataDefinition($var_name), $parts, $langcode);
} catch (TypedDataException $e) {
throw new IntegrityException($e
->getMessage(), 0, $e);
}
}
public function autocomplete($partial_property_path) {
return $this
->getDataFetcher()
->autocompletePropertyPath($this->dataDefinitions, $partial_property_path);
}
}