1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Simple class that helps caching data |
5
|
|
|
* created mostly for illustrative purposes |
6
|
|
|
* |
7
|
|
|
* Usage: |
8
|
|
|
* $cache = new CustomCache(360, 'some_remote_data', function(){ |
9
|
|
|
* return file_get_contents('http://imagecms.net/some_controller/get_some_xml'); |
10
|
|
|
* }); |
11
|
|
|
* |
12
|
|
|
* $myXml = $cache->getData(); // you don't know if data was from cache, or from remote source |
13
|
|
|
* |
14
|
|
|
* @author kolia |
15
|
|
|
*/ |
16
|
|
|
class CustomCache |
17
|
|
|
{ |
18
|
|
|
|
19
|
|
|
const CACHE_PATH = 'system/cache/'; |
20
|
|
|
|
21
|
|
|
protected static $cacheFilenames = []; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Interval in seconds |
25
|
|
|
* @var int |
26
|
|
|
*/ |
27
|
|
|
protected $interval; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* |
31
|
|
|
* @var string |
32
|
|
|
*/ |
33
|
|
|
protected $cacheFilepath; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Function that returns the actual data |
37
|
|
|
* @var Closure |
38
|
|
|
*/ |
39
|
|
|
protected $dataSourceCallback; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* |
43
|
|
|
* @param int $interval update interval in seconds |
44
|
|
|
* @param string $cacheFilename file name that will be stored cache |
45
|
|
|
* @param Closure $dataSourceCallback function that returns actual data |
46
|
|
|
* @throws \Exception |
47
|
|
|
*/ |
48
|
|
|
public function __construct($interval = 0, $cacheFilename, Closure $dataSourceCallback) { |
49
|
|
|
if (is_numeric($interval)) { |
50
|
|
|
$this->interval = $interval; |
|
|
|
|
51
|
|
|
} elseif (ENVIRONMENT == 'development') { |
52
|
|
|
throw new \Exception(lang('Interval argument must be integer', 'main')); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
$fileError = ''; |
56
|
|
|
if (!preg_match('/^[a-zA-Z0-9\_\-]{1,240}$/', $cacheFilename)) { |
57
|
|
|
$fileError = lang('Cache file name is not valid string. ', 'main'); |
58
|
|
|
} |
59
|
|
|
if (in_array($cacheFilename, self::$cacheFilenames)) { |
60
|
|
|
$fileError = lang('Cache file with such name already exist. Please chose another name', 'main'); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
if ($fileError === '') { |
64
|
|
|
$this->cacheFilepath = PUBPATH . self::CACHE_PATH . 'custom_cache_' . $cacheFilename . '.txt'; |
65
|
|
|
self::$cacheFilenames[] = $cacheFilename; |
66
|
|
|
} elseif (ENVIRONMENT == 'development') { |
67
|
|
|
throw new \Exception($fileError); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
$this->dataSourceCallback = $dataSourceCallback; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Gets the data (client don't know it's from cache or not) |
75
|
|
|
* @return string |
76
|
|
|
*/ |
77
|
|
|
public function getData() { |
78
|
|
|
$dataSourceCallback = $this->dataSourceCallback; |
79
|
|
|
// if it's production and configuratin has errors data always will be from source |
80
|
|
|
if ($this->cacheFilepath == null || $this->dataSourceCallback == null) { |
81
|
|
|
return $dataSourceCallback(); |
82
|
|
|
} |
83
|
|
|
// if chached file exists and has actual data it's content will be returned |
84
|
|
|
if (file_exists($this->cacheFilepath) && time() < (filemtime($this->cacheFilepath) + $this->interval)) { |
85
|
|
|
return file_get_contents($this->cacheFilepath); |
86
|
|
|
} |
87
|
|
|
// getting new data, save it, return it =) |
88
|
|
|
$data = $dataSourceCallback(); |
89
|
|
|
file_put_contents($this->cacheFilepath, $data); |
90
|
|
|
return $data; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
} |
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountId
that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theid
property of an instance of theAccount
class. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.