1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* AnimeDb package. |
4
|
|
|
* |
5
|
|
|
* @author Peter Gribanov <[email protected]> |
6
|
|
|
* @copyright Copyright (c) 2014, Peter Gribanov |
7
|
|
|
* @license http://opensource.org/licenses/MIT |
8
|
|
|
*/ |
9
|
|
|
namespace AnimeDb\Bundle\CacheTimeKeeperBundle\Service; |
10
|
|
|
|
11
|
|
|
use AnimeDb\Bundle\CacheTimeKeeperBundle\Service\Driver\DriverInterface; |
12
|
|
|
use Symfony\Component\HttpFoundation\Response; |
13
|
|
|
|
14
|
|
|
class Keeper |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* Key for last update of the project. |
18
|
|
|
* |
19
|
|
|
* @var string |
20
|
|
|
*/ |
21
|
|
|
const LAST_UPDATE_KEY = 'last-update'; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var DriverInterface |
25
|
|
|
*/ |
26
|
|
|
protected $driver; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @param DriverInterface $driver |
30
|
|
|
*/ |
31
|
16 |
|
public function __construct(DriverInterface $driver) |
32
|
|
|
{ |
33
|
16 |
|
$this->driver = $driver; |
34
|
16 |
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Get time for key. |
38
|
|
|
* |
39
|
|
|
* @param string $key |
40
|
|
|
* |
41
|
|
|
* @return \DateTime |
42
|
|
|
*/ |
43
|
3 |
|
public function get($key) |
44
|
|
|
{ |
45
|
3 |
|
if (!($time = $this->driver->get($key))) { |
46
|
2 |
|
if ($key == self::LAST_UPDATE_KEY) { |
47
|
1 |
|
$time = $this->reset(); |
48
|
|
|
} else { |
49
|
1 |
|
$time = $this->get(self::LAST_UPDATE_KEY); |
50
|
|
|
} |
51
|
1 |
|
} |
52
|
|
|
|
53
|
2 |
|
return $time; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Set time for key. |
58
|
|
|
* |
59
|
|
|
* @param string $key |
60
|
|
|
* @param \DateTime $time |
61
|
|
|
* |
62
|
|
|
* @return bool |
63
|
|
|
*/ |
64
|
1 |
|
public function set($key, \DateTime $time) |
65
|
|
|
{ |
66
|
1 |
|
return $this->driver->set($key, $time); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Remove time for key. |
71
|
|
|
* |
72
|
|
|
* @param string $key |
73
|
|
|
* |
74
|
|
|
* @return bool |
75
|
|
|
*/ |
76
|
2 |
|
public function remove($key) |
77
|
|
|
{ |
78
|
2 |
|
return $this->driver->remove($key); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Get a list of keys or dates and chooses the max date. |
83
|
|
|
* |
84
|
|
|
* @param mixed $params |
85
|
|
|
* |
86
|
|
|
* @return \DateTime |
87
|
|
|
*/ |
88
|
10 |
|
public function getMax($params = []) |
89
|
|
|
{ |
90
|
10 |
|
$params = (array) $params; |
91
|
|
|
// always check the date of the last update of the project |
92
|
10 |
|
if (!in_array(self::LAST_UPDATE_KEY, $params)) { |
93
|
8 |
|
$params[] = self::LAST_UPDATE_KEY; |
94
|
8 |
|
} |
95
|
|
|
|
96
|
10 |
|
if (!($time = $this->driver->getMax($params))) { |
97
|
4 |
|
$time = $this->reset(); |
98
|
4 |
|
} |
99
|
|
|
|
100
|
10 |
|
return $time; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Get cache response. |
105
|
|
|
* |
106
|
|
|
* Set $lifetime as < 0 for not set max-age |
107
|
|
|
* |
108
|
|
|
* @param mixed $params |
109
|
|
|
* @param int $lifetime |
110
|
|
|
* @param Response|null $response |
111
|
|
|
* |
112
|
|
|
* @return Response |
113
|
|
|
*/ |
114
|
2 |
|
public function getResponse($params = [], $lifetime = -1, Response $response = null) |
115
|
|
|
{ |
116
|
2 |
|
if (!$response) { |
117
|
1 |
|
$response = new Response(); |
118
|
1 |
|
} |
119
|
|
|
|
120
|
2 |
|
if ($lifetime > 0) { |
121
|
|
|
$response |
122
|
1 |
|
->setMaxAge($lifetime) |
123
|
1 |
|
->setSharedMaxAge($lifetime) |
124
|
1 |
|
->setExpires((new \DateTime())->modify('+'.$lifetime.' seconds')); |
125
|
1 |
|
} |
126
|
|
|
|
127
|
|
|
return $response |
128
|
2 |
|
->setPublic() |
129
|
2 |
|
->setLastModified($this->getMax($params)); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Reset last update date. |
134
|
|
|
* |
135
|
|
|
* @return \DateTime |
136
|
|
|
*/ |
137
|
5 |
|
private function reset() |
138
|
|
|
{ |
139
|
5 |
|
$time = new \DateTime(); |
140
|
5 |
|
$this->driver->set(self::LAST_UPDATE_KEY, $time); |
141
|
|
|
|
142
|
4 |
|
return $time; |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
|