class State

Same name in this branch
  1. 10 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. 11.x core/modules/workflows/src/State.php \Drupal\workflows\State
  6. 11.x 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

7 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

76 string references to 'State'
AliasTest::testWhitelist in core/modules/path_alias/tests/src/Kernel/AliasTest.php
Tests the alias whitelist.
AliasTest::testWhitelistCacheDeletionMidRequest in core/modules/path_alias/tests/src/Kernel/AliasTest.php
Tests situation where the whitelist 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
CacheableMetadataCalculationTest::setUp in core/modules/views/tests/src/Kernel/CacheableMetadataCalculationTest.php

... See full list

1 service uses State
state in core/core.services.yml
Drupal\Core\State\State

File

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

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 = [
        'system.css_js_query_string' => [
            'replacement' => AssetQueryString::STATE_KEY,
            'message' => 'The \'system.css_js_query_string\' state is deprecated in drupal:10.2.0. Use \\Drupal\\Core\\Asset\\AssetQueryStringInterface::get() and ::reset() instead. See https://www.drupal.org/node/3358337.',
        ],
    ];
    
    /**
     * 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 = NULL, ?LockBackendInterface $lock = NULL) {
        if (!$cache) {
            @trigger_error('Calling  ' . __METHOD__ . '() without the $cache argument is deprecated in drupal:10.3.0 and is required in drupal:11.0.0. See https://www.drupal.org/node/3177901', E_USER_DEPRECATED);
            $cache = \Drupal::cache('bootstrap');
        }
        if (!$lock) {
            @trigger_error('Calling  ' . __METHOD__ . '() without the $lock argument is deprecated in drupal:10.3.0 and is required in drupal:11.0.0. See https://www.drupal.org/node/3177901', E_USER_DEPRECATED);
            $lock = \Drupal::service('lock');
        }
        parent::__construct('state', $cache, $lock);
        $this->keyValueStore = $key_value_factory->get('state');
        // For backward compatibility, allow to opt-out of state caching, if cache
        // is not explicitly enabled, flag the cache as already loaded.
        if (Settings::get('state_cache') !== TRUE) {
            $this->cacheLoaded = TRUE;
        }
    }
    
    /**
     * {@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();
    }
    
    /**
     * {@inheritdoc}
     */
    protected function updateCache($lock = TRUE) {
        // For backward compatibility, allow to opt-out of state caching, if cache
        // is not explicitly enabled, there is no need to update it.
        if (Settings::get('state_cache') !== TRUE) {
            return;
        }
        parent::updateCache($lock);
    }
    
    /**
     * {@inheritdoc}
     */
    protected function invalidateCache() {
        // For backward compatibility, allow to opt-out of state caching, if cache
        // is not explicitly enabled, there is no need to invalidate it.
        if (Settings::get('state_cache') !== TRUE) {
            return;
        }
        parent::invalidateCache();
    }

}

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 Overrides CacheCollectorInterface::clear 1
CacheCollector::destruct public function Overrides DestructableInterface::destruct
CacheCollector::getCid protected function Gets the cache ID. 3
CacheCollector::has public function Overrides CacheCollectorInterface::has 1
CacheCollector::lazyLoadCache protected function Loads the cache if not already done. 1
CacheCollector::normalizeLockName protected function Normalizes a cache ID in order to comply with database limitations. 1
CacheCollector::persist protected function Flags an offset value to be written to the persistent cache.
CacheCollector::reset public function Overrides CacheCollectorInterface::reset 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 Overrides CacheCollector::delete
State::deleteMultiple public function
State::get public function Overrides CacheCollector::get
State::getMultiple public function
State::invalidateCache protected function Overrides CacheCollector::invalidateCache
State::resetCache public function
State::resolveCacheMiss protected function Overrides CacheCollector::resolveCacheMiss
State::set public function Overrides CacheCollector::set
State::setMultiple public function
State::updateCache protected function Overrides CacheCollector::updateCache
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.