|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* persistence implementation class |
|
4
|
|
|
*/ |
|
5
|
|
|
namespace Phile\Plugin\Phile\SimpleFileDataPersistence\Persistence; |
|
6
|
|
|
|
|
7
|
|
|
use Phile\ServiceLocator\PersistenceInterface; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Class SimpleFileDataPersistence |
|
11
|
|
|
* |
|
12
|
|
|
* @author Frank Nägler |
|
13
|
|
|
* @link https://philecms.github.io |
|
14
|
|
|
* @license http://opensource.org/licenses/MIT |
|
15
|
|
|
* @package Phile\Plugin\Phile\SimpleFileDataPersistence\Persistence |
|
16
|
|
|
*/ |
|
17
|
|
|
class SimpleFileDataPersistence implements PersistenceInterface |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* @var string $dataDirectory the data storage directory |
|
21
|
|
|
*/ |
|
22
|
|
|
protected $dataDirectory; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* the constructor |
|
26
|
|
|
* |
|
27
|
|
|
* @param string $dataDir Directory to store data |
|
28
|
|
|
*/ |
|
29
|
34 |
|
public function __construct(string $dataDir) |
|
30
|
|
|
{ |
|
31
|
34 |
|
$this->dataDirectory = $dataDir; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* check if key exists |
|
36
|
|
|
* |
|
37
|
|
|
* @param string $key |
|
38
|
|
|
* |
|
39
|
|
|
* @return bool |
|
40
|
|
|
*/ |
|
41
|
|
|
public function has($key) |
|
42
|
|
|
{ |
|
43
|
|
|
return (file_exists($this->getStorageFile($key))); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* get value for given key |
|
48
|
|
|
* |
|
49
|
|
|
* @param string $key |
|
50
|
|
|
* |
|
51
|
|
|
* @return mixed |
|
52
|
|
|
* @throws \Phile\Exception\AbstractException |
|
53
|
|
|
*/ |
|
54
|
|
|
public function get($key) |
|
55
|
|
|
{ |
|
56
|
|
|
if (!$this->has($key)) { |
|
57
|
|
|
throw new \Phile\Exception\AbstractException("no data storage for key '{$key}' exists!"); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
return unserialize(file_get_contents($this->getStorageFile($key))); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* set value for given key |
|
65
|
|
|
* |
|
66
|
|
|
* @param string $key |
|
67
|
|
|
* @param mixed $value |
|
68
|
|
|
* |
|
69
|
|
|
* @return void |
|
70
|
|
|
*/ |
|
71
|
|
|
public function set($key, $value) |
|
72
|
|
|
{ |
|
73
|
|
|
file_put_contents($this->getStorageFile($key), serialize($value)); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* delte given key/index |
|
78
|
|
|
* |
|
79
|
|
|
* @param string $key |
|
80
|
|
|
* @param array $options |
|
81
|
|
|
* |
|
82
|
|
|
* @return void |
|
83
|
|
|
* @throws \Phile\Exception\AbstractException |
|
84
|
|
|
*/ |
|
85
|
|
|
public function delete($key, array $options = array()) |
|
86
|
|
|
{ |
|
87
|
|
|
if (!$this->has($key)) { |
|
88
|
|
|
throw new \Phile\Exception\AbstractException("no data storage for key '{$key}' exists!"); |
|
89
|
|
|
} |
|
90
|
|
|
unlink($this->getStorageFile($key)); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* generate internal key |
|
95
|
|
|
* |
|
96
|
|
|
* @param string $key |
|
97
|
|
|
* |
|
98
|
|
|
* @return string |
|
99
|
|
|
*/ |
|
100
|
|
|
protected function getInternalKey($key) |
|
101
|
|
|
{ |
|
102
|
|
|
return md5($key); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* get storage filename |
|
107
|
|
|
* |
|
108
|
|
|
* @param string $key |
|
109
|
|
|
* |
|
110
|
|
|
* @return string |
|
111
|
|
|
*/ |
|
112
|
|
|
protected function getStorageFile($key) |
|
113
|
|
|
{ |
|
114
|
|
|
return $this->dataDirectory . $this->getInternalKey($key) . '.ds'; |
|
115
|
|
|
} |
|
116
|
|
|
} |
|
117
|
|
|
|