Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\Core\State;
|
Chris@0
|
4
|
Chris@0
|
5 use Drupal\Core\KeyValueStore\KeyValueFactoryInterface;
|
Chris@0
|
6
|
Chris@0
|
7 /**
|
Chris@0
|
8 * Provides the state system using a key value store.
|
Chris@0
|
9 */
|
Chris@17
|
10 class State implements StateInterface {
|
Chris@0
|
11
|
Chris@0
|
12 /**
|
Chris@0
|
13 * The key value store to use.
|
Chris@0
|
14 *
|
Chris@0
|
15 * @var \Drupal\Core\KeyValueStore\KeyValueStoreInterface
|
Chris@0
|
16 */
|
Chris@0
|
17 protected $keyValueStore;
|
Chris@0
|
18
|
Chris@0
|
19 /**
|
Chris@17
|
20 * Static state cache.
|
Chris@17
|
21 *
|
Chris@17
|
22 * @var array
|
Chris@17
|
23 */
|
Chris@17
|
24 protected $cache = [];
|
Chris@17
|
25
|
Chris@17
|
26 /**
|
Chris@0
|
27 * Constructs a State object.
|
Chris@0
|
28 *
|
Chris@0
|
29 * @param \Drupal\Core\KeyValueStore\KeyValueFactoryInterface $key_value_factory
|
Chris@0
|
30 * The key value store to use.
|
Chris@0
|
31 */
|
Chris@17
|
32 public function __construct(KeyValueFactoryInterface $key_value_factory) {
|
Chris@0
|
33 $this->keyValueStore = $key_value_factory->get('state');
|
Chris@0
|
34 }
|
Chris@0
|
35
|
Chris@0
|
36 /**
|
Chris@0
|
37 * {@inheritdoc}
|
Chris@0
|
38 */
|
Chris@0
|
39 public function get($key, $default = NULL) {
|
Chris@17
|
40 $values = $this->getMultiple([$key]);
|
Chris@17
|
41 return isset($values[$key]) ? $values[$key] : $default;
|
Chris@0
|
42 }
|
Chris@0
|
43
|
Chris@0
|
44 /**
|
Chris@0
|
45 * {@inheritdoc}
|
Chris@0
|
46 */
|
Chris@0
|
47 public function getMultiple(array $keys) {
|
Chris@0
|
48 $values = [];
|
Chris@17
|
49 $load = [];
|
Chris@0
|
50 foreach ($keys as $key) {
|
Chris@17
|
51 // Check if we have a value in the cache.
|
Chris@17
|
52 if (isset($this->cache[$key])) {
|
Chris@17
|
53 $values[$key] = $this->cache[$key];
|
Chris@17
|
54 }
|
Chris@17
|
55 // Load the value if we don't have an explicit NULL value.
|
Chris@17
|
56 elseif (!array_key_exists($key, $this->cache)) {
|
Chris@17
|
57 $load[] = $key;
|
Chris@17
|
58 }
|
Chris@0
|
59 }
|
Chris@17
|
60
|
Chris@17
|
61 if ($load) {
|
Chris@17
|
62 $loaded_values = $this->keyValueStore->getMultiple($load);
|
Chris@17
|
63 foreach ($load as $key) {
|
Chris@17
|
64 // If we find a value, even one that is NULL, add it to the cache and
|
Chris@17
|
65 // return it.
|
Chris@17
|
66 if (isset($loaded_values[$key]) || array_key_exists($key, $loaded_values)) {
|
Chris@17
|
67 $values[$key] = $loaded_values[$key];
|
Chris@17
|
68 $this->cache[$key] = $loaded_values[$key];
|
Chris@17
|
69 }
|
Chris@17
|
70 else {
|
Chris@17
|
71 $this->cache[$key] = NULL;
|
Chris@17
|
72 }
|
Chris@17
|
73 }
|
Chris@17
|
74 }
|
Chris@17
|
75
|
Chris@0
|
76 return $values;
|
Chris@0
|
77 }
|
Chris@0
|
78
|
Chris@0
|
79 /**
|
Chris@0
|
80 * {@inheritdoc}
|
Chris@0
|
81 */
|
Chris@0
|
82 public function set($key, $value) {
|
Chris@17
|
83 $this->cache[$key] = $value;
|
Chris@0
|
84 $this->keyValueStore->set($key, $value);
|
Chris@0
|
85 }
|
Chris@0
|
86
|
Chris@0
|
87 /**
|
Chris@0
|
88 * {@inheritdoc}
|
Chris@0
|
89 */
|
Chris@0
|
90 public function setMultiple(array $data) {
|
Chris@0
|
91 foreach ($data as $key => $value) {
|
Chris@17
|
92 $this->cache[$key] = $value;
|
Chris@0
|
93 }
|
Chris@0
|
94 $this->keyValueStore->setMultiple($data);
|
Chris@0
|
95 }
|
Chris@0
|
96
|
Chris@0
|
97 /**
|
Chris@0
|
98 * {@inheritdoc}
|
Chris@0
|
99 */
|
Chris@0
|
100 public function delete($key) {
|
Chris@17
|
101 $this->deleteMultiple([$key]);
|
Chris@0
|
102 }
|
Chris@0
|
103
|
Chris@0
|
104 /**
|
Chris@0
|
105 * {@inheritdoc}
|
Chris@0
|
106 */
|
Chris@0
|
107 public function deleteMultiple(array $keys) {
|
Chris@0
|
108 foreach ($keys as $key) {
|
Chris@17
|
109 unset($this->cache[$key]);
|
Chris@0
|
110 }
|
Chris@0
|
111 $this->keyValueStore->deleteMultiple($keys);
|
Chris@0
|
112 }
|
Chris@0
|
113
|
Chris@0
|
114 /**
|
Chris@0
|
115 * {@inheritdoc}
|
Chris@0
|
116 */
|
Chris@0
|
117 public function resetCache() {
|
Chris@17
|
118 $this->cache = [];
|
Chris@0
|
119 }
|
Chris@0
|
120
|
Chris@0
|
121 }
|