|
1
|
|
|
<?php |
|
2
|
|
|
namespace micro\cache; |
|
3
|
|
|
|
|
4
|
|
|
/** |
|
5
|
|
|
* This class is responsible for storing Arrays in PHP files. |
|
6
|
|
|
*/ |
|
7
|
|
|
class ArrayCache extends AbstractDataCache{ |
|
8
|
|
|
/** |
|
9
|
|
|
* |
|
10
|
|
|
* @var int The file mode used when creating new cache files |
|
11
|
|
|
*/ |
|
12
|
|
|
private $_fileMode; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Initializes the file cache-provider |
|
16
|
|
|
* @param string $root absolute path to the root-folder where cache-files will be stored |
|
17
|
|
|
* @param string Termination of file names |
|
18
|
|
|
* @param int $fileMode file creation mode; defaults to 0777 |
|
19
|
|
|
*/ |
|
20
|
|
|
public function __construct($root, $postfix="", $fileMode=0777) { |
|
21
|
|
|
parent::__construct($root,$postfix); |
|
22
|
|
|
$this->_fileMode=$fileMode; |
|
23
|
|
|
if (!is_dir($root)) |
|
24
|
|
|
\mkdir($root, $fileMode, true); |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Check if annotation-data for the key has been stored. |
|
29
|
|
|
* @param string $key cache key |
|
30
|
|
|
* @return bool true if data with the given key has been stored; otherwise false |
|
31
|
|
|
*/ |
|
32
|
|
|
public function exists($key) { |
|
33
|
|
|
return file_exists($this->_getPath($key)); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Caches the given data with the given key. |
|
38
|
|
|
* @param string $key cache key |
|
39
|
|
|
* @param string $code the source-code to be cached |
|
|
|
|
|
|
40
|
|
|
* @throws AnnotationException if file could not be written |
|
41
|
|
|
*/ |
|
42
|
|
|
protected function storeContent($key,$content) { |
|
43
|
|
|
$path=$this->_getPath($key); |
|
44
|
|
|
if (@\file_put_contents($path, $content, LOCK_EX) === false) { |
|
45
|
|
|
throw new \Exception("Unable to write cache file: {$path}"); |
|
46
|
|
|
} |
|
47
|
|
|
if (@\chmod($path, $this->_fileMode) === false) { |
|
48
|
|
|
throw new \Exception("Unable to set permissions of cache file: {$path}"); |
|
49
|
|
|
} |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Fetches data stored for the given key. |
|
54
|
|
|
* @param string $key cache key |
|
55
|
|
|
* @return mixed the cached data |
|
56
|
|
|
*/ |
|
57
|
|
|
public function fetch($key) { |
|
58
|
|
|
return include ($this->_getPath($key)); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* return data stored for the given key. |
|
63
|
|
|
* @param string $key cache key |
|
64
|
|
|
* @return mixed the cached data |
|
65
|
|
|
*/ |
|
66
|
|
|
public function file_get_contents($key) { |
|
67
|
|
|
return \file_get_contents($this->_getPath($key)); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Returns the timestamp of the last cache update for the given key. |
|
72
|
|
|
* |
|
73
|
|
|
* @param string $key cache key |
|
74
|
|
|
* @return int unix timestamp |
|
75
|
|
|
*/ |
|
76
|
|
|
public function getTimestamp($key) { |
|
77
|
|
|
return \filemtime($this->_getPath($key)); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Maps a cache-key to the absolute path of a PHP file |
|
82
|
|
|
* |
|
83
|
|
|
* @param string $key cache key |
|
84
|
|
|
* @return string absolute path of the PHP file |
|
85
|
|
|
*/ |
|
86
|
|
|
private function _getPath($key) { |
|
87
|
|
|
return $this->_root . DIRECTORY_SEPARATOR . $key . $this->postfix . '.php'; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
public function remove($key) { |
|
91
|
|
|
$file=$this->_getPath($key); |
|
92
|
|
|
if (\file_exists($file)) |
|
93
|
|
|
\unlink($file); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
public function clear() { |
|
97
|
|
|
$files=glob($this->_root . '/*'); |
|
98
|
|
|
foreach ( $files as $file ) { |
|
99
|
|
|
if (\is_file($file)) |
|
100
|
|
|
\unlink($file); |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.