|
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
|
|
|
|
|
10
|
|
|
namespace AnimeDb\Bundle\CacheTimeKeeperBundle\Service; |
|
11
|
|
|
|
|
12
|
|
|
use AnimeDb\Bundle\CacheTimeKeeperBundle\Exception\NotModifiedException; |
|
13
|
|
|
use AnimeDb\Bundle\CacheTimeKeeperBundle\Service\Driver\DriverInterface; |
|
14
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
15
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
16
|
|
|
|
|
17
|
|
|
class Keeper |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* Key for last update of the project. |
|
21
|
|
|
* |
|
22
|
|
|
* @var string |
|
23
|
|
|
*/ |
|
24
|
|
|
const LAST_UPDATE_KEY = 'last-update'; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var string |
|
28
|
|
|
*/ |
|
29
|
|
|
const IDENTIFIER_SEPARATOR = CacheKeyBuilder::IDENTIFIER_SEPARATOR; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var string |
|
33
|
|
|
*/ |
|
34
|
|
|
const IDENTIFIER_PREFIX = CacheKeyBuilder::IDENTIFIER_PREFIX; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @var DriverInterface |
|
38
|
|
|
*/ |
|
39
|
|
|
protected $driver; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @var ResponseConfigurator |
|
43
|
|
|
*/ |
|
44
|
|
|
protected $configurator; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @var bool |
|
48
|
|
|
*/ |
|
49
|
|
|
protected $enable = true; |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @param DriverInterface $driver |
|
53
|
|
|
* @param ResponseConfigurator $configurator |
|
54
|
|
|
* @param bool $enable |
|
55
|
|
|
*/ |
|
56
|
22 |
|
public function __construct(DriverInterface $driver, ResponseConfigurator $configurator, $enable) |
|
57
|
|
|
{ |
|
58
|
22 |
|
$this->driver = $driver; |
|
59
|
22 |
|
$this->configurator = $configurator; |
|
60
|
22 |
|
$this->enable = $enable; |
|
61
|
22 |
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Get time for key. |
|
65
|
|
|
* |
|
66
|
|
|
* @param string $key |
|
67
|
|
|
* |
|
68
|
|
|
* @return \DateTime |
|
69
|
|
|
*/ |
|
70
|
3 |
|
public function get($key) |
|
71
|
|
|
{ |
|
72
|
3 |
|
if (!($time = $this->driver->get($key))) { |
|
73
|
2 |
|
if ($key == self::LAST_UPDATE_KEY) { |
|
74
|
1 |
|
$time = $this->reset(); |
|
75
|
1 |
|
} else { |
|
76
|
1 |
|
$time = $this->get(self::LAST_UPDATE_KEY); |
|
77
|
|
|
} |
|
78
|
2 |
|
} |
|
79
|
|
|
|
|
80
|
3 |
|
return $time; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Set time for key. |
|
85
|
|
|
* |
|
86
|
|
|
* @param string $key |
|
87
|
|
|
* @param \DateTime $time |
|
88
|
|
|
* |
|
89
|
|
|
* @return bool |
|
90
|
|
|
*/ |
|
91
|
1 |
|
public function set($key, \DateTime $time) |
|
92
|
|
|
{ |
|
93
|
1 |
|
return $this->driver->set($key, $time); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* Remove time for key. |
|
98
|
|
|
* |
|
99
|
|
|
* @param string $key |
|
100
|
|
|
* |
|
101
|
|
|
* @return bool |
|
102
|
|
|
*/ |
|
103
|
2 |
|
public function remove($key) |
|
104
|
|
|
{ |
|
105
|
2 |
|
return $this->driver->remove($key); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* Get a list of keys or dates and chooses the max date. |
|
110
|
|
|
* |
|
111
|
|
|
* @param mixed $params |
|
112
|
|
|
* |
|
113
|
|
|
* @return \DateTime |
|
114
|
|
|
*/ |
|
115
|
14 |
|
public function getMax($params = []) |
|
116
|
|
|
{ |
|
117
|
14 |
|
if (!$this->enable) { |
|
118
|
1 |
|
return new \DateTime(); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
13 |
|
$params = (array) $params; |
|
122
|
|
|
// always check the date of the last update of the project |
|
123
|
13 |
|
if (!in_array(self::LAST_UPDATE_KEY, $params)) { |
|
124
|
11 |
|
$params[] = self::LAST_UPDATE_KEY; |
|
125
|
11 |
|
} |
|
126
|
|
|
|
|
127
|
13 |
|
if (!($time = $this->driver->getMax($params))) { |
|
128
|
4 |
|
$time = $this->reset(); |
|
129
|
4 |
|
} |
|
130
|
|
|
|
|
131
|
13 |
|
return $time; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* Get cache response. |
|
136
|
|
|
* |
|
137
|
|
|
* Set $lifetime as < 0 for not set max-age |
|
138
|
|
|
* |
|
139
|
|
|
* @param mixed $params |
|
140
|
|
|
* @param int $lifetime |
|
141
|
|
|
* @param Response|null $response |
|
142
|
|
|
* |
|
143
|
|
|
* @return Response |
|
144
|
|
|
*/ |
|
145
|
7 |
|
public function getResponse($params = [], $lifetime = -1, Response $response = null) |
|
146
|
|
|
{ |
|
147
|
7 |
|
if (!$response) { |
|
148
|
4 |
|
$response = new Response(); |
|
149
|
4 |
|
} |
|
150
|
|
|
|
|
151
|
7 |
|
if (!$this->enable) { |
|
152
|
2 |
|
return $response; |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
5 |
|
return $this->configurator->configure($response, $this->getMax($params), $lifetime); |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
/** |
|
159
|
|
|
* Get only modified response. |
|
160
|
|
|
* |
|
161
|
|
|
* Throw exception if response was not modified for this request |
|
162
|
|
|
* |
|
163
|
|
|
* Set $lifetime as < 0 for not set max-age |
|
164
|
|
|
* |
|
165
|
|
|
* @throws NotModifiedException |
|
166
|
|
|
* |
|
167
|
|
|
* @param Request $request |
|
168
|
|
|
* @param mixed $params |
|
169
|
|
|
* @param int $lifetime |
|
170
|
|
|
* @param Response|null $response |
|
171
|
|
|
* |
|
172
|
|
|
* @return Response |
|
173
|
|
|
*/ |
|
174
|
3 |
|
public function getModifiedResponse(Request $request, $params = [], $lifetime = -1, Response $response = null) |
|
175
|
|
|
{ |
|
176
|
3 |
|
$response = $this->getResponse($params, $lifetime, $response); |
|
177
|
|
|
|
|
178
|
3 |
|
if ($response->isNotModified($request)) { |
|
179
|
1 |
|
throw new NotModifiedException($response); |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
2 |
|
return $response; |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
/** |
|
186
|
|
|
* Reset last update date. |
|
187
|
|
|
* |
|
188
|
|
|
* @return \DateTime |
|
189
|
|
|
*/ |
|
190
|
5 |
|
private function reset() |
|
191
|
|
|
{ |
|
192
|
5 |
|
$time = new \DateTime(); |
|
193
|
5 |
|
$this->driver->set(self::LAST_UPDATE_KEY, $time); |
|
194
|
|
|
|
|
195
|
5 |
|
return $time; |
|
196
|
|
|
} |
|
197
|
|
|
} |
|
198
|
|
|
|