LazyArrayStorage.php
3.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
<?php
namespace YahnisElsts\AdminMenuEditor\Customizable\Storage;
use ameMultiDictionary;
/**
* A simple storage implementation that stores data in a PHP array.
*
* This class has extension points for lazy-loading and saving data,
* but it doesn't implement them by default. Subclasses can override
* methods like storeData() to implement these features. On its own,
* this class acts as an in-memory store.
*/
class LazyArrayStorage extends StorageMethods implements StorageInterface {
/**
* @var array|null
*/
protected $data = null;
protected $doneLoading = false;
protected $isMarkedForDeletion = false;
/**
* @param array|null $initialData
*/
public function __construct($initialData = null) {
//This just makes the constructor public.
parent::__construct();
if ( $initialData !== null ) {
$this->data = $initialData;
$this->doneLoading = true;
}
}
/**
* @inheritDoc
*/
public function setValue($value) {
$this->data = $value;
$this->doneLoading = true;
$this->isMarkedForDeletion = false;
return true;
}
public function deleteValue() {
$this->data = null;
$this->doneLoading = true;
$this->isMarkedForDeletion = true;
}
/**
* @inheritDoc
*/
public function setPath($path, $value) {
$this->lazyLoad();
$this->isMarkedForDeletion = false;
$path = $this->parsePath($path);
//Data could be NULL if it was previously deleted.
if ( $this->data === null ) {
$this->data = [];
}
return ameMultiDictionary::set($this->data, $path, $value);
}
public function deletePath($path) {
$this->lazyLoad();
$path = $this->parsePath($path);
//An empty path is invalid. This method can't delete the entire storage.
if ( empty($path) ) {
throw new \InvalidArgumentException('Path cannot be empty');
}
ameMultiDictionary::delete($this->data, $path);
}
/**
* @inheritDoc
*/
public function buildSlot($path) {
return new Slot($this, $path);
}
/**
* @inheritDoc
*/
protected function rawGetPath($path, $defaultValue = null) {
$this->lazyLoad();
return ameMultiDictionary::get($this->data, $path, $defaultValue);
}
protected function rawGetValue($defaultValue = null) {
$this->lazyLoad();
return ($this->data !== null) ? $this->data : $defaultValue;
}
protected function lazyLoad() {
if ( $this->doneLoading ) {
return;
}
$this->doneLoading = true;
$this->isMarkedForDeletion = false;
$this->data = $this->loadData();
}
public function save() {
if ( $this->isMarkedForDeletion && ($this->data === null) ) {
$this->deleteStoredData();
} else {
$this->lazyLoad();
$this->storeData($this->data);
}
}
/**
* Save data to the database or other persistent storage.
*
* @param array $newData
* @return void
*/
protected function storeData($newData) {
//Override in subclasses.
}
/**
* Load data from persistent storage.
*
* @return array|null
*/
protected function loadData() {
//Override in subclasses.
return [];
}
/**
* Delete data in persistent storage.
*
* @return void
*/
protected function deleteStoredData() {
//Override in subclasses.
}
}