|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace PragmaRX\Tracker\Support; |
|
4
|
|
|
|
|
5
|
|
|
use Cache as IlluminateCache; |
|
6
|
|
|
use Illuminate\Database\Eloquent\Model; |
|
7
|
|
|
use Illuminate\Foundation\Application; |
|
8
|
|
|
use PragmaRX\Support\Config as Config; |
|
9
|
|
|
|
|
10
|
|
|
class Cache |
|
11
|
|
|
{ |
|
12
|
|
|
private $app; |
|
13
|
|
|
|
|
14
|
|
|
private $config; |
|
15
|
|
|
|
|
16
|
|
|
public function __construct(Config $config, Application $app) |
|
17
|
|
|
{ |
|
18
|
|
|
$this->config = $config; |
|
19
|
|
|
|
|
20
|
|
|
$this->app = $app; |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
public function cachePut($cacheKey, $model) |
|
24
|
|
|
{ |
|
25
|
|
|
if ($this->config->get('cache_enabled')) { |
|
26
|
|
|
IlluminateCache::put($cacheKey, $model, 10); |
|
27
|
|
|
} |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
private function extractAttributes($attributes) |
|
31
|
|
|
{ |
|
32
|
|
|
if (is_array($attributes) || is_string($attributes)) { |
|
33
|
|
|
return $attributes; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
if (is_string($attributes) || is_numeric($attributes)) { |
|
37
|
|
|
return (array) $attributes; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
if ($attributes instanceof Model) { |
|
41
|
|
|
return $attributes->getAttributes(); |
|
42
|
|
|
} |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @param $attributes |
|
47
|
|
|
* @param $keys |
|
48
|
|
|
* |
|
49
|
|
|
* @return array |
|
50
|
|
|
*/ |
|
51
|
|
|
private function extractKeys($attributes, $keys) |
|
52
|
|
|
{ |
|
53
|
|
|
if (!$keys) { |
|
54
|
|
|
$keys = array_keys($attributes); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
if (!is_array($keys)) { |
|
58
|
|
|
$keys = (array) $keys; |
|
59
|
|
|
|
|
60
|
|
|
return $keys; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
return $keys; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @param string $key |
|
68
|
|
|
* |
|
69
|
|
|
* @return array |
|
70
|
|
|
*/ |
|
71
|
|
|
public function findCachedWithKey($key) |
|
72
|
|
|
{ |
|
73
|
|
|
if ($this->config->get('cache_enabled')) { |
|
74
|
|
|
return IlluminateCache::get($key); |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
public function makeKeyAndPut($model, $key) |
|
79
|
|
|
{ |
|
80
|
|
|
$key = $this->makeCacheKey($model, $key, get_class($model)); |
|
81
|
|
|
|
|
82
|
|
|
$this->cachePut($key, $model); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* @param string $identifier |
|
87
|
|
|
*/ |
|
88
|
|
|
public function findCached($attributes, $keys, $identifier = null) |
|
89
|
|
|
{ |
|
90
|
|
|
if (!$this->config->get('cache_enabled')) { |
|
91
|
|
|
return; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
$key = $this->makeCacheKey($attributes, $keys, $identifier); |
|
95
|
|
|
|
|
96
|
|
|
return [ |
|
97
|
|
|
$this->findCachedWithKey($key), |
|
98
|
|
|
$key, |
|
99
|
|
|
]; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
public function makeCacheKey($attributes, $keys, $identifier) |
|
103
|
|
|
{ |
|
104
|
|
|
$attributes = $this->extractAttributes($attributes); |
|
105
|
|
|
|
|
106
|
|
|
$cacheKey = "className=$identifier;"; |
|
107
|
|
|
|
|
108
|
|
|
$keys = $this->extractKeys($attributes, $keys, $identifier); |
|
|
|
|
|
|
109
|
|
|
|
|
110
|
|
|
foreach ($keys as $key) { |
|
111
|
|
|
if (isset($attributes[$key])) { |
|
112
|
|
|
$cacheKey .= "$key=$attributes[$key];"; |
|
113
|
|
|
} |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
return sha1($cacheKey); |
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
|
|
|
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.
In this case you can add the
@ignorePhpDoc annotation to the duplicate definition and it will be ignored.