-
Notifications
You must be signed in to change notification settings - Fork 7
/
HumanReadableCache.php
129 lines (120 loc) · 3.37 KB
/
HumanReadableCache.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
<?php
namespace Luracast\Restler;
/**
* Default Cache that writes/reads human readable files for caching purpose
*
* @category Framework
* @package Restler
* @author R.Arul Kumaran <arul@luracast.com>
* @copyright 2010 Luracast
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @link http://luracast.com/products/restler/
*
*/
class HumanReadableCache implements iCache
{
/**
* @var string path of the folder to hold cache files
*/
public static $cacheDir;
public function __construct()
{
if (is_null(self::$cacheDir)) {
self::$cacheDir = Defaults::$cacheDirectory;
}
}
/**
* store data in the cache
*
* @param string $name
* @param mixed $data
*
* @throws \Exception
* @return boolean true if successful
*/
public function set($name, $data)
{
if (is_array($data)) {
$s = '$o = array();' . PHP_EOL . PHP_EOL;
$s .= '// ** THIS IS AN AUTO GENERATED FILE.'
. ' DO NOT EDIT MANUALLY ** ';
foreach ($data as $key => $value) {
$s .= PHP_EOL . PHP_EOL .
"//==================== $key ===================="
. PHP_EOL . PHP_EOL;
if (is_array($value)) {
$s .= '$o[\'' . $key . '\'] = array();';
foreach ($value as $ke => $va) {
$s .= PHP_EOL . PHP_EOL . "//==== $key $ke ===="
. PHP_EOL . PHP_EOL;
$s .= '$o[\'' . $key . '\'][\'' . $ke . '\'] = ' .
str_replace(' ', ' ',
var_export($va, true)) . ';';
}
} else {
$s .= '$o[\'' . $key . '\'] = '
. var_export($value, true) . ';';
}
}
$s .= PHP_EOL . 'return $o;';
} else {
$s = 'return ' . var_export($data, true) . ';';
}
$file = $this->_file($name);
$r = @file_put_contents($file, "<?php $s");
@chmod($file, 0777);
if ($r === false) {
$this->throwException();
}
return $r;
}
/**
* retrieve data from the cache
*
* @param string $name
* @param bool $ignoreErrors
*
* @return mixed
*/
public function get($name, $ignoreErrors = false)
{
$file = $this->_file($name);
if (file_exists($file)) {
return include($file);
}
}
/**
* delete data from the cache
*
* @param string $name
* @param bool $ignoreErrors
*
* @return boolean true if successful
*/
public function clear($name, $ignoreErrors = false)
{
return @unlink($this->_file($name));
}
/**
* check if the given name is cached
*
* @param string $name
*
* @return boolean true if cached
*/
public function isCached($name)
{
return file_exists($this->_file($name));
}
private function _file($name)
{
return self::$cacheDir . '/' . $name . '.php';
}
private function throwException()
{
throw new \Exception(
'The cache directory `'
. self::$cacheDir . '` should exist with write permission.'
);
}
}