1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* osCommerce Online Merchant |
4
|
|
|
* |
5
|
|
|
* @copyright Copyright (c) 2016 osCommerce; http://www.oscommerce.com |
6
|
|
|
* @license GPL; http://www.oscommerce.com/gpllicense.txt |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace OSC\OM; |
10
|
|
|
|
11
|
|
|
use OSC\OM\FileSystem; |
12
|
|
|
use OSC\OM\OSCOM; |
13
|
|
|
|
14
|
|
|
class Cache |
15
|
|
|
{ |
16
|
|
|
protected static $path; |
17
|
|
|
|
18
|
|
|
protected $key; |
19
|
|
|
protected $data; |
20
|
|
|
|
21
|
|
|
public function __construct($key) |
22
|
|
|
{ |
23
|
|
|
$this->setPath(); |
24
|
|
|
|
25
|
|
|
$this->setKey($key); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
public function setKey($key) |
29
|
|
|
{ |
30
|
|
|
if (!$this->hasSafeName($key)) { |
31
|
|
|
trigger_error('OSC\\OM\\Cache: Invalid key name (\'' . $key . '\'). Valid characters are a-zA-Z0-9-_'); |
32
|
|
|
|
33
|
|
|
return false; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
$this->key = $key; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function getKey() |
40
|
|
|
{ |
41
|
|
|
return $this->key; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function save($data) |
45
|
|
|
{ |
46
|
|
|
if (FileSystem::isWritable(static::$path)) { |
47
|
|
|
return file_put_contents(static::$path . $this->key . '.cache', serialize($data), LOCK_EX) !== false; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
return false; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function exists($expire = null) |
54
|
|
|
{ |
55
|
|
|
$filename = static::$path . $this->key . '.cache'; |
56
|
|
|
|
57
|
|
|
if (is_file($filename)) { |
58
|
|
|
if (!isset($expire)) { |
59
|
|
|
return true; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
$difference = floor((time() - filemtime($filename)) / 60); |
63
|
|
|
|
64
|
|
|
if (is_numeric($expire) && ($difference < $expire)) { |
65
|
|
|
return true; |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
return false; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function get() |
73
|
|
|
{ |
74
|
|
|
$filename = static::$path . $this->key . '.cache'; |
75
|
|
|
|
76
|
|
|
if (is_file($filename)) { |
77
|
|
|
$this->data = unserialize(file_get_contents($filename)); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
return $this->data; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
public static function hasSafeName($key) |
84
|
|
|
{ |
85
|
|
|
return preg_match('/^[a-zA-Z0-9-_]+$/', $key) === 1; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
public function getTime() |
89
|
|
|
{ |
90
|
|
|
$filename = static::$path . $this->key . '.cache'; |
91
|
|
|
|
92
|
|
|
if (is_file($filename)) { |
93
|
|
|
return filemtime($filename); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
return false; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
public static function find($key, $strict = true) |
100
|
|
|
{ |
101
|
|
|
if (!static::hasSafeName($key)) { |
102
|
|
|
trigger_error('OSC\\OM\\Cache::find(): Invalid key name (\'' . $key . '\'). Valid characters are a-zA-Z0-9-_'); |
103
|
|
|
|
104
|
|
|
return false; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
if (is_file(static::$path . $key . '.cache')) { |
108
|
|
|
return true; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
if ($strict === false) { |
112
|
|
|
$key_length = strlen($key); |
113
|
|
|
|
114
|
|
|
$d = dir(static::$path); |
115
|
|
|
|
116
|
|
|
while (($entry = $d->read()) !== false) { |
117
|
|
|
if ((strlen($entry) >= $key_length) && (substr($entry, 0, $key_length) == $key)) { |
118
|
|
|
$d->close(); |
119
|
|
|
|
120
|
|
|
return true; |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
return false; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
public static function setPath() |
129
|
|
|
{ |
130
|
|
|
static::$path = OSCOM::BASE_DIR . 'Work/Cache/'; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
public static function getPath() |
134
|
|
|
{ |
135
|
|
|
if (!isset(static::$path)) { |
136
|
|
|
static::setPath(); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
return static::$path; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
public static function clear($key) |
143
|
|
|
{ |
144
|
|
|
if (!static::hasSafeName($key)) { |
145
|
|
|
trigger_error('OSC\\OM\\Cache::clear(): Invalid key name (\'' . $key . '\'). Valid characters are a-zA-Z0-9-_'); |
146
|
|
|
|
147
|
|
|
return false; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
View Code Duplication |
if (FileSystem::isWritable(static::$path)) { |
|
|
|
|
151
|
|
|
foreach (glob(static::$path . $key . '*.cache') as $c) { |
152
|
|
|
unlink($c); |
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
public static function clearAll() |
158
|
|
|
{ |
159
|
|
View Code Duplication |
if (FileSystem::isWritable(static::$path)) { |
|
|
|
|
160
|
|
|
foreach (glob(static::$path . '*.cache') as $c) { |
161
|
|
|
unlink($c); |
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
} |
166
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.