1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
4
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
5
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
6
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
7
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
8
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
9
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
10
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
11
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
12
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
13
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
14
|
|
|
* |
15
|
|
|
* This software consists of voluntary contributions made by many individuals |
16
|
|
|
* and is licensed under the LGPL. For more information please see |
17
|
|
|
* <http://phing.info>. |
18
|
|
|
*/ |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Class PropertiesfileCache |
22
|
|
|
*/ |
23
|
|
|
class PropertiesfileCache implements Cache |
24
|
|
|
{ |
25
|
|
|
/** Where to store the properties? */ |
26
|
|
|
private $cachefile; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Object for storing the key-value-pairs. |
30
|
|
|
* @var Properties |
31
|
|
|
*/ |
32
|
|
|
private $cache; |
33
|
|
|
|
34
|
|
|
/** Is the cache already loaded? Prevents from multiple load operations. */ |
35
|
|
|
private $cacheLoaded = false; |
36
|
|
|
|
37
|
|
|
/** Must the cache be saved? Prevents from multiple save operations. */ |
38
|
|
|
private $cacheDirty = true; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Constructor. |
42
|
|
|
* @param PhingFile $cachefile set the cachefile |
43
|
|
|
*/ |
44
|
4 |
|
public function __construct(PhingFile $cachefile = null) |
45
|
|
|
{ |
46
|
4 |
|
$this->cache = new Properties(); |
47
|
4 |
|
$this->cachefile = $cachefile; |
48
|
4 |
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Getter. |
52
|
|
|
* @return PhingFile|null the cachefile |
53
|
|
|
*/ |
54
|
|
|
public function getCachefile(): ?PhingFile |
55
|
|
|
{ |
56
|
|
|
return $this->cachefile; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Setter. |
61
|
|
|
* @param PhingFile $file new value |
62
|
|
|
*/ |
63
|
2 |
|
public function setCachefile(PhingFile $file): void |
64
|
|
|
{ |
65
|
2 |
|
$this->cachefile = $file; |
66
|
2 |
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* This cache is valid if the cachefile is set. |
70
|
|
|
* @return true if all is ok false otherwise |
71
|
|
|
*/ |
72
|
2 |
|
public function isValid(): bool |
73
|
|
|
{ |
74
|
2 |
|
return $this->cachefile !== null; |
|
|
|
|
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Saves modification of the cache. |
79
|
|
|
* Cache is only saved if there is one ore more entries. |
80
|
|
|
* Because entries can not be deleted by this API, this Cache |
81
|
|
|
* implementation checks the existence of entries before creating the file |
82
|
|
|
* for performance optimisation. |
83
|
|
|
*/ |
84
|
1 |
|
public function save(): void |
85
|
|
|
{ |
86
|
1 |
|
if (!$this->cacheDirty) { |
87
|
|
|
return; |
88
|
|
|
} |
89
|
1 |
|
if ($this->cachefile !== null && count($this->cache->propertyNames()) > 0) { |
90
|
1 |
|
$this->cache->store($this->cachefile); |
91
|
|
|
} |
92
|
1 |
|
$this->cacheDirty = false; |
93
|
1 |
|
} |
94
|
|
|
|
95
|
|
|
/** Deletes the cache and its underlying file. */ |
96
|
|
|
public function delete(): void |
97
|
|
|
{ |
98
|
|
|
$this->cache = new Properties(); |
99
|
|
|
$this->cachefile->delete(); |
|
|
|
|
100
|
|
|
$this->cacheLoaded = true; |
101
|
|
|
$this->cacheDirty = false; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Returns a value for a given key from the cache. |
106
|
|
|
* @param string $key the key |
107
|
|
|
* @return mixed the stored value |
108
|
|
|
*/ |
109
|
2 |
|
public function get($key) |
110
|
|
|
{ |
111
|
2 |
|
if (!$this->cacheLoaded) { |
112
|
|
|
$this->load(); |
113
|
|
|
} |
114
|
|
|
|
115
|
2 |
|
return $this->cache->getProperty((string) $key); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Load the cache from underlying properties file. |
120
|
|
|
*/ |
121
|
2 |
|
public function load(): void |
122
|
|
|
{ |
123
|
2 |
|
if ($this->cachefile !== null && $this->cachefile->isFile() && $this->cachefile->canRead()) { |
124
|
|
|
try { |
125
|
1 |
|
$this->cache->load($this->cachefile); |
126
|
|
|
} catch (Exception $e) { |
127
|
|
|
echo $e->getTraceAsString(); |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
// after loading the cache is up to date with the file |
131
|
2 |
|
$this->cacheLoaded = true; |
132
|
2 |
|
$this->cacheDirty = false; |
133
|
2 |
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Saves a key-value-pair in the cache. |
137
|
|
|
* @param string $key the key |
138
|
|
|
* @param string $value the value |
139
|
|
|
*/ |
140
|
1 |
|
public function put($key, $value): void |
141
|
|
|
{ |
142
|
1 |
|
$this->cache->put((string) $key, (string) $value); |
143
|
1 |
|
$this->cacheDirty = true; |
144
|
1 |
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Returns an iterator over the keys in the cache. |
148
|
|
|
* @return Iterator An iterator over the keys. |
149
|
|
|
*/ |
150
|
|
|
public function getIterator(): Iterator |
151
|
|
|
{ |
152
|
|
|
return new ArrayIterator($this->cache->propertyNames()); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* @return string information about this cache |
157
|
|
|
*/ |
158
|
2 |
|
public function __toString(): string |
159
|
|
|
{ |
160
|
2 |
|
if (!$this->cacheLoaded) { |
161
|
2 |
|
$this->load(); |
162
|
|
|
} |
163
|
2 |
|
return sprintf( |
164
|
2 |
|
'<%s:cachefile=%s;noOfEntries=%d>', |
165
|
2 |
|
__CLASS__, |
166
|
2 |
|
$this->cachefile, |
167
|
2 |
|
count($this->cache->keys()) |
168
|
|
|
); |
169
|
|
|
} |
170
|
|
|
} |
171
|
|
|
|