Mercurial > hg > isophonics-drupal-site
comparison core/modules/locale/src/LocaleProjectStorage.php @ 0:4c8ae668cc8c
Initial import (non-working)
author | Chris Cannam |
---|---|
date | Wed, 29 Nov 2017 16:09:58 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:4c8ae668cc8c |
---|---|
1 <?php | |
2 | |
3 namespace Drupal\locale; | |
4 | |
5 use Drupal\Core\KeyValueStore\KeyValueFactoryInterface; | |
6 | |
7 /** | |
8 * Provides the locale project storage system using a key value store. | |
9 */ | |
10 class LocaleProjectStorage implements LocaleProjectStorageInterface { | |
11 | |
12 /** | |
13 * The key value store to use. | |
14 * | |
15 * @var \Drupal\Core\KeyValueStore\KeyValueStoreInterface | |
16 */ | |
17 protected $keyValueStore; | |
18 | |
19 /** | |
20 * Static state cache. | |
21 * | |
22 * @var array | |
23 */ | |
24 protected $cache = []; | |
25 | |
26 /** | |
27 * Cache status flag. | |
28 * | |
29 * @var bool | |
30 */ | |
31 protected static $all = FALSE; | |
32 | |
33 /** | |
34 * Constructs a State object. | |
35 * | |
36 * @param \Drupal\Core\KeyValueStore\KeyValueFactoryInterface $key_value_factory | |
37 * The key value store to use. | |
38 */ | |
39 public function __construct(KeyValueFactoryInterface $key_value_factory) { | |
40 $this->keyValueStore = $key_value_factory->get('locale.project'); | |
41 } | |
42 | |
43 /** | |
44 * {@inheritdoc} | |
45 */ | |
46 public function get($key, $default = NULL) { | |
47 $values = $this->getMultiple([$key]); | |
48 return isset($values[$key]) ? $values[$key] : $default; | |
49 } | |
50 | |
51 /** | |
52 * {@inheritdoc} | |
53 */ | |
54 public function getMultiple(array $keys) { | |
55 $values = []; | |
56 $load = []; | |
57 foreach ($keys as $key) { | |
58 // Check if we have a value in the cache. | |
59 if (isset($this->cache[$key])) { | |
60 $values[$key] = $this->cache[$key]; | |
61 } | |
62 // Load the value if we don't have an explicit NULL value. | |
63 elseif (!array_key_exists($key, $this->cache)) { | |
64 $load[] = $key; | |
65 } | |
66 } | |
67 | |
68 if ($load) { | |
69 $loaded_values = $this->keyValueStore->getMultiple($load); | |
70 foreach ($load as $key) { | |
71 // If we find a value, even one that is NULL, add it to the cache and | |
72 // return it. | |
73 if (isset($loaded_values[$key])) { | |
74 $values[$key] = $loaded_values[$key]; | |
75 $this->cache[$key] = $loaded_values[$key]; | |
76 } | |
77 else { | |
78 $this->cache[$key] = NULL; | |
79 } | |
80 } | |
81 } | |
82 | |
83 return $values; | |
84 } | |
85 | |
86 /** | |
87 * {@inheritdoc} | |
88 */ | |
89 public function set($key, $value) { | |
90 $this->setMultiple([$key => $value]); | |
91 } | |
92 | |
93 /** | |
94 * {@inheritdoc} | |
95 */ | |
96 public function setMultiple(array $data) { | |
97 foreach ($data as $key => $value) { | |
98 $this->cache[$key] = $value; | |
99 } | |
100 $this->keyValueStore->setMultiple($data); | |
101 } | |
102 | |
103 /** | |
104 * {@inheritdoc} | |
105 */ | |
106 public function delete($key) { | |
107 $this->deleteMultiple([$key]); | |
108 } | |
109 | |
110 /** | |
111 * {@inheritdoc} | |
112 */ | |
113 public function deleteMultiple(array $keys) { | |
114 foreach ($keys as $key) { | |
115 $this->cache[$key] = NULL; | |
116 } | |
117 $this->keyValueStore->deleteMultiple($keys); | |
118 } | |
119 | |
120 /** | |
121 * {@inheritdoc} | |
122 */ | |
123 public function resetCache() { | |
124 $this->cache = []; | |
125 static::$all = FALSE; | |
126 } | |
127 | |
128 /** | |
129 * {@inheritdoc} | |
130 */ | |
131 public function deleteAll() { | |
132 $this->keyValueStore->deleteAll(); | |
133 $this->resetCache(); | |
134 } | |
135 | |
136 /** | |
137 * {@inheritdoc} | |
138 */ | |
139 public function disableAll() { | |
140 $projects = $this->keyValueStore->getAll(); | |
141 foreach (array_keys($projects) as $key) { | |
142 $projects[$key]['status'] = 0; | |
143 if (isset($cache[$key])) { | |
144 $cache[$key] = $projects[$key]; | |
145 } | |
146 } | |
147 $this->keyValueStore->setMultiple($projects); | |
148 | |
149 } | |
150 | |
151 /** | |
152 * {@inheritdoc} | |
153 */ | |
154 public function countProjects() { | |
155 return count($this->getAll()); | |
156 } | |
157 | |
158 /** | |
159 * {@inheritdoc} | |
160 */ | |
161 public function getAll() { | |
162 if (!static::$all) { | |
163 $this->cache = $this->keyValueStore->getAll(); | |
164 static::$all = TRUE; | |
165 } | |
166 return $this->cache; | |
167 } | |
168 | |
169 } |