Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\Core\State;
|
Chris@0
|
4
|
Chris@0
|
5 use Drupal\Core\Cache\CacheBackendInterface;
|
Chris@0
|
6 use Drupal\Core\Cache\CacheCollector;
|
Chris@0
|
7 use Drupal\Core\KeyValueStore\KeyValueFactoryInterface;
|
Chris@0
|
8 use Drupal\Core\Lock\LockBackendInterface;
|
Chris@0
|
9
|
Chris@0
|
10 /**
|
Chris@0
|
11 * Provides the state system using a key value store.
|
Chris@0
|
12 */
|
Chris@0
|
13 class State extends CacheCollector implements StateInterface {
|
Chris@0
|
14
|
Chris@0
|
15 /**
|
Chris@0
|
16 * The key value store to use.
|
Chris@0
|
17 *
|
Chris@0
|
18 * @var \Drupal\Core\KeyValueStore\KeyValueStoreInterface
|
Chris@0
|
19 */
|
Chris@0
|
20 protected $keyValueStore;
|
Chris@0
|
21
|
Chris@0
|
22 /**
|
Chris@0
|
23 * Constructs a State object.
|
Chris@0
|
24 *
|
Chris@0
|
25 * @param \Drupal\Core\KeyValueStore\KeyValueFactoryInterface $key_value_factory
|
Chris@0
|
26 * The key value store to use.
|
Chris@0
|
27 * @param \Drupal\Core\Cache\CacheBackendInterface $cache
|
Chris@0
|
28 * The cache backend.
|
Chris@0
|
29 * @param \Drupal\Core\Lock\LockBackendInterface $lock
|
Chris@0
|
30 * The lock backend.
|
Chris@0
|
31 */
|
Chris@0
|
32 public function __construct(KeyValueFactoryInterface $key_value_factory, CacheBackendInterface $cache, LockBackendInterface $lock) {
|
Chris@0
|
33 parent::__construct('state', $cache, $lock);
|
Chris@0
|
34 $this->keyValueStore = $key_value_factory->get('state');
|
Chris@0
|
35 }
|
Chris@0
|
36
|
Chris@0
|
37 /**
|
Chris@0
|
38 * {@inheritdoc}
|
Chris@0
|
39 */
|
Chris@0
|
40 public function get($key, $default = NULL) {
|
Chris@0
|
41 $value = parent::get($key);
|
Chris@0
|
42 return $value !== NULL ? $value : $default;
|
Chris@0
|
43 }
|
Chris@0
|
44
|
Chris@0
|
45 /**
|
Chris@0
|
46 * {@inheritdoc}
|
Chris@0
|
47 */
|
Chris@0
|
48 protected function resolveCacheMiss($key) {
|
Chris@0
|
49 $value = $this->keyValueStore->get($key);
|
Chris@0
|
50 $this->storage[$key] = $value;
|
Chris@0
|
51 $this->persist($key);
|
Chris@0
|
52 return $value;
|
Chris@0
|
53 }
|
Chris@0
|
54
|
Chris@0
|
55 /**
|
Chris@0
|
56 * {@inheritdoc}
|
Chris@0
|
57 */
|
Chris@0
|
58 public function getMultiple(array $keys) {
|
Chris@0
|
59 $values = [];
|
Chris@0
|
60 foreach ($keys as $key) {
|
Chris@0
|
61 $values[$key] = $this->get($key);
|
Chris@0
|
62 }
|
Chris@0
|
63 return $values;
|
Chris@0
|
64 }
|
Chris@0
|
65
|
Chris@0
|
66 /**
|
Chris@0
|
67 * {@inheritdoc}
|
Chris@0
|
68 */
|
Chris@0
|
69 public function set($key, $value) {
|
Chris@0
|
70 parent::set($key, $value);
|
Chris@0
|
71 $this->keyValueStore->set($key, $value);
|
Chris@0
|
72 }
|
Chris@0
|
73
|
Chris@0
|
74 /**
|
Chris@0
|
75 * {@inheritdoc}
|
Chris@0
|
76 */
|
Chris@0
|
77 public function setMultiple(array $data) {
|
Chris@0
|
78 foreach ($data as $key => $value) {
|
Chris@0
|
79 parent::set($key, $value);
|
Chris@0
|
80 }
|
Chris@0
|
81 $this->keyValueStore->setMultiple($data);
|
Chris@0
|
82 }
|
Chris@0
|
83
|
Chris@0
|
84 /**
|
Chris@0
|
85 * {@inheritdoc}
|
Chris@0
|
86 */
|
Chris@0
|
87 public function delete($key) {
|
Chris@0
|
88 parent::delete($key);
|
Chris@0
|
89 $this->keyValueStore->delete($key);
|
Chris@0
|
90 }
|
Chris@0
|
91
|
Chris@0
|
92 /**
|
Chris@0
|
93 * {@inheritdoc}
|
Chris@0
|
94 */
|
Chris@0
|
95 public function deleteMultiple(array $keys) {
|
Chris@0
|
96 foreach ($keys as $key) {
|
Chris@0
|
97 parent::delete($key);
|
Chris@0
|
98 }
|
Chris@0
|
99 $this->keyValueStore->deleteMultiple($keys);
|
Chris@0
|
100 }
|
Chris@0
|
101
|
Chris@0
|
102 /**
|
Chris@0
|
103 * {@inheritdoc}
|
Chris@0
|
104 */
|
Chris@0
|
105 public function resetCache() {
|
Chris@0
|
106 $this->clear();
|
Chris@0
|
107 }
|
Chris@0
|
108
|
Chris@0
|
109 }
|