class State

Same name in this branch
  1. 11.x core/modules/workflows/src/State.php \Drupal\workflows\State
Same name in other branches
  1. 9 core/modules/workflows/src/State.php \Drupal\workflows\State
  2. 9 core/lib/Drupal/Core/State/State.php \Drupal\Core\State\State
  3. 8.9.x core/modules/workflows/src/State.php \Drupal\workflows\State
  4. 8.9.x core/lib/Drupal/Core/State/State.php \Drupal\Core\State\State
  5. 10 core/modules/workflows/src/State.php \Drupal\workflows\State
  6. 10 core/lib/Drupal/Core/State/State.php \Drupal\Core\State\State

Provides the state system using a key value store.

Hierarchy

Expanded class hierarchy of State

8 files declare their use of State
CheckpointStorageTest.php in core/tests/Drupal/Tests/Core/Config/Checkpoint/CheckpointStorageTest.php
CronTest.php in core/tests/Drupal/Tests/Core/CronTest.php
LegacyMatcherDumperTest.php in core/tests/Drupal/KernelTests/Core/Routing/LegacyMatcherDumperTest.php
MatcherDumperTest.php in core/tests/Drupal/KernelTests/Core/Routing/MatcherDumperTest.php
RendererBubblingTest.php in core/tests/Drupal/Tests/Core/Render/RendererBubblingTest.php

... See full list

78 string references to 'State'
AliasTest::testPrefixList in core/modules/path_alias/tests/src/Kernel/AliasTest.php
Tests the alias prefix.
AliasTest::testPrefixListCacheDeletionMidRequest in core/modules/path_alias/tests/src/Kernel/AliasTest.php
Tests situation where the prefix list cache is deleted mid-request.
AssetQueryStringTest::testResetGet in core/tests/Drupal/KernelTests/Core/Asset/AssetQueryStringTest.php
@covers ::get @covers ::reset
CacheableMetadataCalculationTest::create in core/modules/views/tests/modules/views_test_cacheable_metadata_calculation/src/Plugin/views/access/CacheableMetadataCalculationTest.php
Creates an instance of the plugin.
CacheableMetadataCalculationTest::setUp in core/modules/views/tests/src/Kernel/CacheableMetadataCalculationTest.php

... See full list

File

core/lib/Drupal/Core/State/State.php, line 13

Namespace

Drupal\Core\State
View source
class State extends CacheCollector implements StateInterface {
    
    /**
     * Information about all deprecated state, keyed by legacy state key.
     *
     * Each entry should be an array that defines the following keys:
     *   - 'replacement': The new name for the state.
     *   - 'message': The deprecation message to use for trigger_error().
     *
     * @var array
     */
    private static array $deprecatedState = [];
    
    /**
     * The key value store to use.
     *
     * @var \Drupal\Core\KeyValueStore\KeyValueStoreInterface
     */
    protected $keyValueStore;
    
    /**
     * Constructs a State object.
     *
     * @param \Drupal\Core\KeyValueStore\KeyValueFactoryInterface $key_value_factory
     *   The key value store to use.
     * @param \Drupal\Core\Cache\CacheBackendInterface $cache
     *   The cache backend.
     * @param \Drupal\Core\Lock\LockBackendInterface $lock
     *   The lock backend.
     */
    public function __construct(KeyValueFactoryInterface $key_value_factory, CacheBackendInterface $cache, LockBackendInterface $lock) {
        parent::__construct('state', $cache, $lock);
        $this->keyValueStore = $key_value_factory->get('state');
    }
    
    /**
     * {@inheritdoc}
     */
    public function get($key, $default = NULL) {
        // If the caller is asking for the value of a deprecated state, trigger a
        // deprecation message about it.
        if (isset(self::$deprecatedState[$key])) {
            // phpcs:ignore Drupal.Semantics.FunctionTriggerError
            @trigger_error(self::$deprecatedState[$key]['message'], E_USER_DEPRECATED);
            $key = self::$deprecatedState[$key]['replacement'];
        }
        return parent::get($key) ?? $default;
    }
    
    /**
     * {@inheritdoc}
     */
    protected function resolveCacheMiss($key) {
        $value = $this->keyValueStore
            ->get($key);
        $this->storage[$key] = $value;
        $this->persist($key);
        return $value;
    }
    
    /**
     * {@inheritdoc}
     */
    public function getMultiple(array $keys) {
        $values = [];
        foreach ($keys as $key) {
            $values[$key] = $this->get($key);
        }
        return $values;
    }
    
    /**
     * {@inheritdoc}
     */
    public function set($key, $value) {
        if (isset(self::$deprecatedState[$key])) {
            // phpcs:ignore Drupal.Semantics.FunctionTriggerError
            @trigger_error(self::$deprecatedState[$key]['message'], E_USER_DEPRECATED);
            $key = self::$deprecatedState[$key]['replacement'];
        }
        $this->keyValueStore
            ->set($key, $value);
        // If another request had a cache miss before this request, and also hasn't
        // written to cache yet, then it may already have read this value from the
        // database and could write that value to the cache to the end of the
        // request. To avoid this race condition, write to the cache immediately
        // after calling parent::set(). This allows the race condition detection in
        // CacheCollector::set() to work.
        parent::set($key, $value);
        $this->persist($key);
        static::updateCache();
    }
    
    /**
     * {@inheritdoc}
     */
    public function setMultiple(array $data) {
        $this->keyValueStore
            ->setMultiple($data);
        foreach ($data as $key => $value) {
            parent::set($key, $value);
            $this->persist($key);
        }
    }
    
    /**
     * {@inheritdoc}
     */
    public function delete($key) {
        $this->keyValueStore
            ->delete($key);
        parent::delete($key);
    }
    
    /**
     * {@inheritdoc}
     */
    public function deleteMultiple(array $keys) {
        $this->keyValueStore
            ->deleteMultiple($keys);
        foreach ($keys as $key) {
            parent::delete($key);
        }
    }
    
    /**
     * {@inheritdoc}
     */
    public function resetCache() {
        $this->clear();
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title Overrides
CacheCollector::$cache protected property The cache backend that should be used. 1
CacheCollector::$cacheCreated protected property Stores the cache creation time.
CacheCollector::$cacheInvalidated protected property Flag that indicates of the cache has been invalidated.
CacheCollector::$cacheLoaded protected property Indicates if the collected cache was already loaded.
CacheCollector::$cid protected property The cache id that is used for the cache entry.
CacheCollector::$keysToPersist protected property An array of keys to add to the cache on service termination.
CacheCollector::$keysToRemove protected property An array of keys to remove from the cache on service termination.
CacheCollector::$lock protected property The lock backend that should be used. 1
CacheCollector::$storage protected property Storage for the data itself.
CacheCollector::$tags protected property A list of tags that are used for the cache entry.
CacheCollector::clear public function Clears the collected cache entry. Overrides CacheCollectorInterface::clear 1
CacheCollector::destruct public function Performs destruct operations. Overrides DestructableInterface::destruct
CacheCollector::getCid protected function Gets the cache ID. 3
CacheCollector::has public function Returns whether data exists for this key. Overrides CacheCollectorInterface::has 1
CacheCollector::invalidateCache protected function Invalidate the cache.
CacheCollector::lazyLoadCache protected function Loads the cache if not already done. 1
CacheCollector::persist protected function Flags an offset value to be written to the persistent cache.
CacheCollector::reset public function Resets the local cache. Overrides CacheCollectorInterface::reset 1
CacheCollector::updateCache protected function Writes a value to the persistent cache immediately. 1
State::$deprecatedState private static property Information about all deprecated state, keyed by legacy state key.
State::$keyValueStore protected property The key value store to use.
State::delete public function Deletes the element. Overrides CacheCollector::delete
State::deleteMultiple public function Deletes multiple items. Overrides StateInterface::deleteMultiple
State::get public function Gets value from the cache. Overrides CacheCollector::get
State::getMultiple public function Returns the stored key/value pairs for a given set of keys. Overrides StateInterface::getMultiple
State::resetCache public function Resets the static cache. Overrides StateInterface::resetCache
State::resolveCacheMiss protected function Resolves a cache miss. Overrides CacheCollector::resolveCacheMiss
State::set public function Implements \Drupal\Core\Cache\CacheCollectorInterface::set(). Overrides CacheCollector::set
State::setMultiple public function Saves key/value pairs. Overrides StateInterface::setMultiple
State::__construct public function Constructs a State object. Overrides CacheCollector::__construct

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.