class State

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

  • class \Drupal\Core\State\State implements \Drupal\Core\State\StateInterface

Expanded class hierarchy of State

6 files declare their use of State
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
RouteProviderTest.php in core/tests/Drupal/KernelTests/Core/Routing/RouteProviderTest.php

... See full list

78 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
Creates an instance of the plugin.
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 11

Namespace

Drupal\Core\State
View source
class State 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;
    
    /**
     * Static state cache.
     *
     * @var array
     */
    protected $cache = [];
    
    /**
     * Constructs a State object.
     *
     * @param \Drupal\Core\KeyValueStore\KeyValueFactoryInterface $key_value_factory
     *   The key value store to use.
     */
    public function __construct(KeyValueFactoryInterface $key_value_factory) {
        $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'];
        }
        $values = $this->getMultiple([
            $key,
        ]);
        return $values[$key] ?? $default;
    }
    
    /**
     * {@inheritdoc}
     */
    public function getMultiple(array $keys) {
        $values = [];
        $load = [];
        foreach ($keys as $key) {
            // Check if we have a value in the cache.
            if (isset($this->cache[$key])) {
                $values[$key] = $this->cache[$key];
            }
            elseif (!array_key_exists($key, $this->cache)) {
                $load[] = $key;
            }
        }
        if ($load) {
            $loaded_values = $this->keyValueStore
                ->getMultiple($load);
            foreach ($load as $key) {
                // If we find a value, even one that is NULL, add it to the cache and
                // return it.
                if (\array_key_exists($key, $loaded_values)) {
                    $values[$key] = $loaded_values[$key];
                    $this->cache[$key] = $loaded_values[$key];
                }
                else {
                    $this->cache[$key] = NULL;
                }
            }
        }
        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->cache[$key] = $value;
        $this->keyValueStore
            ->set($key, $value);
    }
    
    /**
     * {@inheritdoc}
     */
    public function setMultiple(array $data) {
        foreach ($data as $key => $value) {
            $this->cache[$key] = $value;
        }
        $this->keyValueStore
            ->setMultiple($data);
    }
    
    /**
     * {@inheritdoc}
     */
    public function delete($key) {
        $this->deleteMultiple([
            $key,
        ]);
    }
    
    /**
     * {@inheritdoc}
     */
    public function deleteMultiple(array $keys) {
        foreach ($keys as $key) {
            unset($this->cache[$key]);
        }
        $this->keyValueStore
            ->deleteMultiple($keys);
    }
    
    /**
     * {@inheritdoc}
     */
    public function resetCache() {
        $this->cache = [];
    }

}

Members

Title Sort descending Modifiers Object type Summary
State::$cache protected property Static state cache.
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
State::deleteMultiple public function
State::get public function
State::getMultiple public function
State::resetCache public function
State::set public function
State::setMultiple public function
State::__construct public function Constructs a State object.

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