1 | <?php |
||
8 | trait FileCacheTrait |
||
9 | { |
||
10 | |||
11 | /** |
||
12 | * @param string $name |
||
13 | * @return mixed |
||
14 | */ |
||
15 | 5 | protected function includeFile(string $name) |
|
16 | { |
||
17 | 5 | $filename = $this->getCacheFileName($name); |
|
18 | 5 | if (!is_file($filename)) { |
|
19 | return null; |
||
20 | } |
||
21 | |||
22 | 5 | return include $filename; |
|
23 | } |
||
24 | |||
25 | /** |
||
26 | * @param string $name |
||
27 | * @param string $content |
||
28 | */ |
||
29 | 2 | protected function dumpCacheFile(string $name, string $content) |
|
30 | { |
||
31 | 2 | $fileName = $this->getCacheFileName($name); |
|
32 | |||
33 | 2 | file_put_contents($fileName, '<?php' . PHP_EOL . $content); |
|
34 | 2 | @chmod($fileName, 0777); |
|
|
|||
35 | 2 | } |
|
36 | |||
37 | /** |
||
38 | * @param string $name |
||
39 | * @param mixed $variable |
||
40 | */ |
||
41 | 2 | protected function dumpVariableToCache(string $name, $variable) |
|
42 | { |
||
43 | 2 | $this->dumpCacheFile($name, 'return ' . var_export($variable, true) . ';'); |
|
44 | 2 | } |
|
45 | |||
46 | /** |
||
47 | * @param string $name |
||
48 | * @return string |
||
49 | */ |
||
50 | 5 | private function getCacheFileName($name) |
|
54 | } |
||
55 |
If you suppress an error, we recommend checking for the error condition explicitly: