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 FileStore implements ValueStoreInterface, LockStoreInterface |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var string |
14
|
|
|
*/ |
15
|
|
|
protected $directory; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @param string |
19
|
|
|
*/ |
20
|
|
|
public function __construct($directory = null) |
21
|
|
|
{ |
22
|
|
|
if (!$directory) { |
23
|
|
|
$directory = sys_get_temp_dir(); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
$this->directory = $directory; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* {@inheritdoc} |
31
|
|
|
*/ |
32
|
|
|
public function set($key, $value, $ttl) |
33
|
|
|
{ |
34
|
|
|
$fileName = $this->getFileName($key); |
35
|
|
|
$data = $this->prepareTtl($ttl).'|'.serialize($value); |
36
|
|
|
|
37
|
|
|
return file_put_contents($fileName, $data); |
|
|
|
|
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* {@inheritdoc} |
42
|
|
|
*/ |
43
|
|
|
public function get($key) |
44
|
|
|
{ |
45
|
|
|
$fileName = $this->getFileName($key); |
46
|
|
|
|
47
|
|
|
if (!file_exists($fileName)) { |
48
|
|
|
return false; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
$data = file_get_contents($fileName); |
52
|
|
|
list($ttl, $serializedValue) = explode('|', $data, 2); |
53
|
|
|
|
54
|
|
|
if ($ttl < time()) { |
55
|
|
|
return false; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
return unserialize($serializedValue); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* {@inheritdoc} |
63
|
|
|
*/ |
64
|
|
|
public function add($key, $value, $ttl) |
65
|
|
|
{ |
66
|
|
|
if ($this->get($key) !== false) { |
67
|
|
|
return false; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
return $this->set($key, $value, $ttl); |
|
|
|
|
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* {@inheritdoc} |
75
|
|
|
*/ |
76
|
|
|
public function delete($key) |
77
|
|
|
{ |
78
|
|
|
$fileName = $this->getFileName($key); |
79
|
|
|
|
80
|
|
|
if (file_exists($fileName)) { |
81
|
|
|
unlink($fileName); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
return true; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
protected function getFileName($key) |
88
|
|
|
{ |
89
|
|
|
return $this->directory.DIRECTORY_SEPARATOR.$key; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
protected function prepareTtl($ttl) |
93
|
|
|
{ |
94
|
|
|
return (time() + $ttl); |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.