class State in Drupal 8
Same name in this branch
- 8 core/modules/workflows/src/State.php \Drupal\workflows\State
- 8 core/lib/Drupal/Core/State/State.php \Drupal\Core\State\State
Same name and namespace in other branches
- 9 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 StateInterface
Expanded class hierarchy of State
4 files declare their use of State
- MatcherDumperTest.php in core/
tests/ Drupal/ KernelTests/ Core/ Routing/ MatcherDumperTest.php - RendererBubblingTest.php in core/
tests/ Drupal/ Tests/ Core/ Render/ RendererBubblingTest.php - Contains \Drupal\Tests\Core\Render\RendererBubblingTest.
- RouteProviderTest.php in core/
tests/ Drupal/ KernelTests/ Core/ Routing/ RouteProviderTest.php - Contains \Drupal\KernelTests\Core\Routing\RouteProviderTest.
- ThemeExtensionListTest.php in core/
tests/ Drupal/ Tests/ Core/ Extension/ ThemeExtensionListTest.php
3 string references to 'State'
- core.services.yml in core/
core.services.yml - core/core.services.yml
- WorkflowEditForm::form in core/
modules/ workflows/ src/ Form/ WorkflowEditForm.php - Gets the actual form array to be built.
- workflows_help in core/
modules/ workflows/ workflows.module - Implements hook_help().
1 service uses State
File
- core/
lib/ Drupal/ Core/ State/ State.php, line 10
Namespace
Drupal\Core\StateView source
class State implements StateInterface {
/**
* 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) {
$values = $this
->getMultiple([
$key,
]);
return isset($values[$key]) ? $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 (isset($loaded_values[$key]) || 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) {
$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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
State:: |
protected | property | Static state cache. | |
State:: |
protected | property | The key value store to use. | |
State:: |
public | function |
Deletes an item. Overrides StateInterface:: |
|
State:: |
public | function |
Deletes multiple items. Overrides StateInterface:: |
|
State:: |
public | function |
Returns the stored value for a given key. Overrides StateInterface:: |
|
State:: |
public | function |
Returns the stored key/value pairs for a given set of keys. Overrides StateInterface:: |
|
State:: |
public | function |
Resets the static cache. Overrides StateInterface:: |
|
State:: |
public | function |
Saves a value for a given key. Overrides StateInterface:: |
|
State:: |
public | function |
Saves key/value pairs. Overrides StateInterface:: |
|
State:: |
public | function | Constructs a State object. |