1 | <?php |
||
7 | class Cache |
||
8 | { |
||
9 | private $converter; |
||
10 | |||
11 | 44 | public function __construct(Filesystem $fs, Converter $converter) |
|
12 | { |
||
13 | 44 | $this->converter = $converter; |
|
14 | 44 | $this->path = $fs->getPath('.cache'); |
|
|
|||
15 | 44 | if (!is_dir($this->path)) { |
|
16 | 4 | mkdir($this->path); |
|
17 | 4 | @chown($this->path, 'www-data'); |
|
18 | 4 | @chgrp($this->path, 'www-data'); |
|
19 | } |
||
20 | 44 | } |
|
21 | |||
22 | private $cache = []; |
||
23 | |||
24 | 44 | public function exists($key) |
|
25 | { |
||
26 | 44 | if (array_key_exists($key, $this->cache)) { |
|
27 | 4 | return $this->cache[$key]['expire'] > Carbon::now()->getTimestamp(); |
|
28 | } |
||
29 | 44 | $filename = $this->path . '/' . $key; |
|
30 | 44 | if (file_exists($filename)) { |
|
31 | 2 | if (!array_key_exists($key, $this->cache)) { |
|
32 | 2 | $this->cache[$key] = include $filename; |
|
33 | } |
||
34 | 2 | return $this->cache[$key]['expire'] > Carbon::now()->getTimestamp(); |
|
35 | } |
||
36 | 44 | } |
|
37 | |||
38 | 4 | public function get($key) |
|
44 | |||
45 | 3 | public function set($key, $value) |
|
57 | |||
58 | 44 | public function wrap($key, $callback) |
|
78 | } |
||
79 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: