1 | <?php |
||
18 | abstract class BaseManager |
||
19 | { |
||
20 | /** |
||
21 | * @var string |
||
22 | */ |
||
23 | protected static $cFileName; |
||
24 | |||
25 | /** |
||
26 | * @param $cFileName |
||
27 | */ |
||
28 | public static function setCFileName($cFileName) |
||
32 | |||
33 | /** |
||
34 | * @return string |
||
35 | */ |
||
36 | public static function getCFileName() |
||
37 | { |
||
38 | return static::$cFileName; |
||
39 | } |
||
40 | |||
41 | /** |
||
42 | * @return string |
||
43 | * |
||
44 | * Return absolute file path which store cache list |
||
45 | */ |
||
46 | public static function getCFilePath() |
||
50 | |||
51 | /** |
||
52 | * Unique string for each server |
||
53 | * @param string $salt |
||
54 | * @return string |
||
55 | */ |
||
56 | public static function getUniqueString($salt = '') |
||
|
|||
57 | { |
||
58 | $uniqueString = isset($_SERVER['SERVER_SIGNATURE']) ? $_SERVER['SERVER_SIGNATURE'] . __DIR__ : __DIR__; |
||
59 | return md5($uniqueString . $salt); |
||
60 | } |
||
61 | |||
62 | /** |
||
63 | * @param $key |
||
64 | * @return bool|string |
||
65 | * |
||
66 | * Return absolute file path of caching object |
||
67 | */ |
||
68 | public static function get($key) |
||
69 | { |
||
70 | $cacheList = static::getCacheList(); |
||
71 | if (isset($cacheList[$key])) { |
||
72 | if (($cacheList[$key][0] == 0 || $cacheList[$key][0] >= time())) { |
||
73 | return $cacheList[$key][1]; |
||
74 | } else { |
||
75 | unlink($cacheList[$key][1]); |
||
76 | static::remove($key); |
||
77 | } |
||
78 | } |
||
79 | |||
80 | return false; |
||
81 | } |
||
82 | |||
83 | /** |
||
84 | * @param $key |
||
85 | * @param $filePath |
||
86 | * @return bool |
||
87 | * |
||
88 | * Save $key and $filePath in cacheList |
||
89 | */ |
||
90 | public static function set($key, $filePath, $ttl) |
||
91 | { |
||
92 | $cacheList = static::getCacheList(); |
||
93 | |||
94 | if ($ttl != 0) { |
||
95 | $ttl += time(); |
||
96 | } |
||
97 | $cacheList[$key] = [$ttl, $filePath]; |
||
98 | |||
99 | return static::setCacheList($cacheList); |
||
100 | } |
||
101 | |||
102 | /** |
||
103 | * @param $key |
||
104 | * @return bool |
||
105 | * |
||
106 | * Remove caching object file and remove from cache list |
||
107 | */ |
||
108 | public static function remove($key) |
||
109 | { |
||
110 | $cacheList = static::getCacheList(); |
||
111 | unset($cacheList[$key]); |
||
112 | |||
113 | return static::setCacheList($cacheList); |
||
114 | } |
||
115 | |||
116 | /** |
||
117 | * @param $key |
||
118 | * @return bool |
||
119 | */ |
||
120 | public static function has($key) |
||
121 | { |
||
122 | $cacheList = static::getCacheList(); |
||
123 | if (is_array($cacheList)) { |
||
124 | return array_key_exists($key, $cacheList); |
||
125 | } |
||
126 | |||
127 | return false; |
||
128 | } |
||
129 | |||
130 | /** |
||
131 | * @return array |
||
132 | * |
||
133 | * Return cache list in array |
||
134 | */ |
||
135 | protected static function getCacheList() |
||
143 | |||
144 | /** |
||
145 | * @param $cacheList |
||
146 | * @return bool |
||
147 | */ |
||
148 | protected static function setCacheList($cacheList) |
||
152 | |||
153 | /** |
||
154 | * @param $array |
||
155 | * @return string |
||
156 | */ |
||
157 | protected static function encode($array) |
||
161 | |||
162 | /** |
||
163 | * @param $string |
||
164 | * @return array |
||
165 | */ |
||
166 | protected static function decode($string) |
||
170 | } |
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: