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