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