1 | <?php |
||
22 | class Cache |
||
23 | { |
||
24 | const CACHE_DIR = 'pH7_cache/'; |
||
25 | const CACHE_FILE_EXT = '.cache.php'; |
||
26 | |||
27 | /** @var File */ |
||
28 | private $_oFile; |
||
29 | |||
30 | /** @var string */ |
||
31 | private $_sCacheDir; |
||
32 | |||
33 | /** @var string */ |
||
34 | private $_sGroup; |
||
35 | |||
36 | /** @var string */ |
||
37 | private $_sId; |
||
38 | |||
39 | /** @var integer */ |
||
40 | private $_iTtl; |
||
41 | |||
42 | /** @var string */ |
||
43 | private $_sPrefix = 'pH7_'; |
||
44 | |||
45 | /** @var boolean */ |
||
46 | private $_bEnabled = true; |
||
47 | |||
48 | public function __construct() |
||
53 | |||
54 | /** |
||
55 | * Enabled/Disabled the cache. |
||
56 | * |
||
57 | * @param boolean $bIsEnable |
||
58 | * |
||
59 | * @return self |
||
60 | */ |
||
61 | public function enabled($bIsEnable) |
||
67 | |||
68 | /** |
||
69 | * Sets cache directory. |
||
70 | * If the directory is not correct, the method will cause an exception. |
||
71 | * If you do not use this method, a default directory will be created. |
||
72 | * |
||
73 | * @param string $sCacheDir |
||
74 | * |
||
75 | * @return self |
||
76 | * |
||
77 | * @throws PH7InvalidArgumentException An explanatory message if the directory does not exist. |
||
78 | */ |
||
79 | public function setCacheDir($sCacheDir) |
||
89 | |||
90 | /** |
||
91 | * Sets the cache prefix. |
||
92 | * |
||
93 | * @param string $sPrefix |
||
94 | * |
||
95 | * @return self |
||
96 | */ |
||
97 | public function setPrefix($sPrefix) |
||
102 | |||
103 | /** |
||
104 | * Sets the time expire cache. |
||
105 | * |
||
106 | * @param integer $iExpire (the time with the 'touch' function). |
||
107 | * |
||
108 | * @return self |
||
109 | */ |
||
110 | public function setExpire($iExpire) |
||
117 | |||
118 | /** |
||
119 | * Start the cache. |
||
120 | * |
||
121 | * @param string $sGroup The Group Cache (This creates a folder). |
||
122 | * @param string $sId (The ID for the file). |
||
123 | * @param integer $iTtl Cache lifetime in seconds. If NULL, the file never expires. |
||
124 | * |
||
125 | * @return self |
||
126 | */ |
||
127 | public function start($sGroup, $sId, $iTtl) |
||
141 | |||
142 | /** |
||
143 | * Stop the cache. |
||
144 | * |
||
145 | * @param boolean $bPrint TRUE = Display data with ECHO. FALSE = Return data. Default TRUE. |
||
146 | * |
||
147 | * @return string|null |
||
148 | */ |
||
149 | public function stop($bPrint = true) |
||
166 | |||
167 | /** |
||
168 | * Gets the data cache. |
||
169 | * |
||
170 | * @param boolean $bPrint Default FALSE |
||
171 | * @return boolean|integer|float|string|array|object Returns the converted cache value if successful, FALSE otherwise. |
||
172 | */ |
||
173 | public function get($bPrint = false) |
||
183 | |||
184 | /** |
||
185 | * Puts the data in the cache. |
||
186 | * |
||
187 | * @param string $sData |
||
188 | * |
||
189 | * @return string|null|self If the cache is disabled, returns null, otherwise returns this class. |
||
190 | */ |
||
191 | public function put($sData) |
||
201 | |||
202 | /** |
||
203 | * Clear the cache. |
||
204 | * |
||
205 | * @return self this |
||
206 | */ |
||
207 | public function clear() |
||
216 | |||
217 | |||
218 | /** |
||
219 | * Get the creation/modification time of the current cache file. |
||
220 | * |
||
221 | * @return integer|boolean Time the file was last modified/created as a Unix timestamp, or FALSE on failure. |
||
222 | */ |
||
223 | public function getTimeOfCacheFile() |
||
227 | |||
228 | /** |
||
229 | * Get the header content to put in the file. |
||
230 | * |
||
231 | * @return string |
||
232 | */ |
||
233 | final protected function getHeaderContents() |
||
252 | |||
253 | /** |
||
254 | * Writes data in a cache file. |
||
255 | * |
||
256 | * @param string $sData |
||
257 | * |
||
258 | * @return boolean |
||
259 | * |
||
260 | * @throws Exception If the file cannot be written. |
||
261 | */ |
||
262 | final private function _write($sData) |
||
290 | |||
291 | /** |
||
292 | * Reads the Cache. |
||
293 | * |
||
294 | * @param boolean $bPrint |
||
295 | * @return boolean|string Returns TRUE or a string if successful, FALSE otherwise. |
||
296 | */ |
||
297 | private function _read($bPrint) |
||
314 | |||
315 | /** |
||
316 | * Gets the file cache. |
||
317 | * |
||
318 | * @return string |
||
319 | */ |
||
320 | private function _getFile() |
||
324 | |||
325 | /** |
||
326 | * Checks the cache. |
||
327 | * |
||
328 | * @return boolean |
||
329 | */ |
||
330 | private function _check() |
||
342 | |||
343 | /** |
||
344 | * Checks if the cache directory has been defined otherwise we create a default directory. |
||
345 | * If the folder cache does not exist, it creates a folder. |
||
346 | * |
||
347 | * @return self |
||
348 | */ |
||
349 | private function _checkCacheDir() |
||
355 | } |
||
356 |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.