1
|
|
|
<?php |
2
|
|
|
namespace Metaphore\Store; |
3
|
|
|
|
4
|
|
|
use Metaphore\Store\ValueStoreInterface; |
5
|
|
|
use Metaphore\Store\LockStoreInterface; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Think again if you really truly want to use filesystem for caching any data ;-) |
9
|
|
|
*/ |
10
|
|
|
class FilePhpStore implements ValueStoreInterface, LockStoreInterface |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var string |
14
|
|
|
*/ |
15
|
|
|
protected $directory; |
16
|
|
|
|
17
|
|
|
protected static $strDenyAccess = '<?php return header("HTTP/1.0 404 Not Found"); ?>'; //deny access from web-browser |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @param string |
21
|
|
|
*/ |
22
|
|
|
public function __construct($directory = null) |
23
|
|
|
{ |
24
|
|
|
if (!$directory) { |
25
|
|
|
$directory = sys_get_temp_dir(); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
$this->directory = $directory; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* {@inheritdoc} |
33
|
|
|
*/ |
34
|
|
View Code Duplication |
public function set($key, $value, $ttl) |
|
|
|
|
35
|
|
|
{ |
36
|
|
|
$fileName = $this->getFileName($key); |
37
|
|
|
$data = self::$strDenyAccess . '|' . $ttl . '|' . serialize($value); |
38
|
|
|
|
39
|
|
|
return file_put_contents($fileName, $data); |
|
|
|
|
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* {@inheritdoc} |
44
|
|
|
*/ |
45
|
|
View Code Duplication |
public function get($key) |
|
|
|
|
46
|
|
|
{ |
47
|
|
|
$fileName = $this->getFileName($key); |
48
|
|
|
|
49
|
|
|
if (!file_exists($fileName)) { |
50
|
|
|
return false; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
$data = file_get_contents($fileName); |
54
|
|
|
list($strDenyAccess, $ttl, $serializedValue) = explode('|', $data, 3); |
|
|
|
|
55
|
|
|
|
56
|
|
|
if ($ttl > time()) { |
57
|
|
|
return false; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
return unserialize($serializedValue); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* {@inheritdoc} |
65
|
|
|
*/ |
66
|
|
View Code Duplication |
public function add($key, $value, $ttl) |
|
|
|
|
67
|
|
|
{ |
68
|
|
|
$fileName = $this->getFileName($key); |
69
|
|
|
|
70
|
|
|
if (file_exists($fileName)) { |
71
|
|
|
return false; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
return $this->set($key, $value, $ttl); |
|
|
|
|
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* {@inheritdoc} |
79
|
|
|
*/ |
80
|
|
|
public function delete($key) |
81
|
|
|
{ |
82
|
|
|
$fileName = $this->getFileName($key); |
83
|
|
|
|
84
|
|
|
if (file_exists($fileName)) { |
85
|
|
|
unlink($fileName); |
86
|
|
|
} |
87
|
|
|
return true; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
protected function getFileName($key) |
91
|
|
|
{ |
92
|
|
|
return $this->directory . DIRECTORY_SEPARATOR . $key . '.php'; |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.