LocaleProjectStorage.php in Drupal 9
File
core/modules/locale/src/LocaleProjectStorage.php
View source
<?php
namespace Drupal\locale;
use Drupal\Core\KeyValueStore\KeyValueFactoryInterface;
class LocaleProjectStorage implements LocaleProjectStorageInterface {
protected $keyValueStore;
protected $cache = [];
protected static $all = FALSE;
public function __construct(KeyValueFactoryInterface $key_value_factory) {
$this->keyValueStore = $key_value_factory
->get('locale.project');
}
public function get($key, $default = NULL) {
$values = $this
->getMultiple([
$key,
]);
return isset($values[$key]) ? $values[$key] : $default;
}
public function getMultiple(array $keys) {
$values = [];
$load = [];
foreach ($keys as $key) {
if (isset($this->cache[$key])) {
$values[$key] = $this->cache[$key];
}
elseif (!array_key_exists($key, $this->cache)) {
$load[] = $key;
}
}
if ($load) {
$loaded_values = $this->keyValueStore
->getMultiple($load);
foreach ($load as $key) {
if (isset($loaded_values[$key])) {
$values[$key] = $loaded_values[$key];
$this->cache[$key] = $loaded_values[$key];
}
else {
$this->cache[$key] = NULL;
}
}
}
return $values;
}
public function set($key, $value) {
$this
->setMultiple([
$key => $value,
]);
}
public function setMultiple(array $data) {
foreach ($data as $key => $value) {
$this->cache[$key] = $value;
}
$this->keyValueStore
->setMultiple($data);
}
public function delete($key) {
$this
->deleteMultiple([
$key,
]);
}
public function deleteMultiple(array $keys) {
foreach ($keys as $key) {
$this->cache[$key] = NULL;
}
$this->keyValueStore
->deleteMultiple($keys);
}
public function resetCache() {
$this->cache = [];
static::$all = FALSE;
}
public function deleteAll() {
$this->keyValueStore
->deleteAll();
$this
->resetCache();
}
public function disableAll() {
$projects = $this->keyValueStore
->getAll();
foreach (array_keys($projects) as $key) {
$projects[$key]['status'] = 0;
if (isset($cache[$key])) {
$cache[$key] = $projects[$key];
}
}
$this->keyValueStore
->setMultiple($projects);
}
public function countProjects() {
return count($this
->getAll());
}
public function getAll() {
if (!static::$all) {
$this->cache = $this->keyValueStore
->getAll();
static::$all = TRUE;
}
return $this->cache;
}
}