|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Created by PhpStorm. |
|
4
|
|
|
* User: alive |
|
5
|
|
|
* Date: 5/7/18 |
|
6
|
|
|
* Time: 8:10 AM |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace Alive2212\LaravelSmartMeta; |
|
10
|
|
|
|
|
11
|
|
|
use Illuminate\Support\Facades\Cache; |
|
12
|
|
|
|
|
13
|
|
|
trait SmartMeta |
|
14
|
|
|
{ |
|
15
|
|
|
protected $id; |
|
16
|
|
|
|
|
17
|
|
|
protected $cacheMetas; |
|
18
|
|
|
|
|
19
|
|
|
protected $globalKey; |
|
20
|
|
|
|
|
21
|
|
|
protected $expireTime = 5; // 5 minutes |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* |
|
25
|
|
|
* |
|
26
|
|
|
* @return $this |
|
27
|
|
|
*/ |
|
28
|
|
|
public function initCache() |
|
29
|
|
|
{ |
|
30
|
|
|
if(!($this->id>0)){ |
|
31
|
|
|
$this->id = $this->toArray()['id']; |
|
|
|
|
|
|
32
|
|
|
} |
|
33
|
|
|
if (is_null($this->globalKey)){ |
|
34
|
|
|
$this->globalKey = get_class($this) . '_' . $this->id; |
|
35
|
|
|
} |
|
36
|
|
|
$this->getAllCacheMeta(); |
|
37
|
|
|
return $this; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* |
|
42
|
|
|
* |
|
43
|
|
|
* @param $key |
|
44
|
|
|
* @param $value |
|
45
|
|
|
* @return $this |
|
46
|
|
|
*/ |
|
47
|
|
|
public function putCacheMeta($key, $value) |
|
48
|
|
|
{ |
|
49
|
|
|
$this->initCache(); |
|
50
|
|
|
$value = [$key => $value]; |
|
51
|
|
|
Cache::put($this->globalKey, $value, $this->expireTime); |
|
52
|
|
|
return $this; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
// /** |
|
|
|
|
|
|
56
|
|
|
// * @return array |
|
57
|
|
|
// */ |
|
58
|
|
|
// public function getAllCacheMeta(): array |
|
59
|
|
|
// { |
|
60
|
|
|
// return (Cache::get($this->globalKey)); |
|
61
|
|
|
// } |
|
62
|
|
|
|
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @param $key |
|
66
|
|
|
* @param null $default |
|
67
|
|
|
* @param bool $getObj |
|
68
|
|
|
* @return null |
|
69
|
|
|
*/ |
|
70
|
|
|
public function getCacheMeta($key, $default = null, $getObj = false) |
|
|
|
|
|
|
71
|
|
|
{ |
|
72
|
|
|
$this->initCache(); |
|
73
|
|
|
if (isset($this->cacheMetas[$key])) { |
|
74
|
|
|
return $this->cacheMetas[$key]; |
|
75
|
|
|
} |
|
76
|
|
|
return $default; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* @param $key |
|
81
|
|
|
* @return $this |
|
82
|
|
|
*/ |
|
83
|
|
|
public function deleteCacheMeta($key) |
|
84
|
|
|
{ |
|
85
|
|
|
$this->initCache(); |
|
86
|
|
|
unset($this->cacheMetas[$key]); |
|
87
|
|
|
$this->setAllCacheMeta(); |
|
88
|
|
|
return $this; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* |
|
93
|
|
|
*/ |
|
94
|
|
|
public function deleteCacheMetas() |
|
95
|
|
|
{ |
|
96
|
|
|
$this->initCache(); |
|
97
|
|
|
$this->cacheMetas = null; |
|
98
|
|
|
$this->setAllCacheMeta(); |
|
99
|
|
|
return $this; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* @param $key |
|
104
|
|
|
* @param $value |
|
105
|
|
|
*/ |
|
106
|
|
|
public function addCacheMeta($key, $value) |
|
107
|
|
|
{ |
|
108
|
|
|
$this->initCache(); |
|
109
|
|
|
$this->cacheMetas = array_add($this->cacheMetas, $key, $value); |
|
110
|
|
|
$this->setAllCacheMeta(); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* @param $value |
|
115
|
|
|
* @return $this |
|
116
|
|
|
*/ |
|
117
|
|
|
public function pushCacheMeta($value) |
|
118
|
|
|
{ |
|
119
|
|
|
$this->initCache(); |
|
120
|
|
|
array_push($this->cacheMetas, $value); |
|
121
|
|
|
$this->setAllCacheMeta(); |
|
122
|
|
|
return $this; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* @param $key |
|
127
|
|
|
* @param $default |
|
128
|
|
|
* @return $this |
|
129
|
|
|
*/ |
|
130
|
|
|
public function pullCacheMeta($key, $default = null) |
|
131
|
|
|
{ |
|
132
|
|
|
$this->initCache(); |
|
133
|
|
|
array_pull($this->cacheMetas, $key, $default); |
|
134
|
|
|
$this->setAllCacheMeta(); |
|
135
|
|
|
return $this; |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* @return $this |
|
140
|
|
|
*/ |
|
141
|
|
|
public function setAllCacheMeta() |
|
142
|
|
|
{ |
|
143
|
|
|
Cache::put($this->globalKey, $this->cacheMetas, $this->expireTime); |
|
144
|
|
|
return $this; |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* @return $this |
|
149
|
|
|
*/ |
|
150
|
|
|
public function getAllCacheMeta() |
|
151
|
|
|
{ |
|
152
|
|
|
$this->cacheMetas = Cache::get($this->globalKey); |
|
153
|
|
|
return $this; |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* @return mixed |
|
158
|
|
|
*/ |
|
159
|
1 |
|
public function getId() |
|
160
|
|
|
{ |
|
161
|
1 |
|
return $this->id; |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
/** |
|
165
|
|
|
* @param mixed $id |
|
166
|
|
|
*/ |
|
167
|
1 |
|
public function setId($id) |
|
168
|
|
|
{ |
|
169
|
1 |
|
$this->id = $id; |
|
170
|
1 |
|
} |
|
171
|
|
|
|
|
172
|
|
|
/** |
|
173
|
|
|
* @return mixed |
|
174
|
|
|
*/ |
|
175
|
|
|
public function getGlobalKey() |
|
176
|
|
|
{ |
|
177
|
|
|
return $this->globalKey; |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
/** |
|
181
|
|
|
* @param mixed $globalKey |
|
182
|
|
|
*/ |
|
183
|
1 |
|
public function setGlobalKey($globalKey) |
|
184
|
|
|
{ |
|
185
|
1 |
|
$this->globalKey = $globalKey; |
|
186
|
1 |
|
} |
|
187
|
|
|
|
|
188
|
|
|
/** |
|
189
|
|
|
* @return int |
|
190
|
|
|
*/ |
|
191
|
|
|
public function getExpireTime(): int |
|
192
|
|
|
{ |
|
193
|
|
|
return $this->expireTime; |
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
|
|
/** |
|
197
|
|
|
* @param int $expireTime |
|
198
|
|
|
*/ |
|
199
|
|
|
public function setExpireTime(int $expireTime) |
|
200
|
|
|
{ |
|
201
|
|
|
$this->expireTime = $expireTime; |
|
202
|
|
|
} |
|
203
|
|
|
|
|
204
|
|
|
/** |
|
205
|
|
|
* @return mixed |
|
206
|
|
|
*/ |
|
207
|
|
|
public function getCacheMetas() |
|
208
|
|
|
{ |
|
209
|
|
|
return $this->cacheMetas; |
|
210
|
|
|
} |
|
211
|
|
|
|
|
212
|
|
|
/** |
|
213
|
|
|
* @param mixed $cacheMetas |
|
214
|
|
|
*/ |
|
215
|
|
|
public function setCacheMetas($cacheMetas) |
|
216
|
|
|
{ |
|
217
|
|
|
$this->cacheMetas = $cacheMetas; |
|
218
|
|
|
} |
|
219
|
|
|
} |
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idableprovides a methodequalsIdthat in turn relies on the methodgetId(). If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()as an abstract method to the trait will make sure it is available.