class ExecutionMetadataState

The state used during configuration time holding data definitions.

Hierarchy

Expanded class hierarchy of ExecutionMetadataState

2 files declare their use of ExecutionMetadataState
PrepareExecutionMetadataStateTest.php in tests/src/Unit/Integration/Engine/PrepareExecutionMetadataStateTest.php
RulesComponent.php in src/Engine/RulesComponent.php

File

src/Context/ExecutionMetadataState.php, line 14

Namespace

Drupal\rules\Context
View source
class ExecutionMetadataState implements ExecutionMetadataStateInterface {
    use DataFetcherTrait;
    use GlobalContextRepositoryTrait;
    
    /**
     * The known data definitions.
     *
     * @var \Drupal\Core\TypedData\DataDefinitionInterface
     */
    protected $dataDefinitions = [];
    
    /**
     * {@inheritdoc}
     */
    public static function create(array $data_definitions = []) {
        return new static($data_definitions);
    }
    
    /**
     * Constructs the object.
     *
     * @param \Drupal\Core\TypedData\DataDefinitionInterface[] $data_definitions
     *   (optional) Data definitions to initialize this state with.
     */
    protected function __construct(array $data_definitions) {
        $this->dataDefinitions = $data_definitions;
        // Add definitions of all global contexts.
        $contexts = $this->getGlobalContextRepository()
            ->getAvailableContexts();
        foreach ($contexts as $name => $context) {
            $this->setDataDefinition($name, $context->getContextDefinition()
                ->getDataDefinition());
        }
    }
    
    /**
     * {@inheritdoc}
     */
    public function setDataDefinition($name, DataDefinitionInterface $definition) {
        $this->dataDefinitions[$name] = $definition;
        return $this;
    }
    
    /**
     * {@inheritdoc}
     */
    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];
    }
    
    /**
     * {@inheritdoc}
     */
    public function hasDataDefinition($name) {
        return array_key_exists($name, $this->dataDefinitions);
    }
    
    /**
     * {@inheritdoc}
     */
    public function removeDataDefinition($name) {
        if (array_key_exists($name, $this->dataDefinitions)) {
            unset($this->dataDefinitions[$name]);
        }
        return $this;
    }
    
    /**
     * {@inheritdoc}
     */
    public function fetchDefinitionByPropertyPath($property_path, $langcode = LanguageInterface::LANGCODE_NOT_SPECIFIED) {
        try {
            // Support global context names as variable name by ignoring points in
            // the service name; e.g. @user.current_user_context:current_user.name.
            if (isset($property_path[0]) && $property_path[0] == '@') {
                [
                    $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) {
            // Pass on the original exception in the exception trace.
            throw new IntegrityException($e->getMessage(), 0, $e);
        }
    }
    
    /**
     * {@inheritdoc}
     */
    public function autocomplete($partial_property_path) {
        return $this->getDataFetcher()
            ->autocompletePropertyPath($this->dataDefinitions, $partial_property_path);
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title
ExecutionMetadataState::$dataDefinitions protected property The known data definitions.
ExecutionMetadataState::autocomplete public function
ExecutionMetadataState::create public static function Creates the object. Overrides ExecutionMetadataStateInterface::create
ExecutionMetadataState::fetchDefinitionByPropertyPath public function Applies a data selector and returns the corresponding data definition. Overrides ExecutionMetadataStateInterface::fetchDefinitionByPropertyPath
ExecutionMetadataState::getDataDefinition public function Retrieve a data definition in this execution metadata state. Overrides ExecutionMetadataStateInterface::getDataDefinition
ExecutionMetadataState::hasDataDefinition public function Checks if the variable with the given name is present in the state. Overrides ExecutionMetadataStateInterface::hasDataDefinition
ExecutionMetadataState::removeDataDefinition public function Removes a data definition from the metadata state. Overrides ExecutionMetadataStateInterface::removeDataDefinition
ExecutionMetadataState::setDataDefinition public function Sets a data definition in the execution metadata state. Overrides ExecutionMetadataStateInterface::setDataDefinition
ExecutionMetadataState::__construct protected function Constructs the object.
GlobalContextRepositoryTrait::$contextRepository protected property The global context repository.
GlobalContextRepositoryTrait::getGlobalContextRepository public function Gets the global context repository.
GlobalContextRepositoryTrait::setGlobalContextRepository public function Sets the global context repository.