|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace PhpAbac\Cache\Pool; |
|
4
|
|
|
|
|
5
|
|
|
use Psr\Cache\CacheItemPoolInterface; |
|
6
|
|
|
use Psr\Cache\CacheItemInterface; |
|
7
|
|
|
|
|
8
|
|
|
use PhpAbac\Cache\Item\TextCacheItem; |
|
9
|
|
|
|
|
10
|
|
|
class TextCacheItemPool implements CacheItemPoolInterface { |
|
11
|
|
|
/** @var array **/ |
|
12
|
|
|
protected $deferredItems; |
|
13
|
|
|
/** @var string **/ |
|
14
|
|
|
protected $cacheFolder; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @param array $options |
|
18
|
|
|
*/ |
|
19
|
10 |
|
public function __construct($options = []) { |
|
20
|
10 |
|
$this->configure($options); |
|
21
|
10 |
|
} |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @param array $options |
|
25
|
|
|
*/ |
|
26
|
10 |
|
protected function configure($options) { |
|
27
|
10 |
|
$this->cacheFolder = |
|
28
|
10 |
|
(isset($options['cache_folder'])) |
|
29
|
10 |
|
? "{$options['cache_folder']}/text" |
|
30
|
|
|
: __DIR__ . '/../../../data/cache/text' |
|
31
|
|
|
; |
|
32
|
10 |
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* {@inheritdoc} |
|
36
|
|
|
*/ |
|
37
|
2 |
|
public function deleteItem($key) { |
|
38
|
2 |
|
if(is_file("{$this->cacheFolder}/$key.txt")) { |
|
|
|
|
|
|
39
|
2 |
|
unlink("{$this->cacheFolder}/$key.txt"); |
|
40
|
|
|
} |
|
41
|
2 |
|
if(isset($this->deferredItems[$key])) { |
|
|
|
|
|
|
42
|
|
|
unset($this->deferredItems[$key]); |
|
43
|
|
|
} |
|
44
|
2 |
|
return true; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* {@inheritdoc} |
|
49
|
|
|
*/ |
|
50
|
1 |
|
public function deleteItems(array $keys) { |
|
51
|
1 |
|
foreach($keys as $key) { |
|
|
|
|
|
|
52
|
1 |
|
if(!$this->deleteItem($key)) { |
|
|
|
|
|
|
53
|
1 |
|
return false; |
|
54
|
|
|
} |
|
55
|
|
|
} |
|
56
|
1 |
|
return true; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* {@inheritdoc} |
|
61
|
|
|
*/ |
|
62
|
9 |
|
public function save(CacheItemInterface $item) { |
|
63
|
9 |
|
$data = "{$item->get()};{$item->getExpirationDate()->format('Y-m-d H:i:s')}"; |
|
64
|
|
|
|
|
65
|
9 |
|
file_put_contents("{$this->cacheFolder}/{$item->getKey()}.txt", $data); |
|
66
|
|
|
|
|
67
|
9 |
|
return true; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* {@inheritdoc} |
|
72
|
|
|
*/ |
|
73
|
2 |
|
public function saveDeferred(CacheItemInterface $item) { |
|
74
|
2 |
|
$this->deferredItems[$item->getKey()] = $item; |
|
75
|
|
|
|
|
76
|
2 |
|
return true; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* {@inheritdoc} |
|
81
|
|
|
*/ |
|
82
|
2 |
|
public function commit() { |
|
83
|
2 |
|
foreach($this->deferredItems as $key => $item) { |
|
|
|
|
|
|
84
|
2 |
|
$this->save($item); |
|
85
|
2 |
|
unset($this->deferredItems[$key]); |
|
86
|
|
|
} |
|
87
|
2 |
|
return true; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* {@inheritdoc} |
|
92
|
|
|
*/ |
|
93
|
10 |
|
public function hasItem($key) { |
|
94
|
10 |
|
return is_file("{$this->cacheFolder}/{$key}.txt"); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* {@inheritdoc} |
|
99
|
|
|
*/ |
|
100
|
6 |
|
public function getItem($key) { |
|
101
|
6 |
|
$item = new TextCacheItem($key); |
|
102
|
6 |
|
if(!$this->hasItem($key)) { |
|
|
|
|
|
|
103
|
1 |
|
return $item; |
|
104
|
|
|
} |
|
105
|
5 |
|
$data = explode(';',file_get_contents("{$this->cacheFolder}/{$key}.txt")); |
|
106
|
|
|
return $item |
|
107
|
5 |
|
->set($data[0]) |
|
108
|
5 |
|
->expiresAt((new \DateTime($data[1]))) |
|
109
|
|
|
; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* {@inheritdoc} |
|
114
|
|
|
*/ |
|
115
|
2 |
|
public function getItems(array $keys = array()) { |
|
116
|
2 |
|
$items = []; |
|
117
|
2 |
|
foreach($keys as $key) { |
|
|
|
|
|
|
118
|
2 |
|
if($this->hasItem($key)) { |
|
|
|
|
|
|
119
|
2 |
|
$items[$key] = $this->getItem($key); |
|
120
|
|
|
} |
|
121
|
|
|
} |
|
122
|
2 |
|
return $items; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* {@inheritdoc} |
|
127
|
|
|
*/ |
|
128
|
10 |
|
public function clear() { |
|
129
|
10 |
|
$items = glob("{$this->cacheFolder}/*.txt"); // get all file names |
|
130
|
10 |
|
foreach($items as $item){ // iterate files |
|
|
|
|
|
|
131
|
8 |
|
if(is_file($item)) |
|
|
|
|
|
|
132
|
8 |
|
unlink($item); // delete file |
|
133
|
|
|
} |
|
134
|
10 |
|
$this->deferredItems = []; |
|
135
|
10 |
|
return true; |
|
136
|
|
|
} |
|
137
|
|
|
} |
|
138
|
|
|
|