1 | <?php |
||
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) |
|
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) |
|
68 | |||
69 | /** |
||
70 | * Remove time for key. |
||
71 | * |
||
72 | * @param string $key |
||
73 | * |
||
74 | * @return bool |
||
75 | */ |
||
76 | 2 | public function remove($key) |
|
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() |
|
144 | } |
||
145 |