3 namespace Drupal\locale;
5 use Drupal\Core\KeyValueStore\KeyValueFactoryInterface;
8 * Provides the locale project storage system using a key value store.
10 class LocaleProjectStorage implements LocaleProjectStorageInterface {
13 * The key value store to use.
15 * @var \Drupal\Core\KeyValueStore\KeyValueStoreInterface
17 protected $keyValueStore;
24 protected $cache = [];
31 protected static $all = FALSE;
34 * Constructs a State object.
36 * @param \Drupal\Core\KeyValueStore\KeyValueFactoryInterface $key_value_factory
37 * The key value store to use.
39 public function __construct(KeyValueFactoryInterface $key_value_factory) {
40 $this->keyValueStore = $key_value_factory->get('locale.project');
46 public function get($key, $default = NULL) {
47 $values = $this->getMultiple([$key]);
48 return isset($values[$key]) ? $values[$key] : $default;
54 public function getMultiple(array $keys) {
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];
62 // Load the value if we don't have an explicit NULL value.
63 elseif (!array_key_exists($key, $this->cache)) {
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
73 if (isset($loaded_values[$key])) {
74 $values[$key] = $loaded_values[$key];
75 $this->cache[$key] = $loaded_values[$key];
78 $this->cache[$key] = NULL;
89 public function set($key, $value) {
90 $this->setMultiple([$key => $value]);
96 public function setMultiple(array $data) {
97 foreach ($data as $key => $value) {
98 $this->cache[$key] = $value;
100 $this->keyValueStore->setMultiple($data);
106 public function delete($key) {
107 $this->deleteMultiple([$key]);
113 public function deleteMultiple(array $keys) {
114 foreach ($keys as $key) {
115 $this->cache[$key] = NULL;
117 $this->keyValueStore->deleteMultiple($keys);
123 public function resetCache() {
125 static::$all = FALSE;
131 public function deleteAll() {
132 $this->keyValueStore->deleteAll();
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];
147 $this->keyValueStore->setMultiple($projects);
154 public function countProjects() {
155 return count($this->getAll());
161 public function getAll() {
163 $this->cache = $this->keyValueStore->getAll();