1
|
|
|
<?php namespace CMPayments\Cache; |
2
|
|
|
|
3
|
|
|
use CMPayments\Cache\Exceptions\CacheException; |
4
|
|
|
use CMPayments\Json\Json; |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* Class Cache |
8
|
|
|
* |
9
|
|
|
* @package CMPayments\Cache |
10
|
|
|
* @Author Boy Wijnmaalen <[email protected]> |
11
|
|
|
*/ |
12
|
|
|
class Cache |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var array |
16
|
|
|
*/ |
17
|
|
|
private $options = []; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var array |
21
|
|
|
*/ |
22
|
|
|
private $passthru = []; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Cache constructor. |
26
|
|
|
* |
27
|
|
|
* @param array $options |
28
|
|
|
* @param array $passthru |
29
|
|
|
*/ |
30
|
|
|
public function __construct(array $options = [], &$passthru = []) |
31
|
|
|
{ |
32
|
|
|
$defaultOptions = [ |
33
|
|
|
'directory' => __DIR__ . '/../../../storage/cache/', |
34
|
|
|
'debug' => false |
35
|
|
|
]; |
36
|
|
|
|
37
|
|
|
$this->options = array_merge($defaultOptions, $options); |
38
|
|
|
|
39
|
|
|
$this->passthru = & $passthru; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @return array |
44
|
|
|
*/ |
45
|
|
|
public function getOptions() |
46
|
|
|
{ |
47
|
|
|
return $this->options; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Get the cache directory |
52
|
|
|
* |
53
|
|
|
* @return string |
54
|
|
|
*/ |
55
|
|
|
public function getDirectory() |
56
|
|
|
{ |
57
|
|
|
return $this->options['directory']; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Get the cache filename |
62
|
|
|
* |
63
|
|
|
* @return string|null |
64
|
|
|
*/ |
65
|
|
|
public function getFilename() |
66
|
|
|
{ |
67
|
|
|
return (isset($this->options['filename'])) ? $this->options['filename'] : null; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Set the cache filename |
72
|
|
|
* |
73
|
|
|
* @param string $filename |
74
|
|
|
*/ |
75
|
|
|
public function setFilename($filename) |
76
|
|
|
{ |
77
|
|
|
$this->options['filename'] = $filename; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Get the debug option (boolean) |
82
|
|
|
* |
83
|
|
|
* @return boolean |
84
|
|
|
*/ |
85
|
|
|
public function getDebug() |
86
|
|
|
{ |
87
|
|
|
return $this->options['debug']; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Returns the full path of the filename |
92
|
|
|
* |
93
|
|
|
* @return string |
94
|
|
|
* @throws CacheException |
95
|
|
|
*/ |
96
|
|
|
public function getAbsoluteFilePath() |
97
|
|
|
{ |
98
|
|
|
// check is the cache filename option is set |
99
|
|
|
if (!isset($this->options['filename'])) { |
100
|
|
|
|
101
|
|
|
throw new CacheException(CacheException::ERROR_CACHE_FILENAME_NOT_SET, ['$options[\'filename\']']); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
return $this->options['directory'] . $this->options['filename']; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Store Cache content |
109
|
|
|
* |
110
|
|
|
* @param $data |
111
|
|
|
* @param null|string $filename |
112
|
|
|
* |
113
|
|
|
* @throws CacheException |
114
|
|
|
*/ |
115
|
|
|
public function putContent($data, $filename = null) |
116
|
|
|
{ |
117
|
|
|
$options = $this->getOptions(); |
118
|
|
|
|
119
|
|
|
// check if the directory is writable or not and throw exception when $this->config['debug'] is true |
120
|
|
|
if (!is_writable($options['directory'])) { |
121
|
|
|
|
122
|
|
|
$this->passthru[Json::WARNINGS][] = convert_exception_to_array(new CacheException(CacheException::ERROR_CACHE_DIRECTORY_NOT_WRITABLE, $options['directory'])); |
123
|
|
|
} else { |
124
|
|
|
|
125
|
|
|
file_put_contents(((is_null($filename)) ? $this->getAbsoluteFilePath() : $filename), $this->generateRunnableCache($data)); |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Get Cache content |
131
|
|
|
* |
132
|
|
|
* @param $location |
133
|
|
|
* |
134
|
|
|
* @return mixed|null |
135
|
|
|
*/ |
136
|
|
|
public function getContent($location) |
137
|
|
|
{ |
138
|
|
|
if (file_exists($location)) { |
139
|
|
|
|
140
|
|
|
return require $location; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
return null; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Create runnable cache for $variable |
148
|
|
|
* |
149
|
|
|
* @param $variable |
150
|
|
|
* @param bool|false $recursion |
151
|
|
|
* |
152
|
|
|
* @return string |
153
|
|
|
* |
154
|
|
|
* @author Bas Peters <[email protected]> |
155
|
|
|
*/ |
156
|
|
|
private function generateRunnableCache($variable, $recursion = false) |
157
|
|
|
{ |
158
|
|
|
if ($variable instanceof \stdClass) { |
159
|
|
|
|
160
|
|
|
// workaround for a PHP bug where var_export cannot deal with stdClass |
161
|
|
|
$result = '(object) ' . $this->generateRunnableCache(get_object_vars($variable), true); |
162
|
|
|
} else { |
163
|
|
|
|
164
|
|
|
if (is_array($variable)) { |
165
|
|
|
$array = []; |
166
|
|
|
|
167
|
|
|
foreach ($variable as $key => $value) { |
168
|
|
|
|
169
|
|
|
$array[] = var_export($key, true) . ' => ' . $this->generateRunnableCache($value, true); |
170
|
|
|
} |
171
|
|
|
$result = 'array (' . implode(', ', $array) . ')'; |
172
|
|
|
} else { |
173
|
|
|
|
174
|
|
|
$result = var_export($variable, true); |
175
|
|
|
} |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
return $recursion ? $result : sprintf('<?php return %s;', $result); |
179
|
|
|
} |
180
|
|
|
} |
181
|
|
|
|