f35c954596fc260e31aff65816e553a418405089
[yaffs-website] / web / core / lib / Drupal / Core / State / State.php
1 <?php
2
3 namespace Drupal\Core\State;
4
5 use Drupal\Core\KeyValueStore\KeyValueFactoryInterface;
6
7 /**
8  * Provides the state system using a key value store.
9  */
10 class State implements StateInterface {
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    * Constructs a State object.
28    *
29    * @param \Drupal\Core\KeyValueStore\KeyValueFactoryInterface $key_value_factory
30    *   The key value store to use.
31    */
32   public function __construct(KeyValueFactoryInterface $key_value_factory) {
33     $this->keyValueStore = $key_value_factory->get('state');
34   }
35
36   /**
37    * {@inheritdoc}
38    */
39   public function get($key, $default = NULL) {
40     $values = $this->getMultiple([$key]);
41     return isset($values[$key]) ? $values[$key] : $default;
42   }
43
44   /**
45    * {@inheritdoc}
46    */
47   public function getMultiple(array $keys) {
48     $values = [];
49     $load = [];
50     foreach ($keys as $key) {
51       // Check if we have a value in the cache.
52       if (isset($this->cache[$key])) {
53         $values[$key] = $this->cache[$key];
54       }
55       // Load the value if we don't have an explicit NULL value.
56       elseif (!array_key_exists($key, $this->cache)) {
57         $load[] = $key;
58       }
59     }
60
61     if ($load) {
62       $loaded_values = $this->keyValueStore->getMultiple($load);
63       foreach ($load as $key) {
64         // If we find a value, even one that is NULL, add it to the cache and
65         // return it.
66         if (isset($loaded_values[$key]) || array_key_exists($key, $loaded_values)) {
67           $values[$key] = $loaded_values[$key];
68           $this->cache[$key] = $loaded_values[$key];
69         }
70         else {
71           $this->cache[$key] = NULL;
72         }
73       }
74     }
75
76     return $values;
77   }
78
79   /**
80    * {@inheritdoc}
81    */
82   public function set($key, $value) {
83     $this->cache[$key] = $value;
84     $this->keyValueStore->set($key, $value);
85   }
86
87   /**
88    * {@inheritdoc}
89    */
90   public function setMultiple(array $data) {
91     foreach ($data as $key => $value) {
92       $this->cache[$key] = $value;
93     }
94     $this->keyValueStore->setMultiple($data);
95   }
96
97   /**
98    * {@inheritdoc}
99    */
100   public function delete($key) {
101     $this->deleteMultiple([$key]);
102   }
103
104   /**
105    * {@inheritdoc}
106    */
107   public function deleteMultiple(array $keys) {
108     foreach ($keys as $key) {
109       unset($this->cache[$key]);
110     }
111     $this->keyValueStore->deleteMultiple($keys);
112   }
113
114   /**
115    * {@inheritdoc}
116    */
117   public function resetCache() {
118     $this->cache = [];
119   }
120
121 }