1
|
|
|
<?php |
2
|
|
|
namespace NwLaravel\Repositories\Eloquent; |
3
|
|
|
|
4
|
|
|
use Illuminate\Contracts\Cache\Repository as CacheRepository; |
5
|
|
|
use Prettus\Repository\Contracts\CriteriaInterface; |
6
|
|
|
use ReflectionObject; |
7
|
|
|
use Exception; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class CacheableRepository |
11
|
|
|
*/ |
12
|
|
|
trait CacheableRepository |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var CacheRepository |
16
|
|
|
*/ |
17
|
|
|
protected $cacheRepository = null; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Set Cache Repository |
21
|
|
|
* |
22
|
|
|
* @param CacheRepository $repository |
23
|
|
|
* |
24
|
|
|
* @return $this |
25
|
|
|
*/ |
26
|
|
|
public function setCacheRepository(CacheRepository $repository) |
27
|
|
|
{ |
28
|
|
|
$this->cacheRepository = $repository; |
29
|
|
|
|
30
|
|
|
return $this; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Return instance of Cache Repository |
35
|
|
|
* |
36
|
|
|
* @return CacheRepository |
37
|
|
|
*/ |
38
|
1 |
|
public function getCacheRepository() |
39
|
|
|
{ |
40
|
1 |
|
if (is_null($this->cacheRepository)) { |
41
|
1 |
|
$this->cacheRepository = app(config('repository.cache.repository', 'cache')); |
42
|
1 |
|
} |
43
|
|
|
|
44
|
1 |
|
return $this->cacheRepository; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* CacheTags |
49
|
|
|
* |
50
|
|
|
* @param mixed $tags |
51
|
|
|
* |
52
|
|
|
* @return \Illuminate\Cache\TaggedCache |
53
|
|
|
*/ |
54
|
1 |
|
public function cacheTags($tags = null) |
55
|
|
|
{ |
56
|
1 |
|
$tags = empty($tags) ? get_called_class() : $tags; |
57
|
|
|
|
58
|
1 |
|
return $this->getCacheRepository()->tags($tags); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Skip Cache |
63
|
|
|
* |
64
|
|
|
* @param bool $status |
65
|
|
|
* |
66
|
|
|
* @return $this |
67
|
|
|
*/ |
68
|
|
|
public function skipCache($status = true) |
69
|
|
|
{ |
70
|
|
|
$this->cacheSkip = $status; |
|
|
|
|
71
|
|
|
|
72
|
|
|
return $this; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Is Skipped Cache |
77
|
|
|
* |
78
|
|
|
* @return bool |
79
|
|
|
*/ |
80
|
1 |
|
public function isSkippedCache() |
81
|
|
|
{ |
82
|
1 |
|
$skipped = isset($this->cacheSkip) ? $this->cacheSkip : false; |
83
|
1 |
|
$request = app('Illuminate\Http\Request'); |
84
|
1 |
|
$skipCacheParam = config('repository.cache.params.skipCache', 'skipCache'); |
85
|
|
|
|
86
|
1 |
|
if ($request->has($skipCacheParam) && $request->get($skipCacheParam)) { |
87
|
|
|
$skipped = true; |
88
|
|
|
} |
89
|
|
|
|
90
|
1 |
|
return $skipped; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Allowed Cache |
95
|
|
|
* |
96
|
|
|
* @param string $method |
97
|
|
|
* |
98
|
|
|
* @return bool |
99
|
|
|
*/ |
100
|
1 |
|
protected function allowedCache($method) |
101
|
|
|
{ |
102
|
1 |
|
$cacheEnabled = config('repository.cache.enabled', true); |
103
|
|
|
|
104
|
1 |
|
if (!$cacheEnabled) { |
105
|
|
|
return false; |
106
|
|
|
} |
107
|
|
|
|
108
|
1 |
|
$cacheOnly = isset($this->cacheOnly) ? $this->cacheOnly : config('repository.cache.allowed.only', null); |
|
|
|
|
109
|
1 |
|
$cacheExcept = isset($this->cacheExcept) ? $this->cacheExcept : config('repository.cache.allowed.except', null); |
|
|
|
|
110
|
|
|
|
111
|
1 |
|
if (is_array($cacheOnly)) { |
112
|
|
|
return in_array($method, $cacheOnly); |
113
|
|
|
} |
114
|
|
|
|
115
|
1 |
|
if (is_array($cacheExcept)) { |
116
|
|
|
return !in_array($method, $cacheExcept); |
117
|
|
|
} |
118
|
|
|
|
119
|
1 |
|
if (is_null($cacheOnly) && is_null($cacheExcept)) { |
120
|
1 |
|
return true; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
return false; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Get Cache key for the method |
128
|
|
|
* |
129
|
|
|
* @param string $method |
130
|
|
|
* @param array $args |
131
|
|
|
* |
132
|
|
|
* @return string |
133
|
|
|
*/ |
134
|
1 |
|
public function getCacheKey($method, $args = null) |
135
|
|
|
{ |
136
|
|
|
|
137
|
1 |
|
$request = app('Illuminate\Http\Request'); |
138
|
1 |
|
$args = serialize($args); |
139
|
1 |
|
$criteria = $this->serializeCriteria(); |
140
|
|
|
|
141
|
1 |
|
return sprintf('%s@%s-%s', get_called_class(), $method, md5($args . $criteria . $request->fullUrl())); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Serialize the criteria making sure the Closures are taken care of. |
146
|
|
|
* |
147
|
|
|
* @return string |
148
|
|
|
*/ |
149
|
1 |
|
protected function serializeCriteria() |
150
|
|
|
{ |
151
|
|
|
try { |
152
|
1 |
|
return serialize($this->getCriteria()); |
|
|
|
|
153
|
|
|
} catch (Exception $e) { |
154
|
|
|
return serialize($this->getCriteria()->map(function ($criterion) { |
|
|
|
|
155
|
|
|
return $this->serializeCriterion($criterion); |
156
|
|
|
})); |
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Serialize single criterion with customized serialization of Closures. |
162
|
|
|
* |
163
|
|
|
* @param \Prettus\Repository\Contracts\CriteriaInterface $criterion |
164
|
|
|
* |
165
|
|
|
* @return \Prettus\Repository\Contracts\CriteriaInterface|array |
166
|
|
|
* |
167
|
|
|
* @throws \Exception |
168
|
|
|
*/ |
169
|
|
|
protected function serializeCriterion($criterion) |
170
|
|
|
{ |
171
|
|
|
try { |
172
|
|
|
serialize($criterion); |
173
|
|
|
|
174
|
|
|
return $criterion; |
175
|
|
|
} catch (Exception $e) { |
176
|
|
|
// We want to take care of the closure serialization errors, |
177
|
|
|
// other than that we will simply re-throw the exception. |
178
|
|
|
if ($e->getMessage() !== "Serialization of 'Closure' is not allowed") { |
179
|
|
|
throw $e; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
$reflection = new ReflectionObject($criterion); |
183
|
|
|
|
184
|
|
|
return [ |
185
|
|
|
'hash' => md5((string) $reflection), |
186
|
|
|
'properties' => $reflection->getProperties(), |
187
|
|
|
]; |
188
|
|
|
} |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* Get cache minutes |
193
|
|
|
* |
194
|
|
|
* @return int |
195
|
|
|
*/ |
196
|
1 |
|
public function getCacheMinutes() |
197
|
|
|
{ |
198
|
1 |
|
$cacheMinutes = isset($this->cacheMinutes) ? $this->cacheMinutes : config('repository.cache.minutes', 30); |
|
|
|
|
199
|
|
|
|
200
|
1 |
|
return $cacheMinutes; |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* call Cache |
205
|
|
|
* |
206
|
|
|
* @param string $method |
207
|
|
|
* @param array $args |
208
|
|
|
* |
209
|
|
|
* @return mixed |
210
|
|
|
*/ |
211
|
1 |
|
public function callCache($method, array $args) |
212
|
|
|
{ |
213
|
1 |
|
if (!$this->allowedCache($method) || $this->isSkippedCache()) { |
214
|
|
|
return call_user_func_array(['parent', $method], $args); |
215
|
|
|
} |
216
|
|
|
|
217
|
1 |
|
$key = $this->getCacheKey($method, $args); |
218
|
1 |
|
$minutes = $this->getCacheMinutes(); |
219
|
1 |
|
$value = $this->cacheTags()->remember($key, $minutes, function () use ($method, $args) { |
220
|
|
|
return call_user_func_array(['parent', $method], $args); |
221
|
1 |
|
}); |
222
|
|
|
|
223
|
1 |
|
return $value; |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
/** |
227
|
|
|
* Retrieve all data of repository |
228
|
|
|
* |
229
|
|
|
* @param array $columns |
230
|
|
|
* |
231
|
|
|
* @return mixed |
232
|
|
|
*/ |
233
|
1 |
|
public function all($columns = ['*']) |
|
|
|
|
234
|
|
|
{ |
235
|
1 |
|
return $this->callCache(__FUNCTION__, func_get_args()); |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
/** |
239
|
|
|
* Retrieve all data of repository, paginated |
240
|
|
|
* |
241
|
|
|
* @param null $limit |
242
|
|
|
* @param array $columns |
243
|
|
|
* |
244
|
|
|
* @return mixed |
245
|
|
|
*/ |
246
|
1 |
|
public function paginate($limit = null, $columns = ['*']) |
|
|
|
|
247
|
|
|
{ |
248
|
1 |
|
return $this->callCache(__FUNCTION__, func_get_args()); |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
/** |
252
|
|
|
* Find data by id |
253
|
|
|
* |
254
|
|
|
* @param int $id |
255
|
|
|
* @param array $columns |
256
|
|
|
* |
257
|
|
|
* @return mixed |
258
|
|
|
*/ |
259
|
1 |
|
public function find($id, $columns = ['*']) |
|
|
|
|
260
|
|
|
{ |
261
|
1 |
|
return $this->callCache(__FUNCTION__, func_get_args()); |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
/** |
265
|
|
|
* Find data by field and value |
266
|
|
|
* |
267
|
|
|
* @param string $field |
268
|
|
|
* @param mixed $value |
269
|
|
|
* @param array $columns |
270
|
|
|
* |
271
|
|
|
* @return mixed |
272
|
|
|
*/ |
273
|
1 |
|
public function findByField($field, $value = null, $columns = ['*']) |
|
|
|
|
274
|
|
|
{ |
275
|
1 |
|
return $this->callCache(__FUNCTION__, func_get_args()); |
276
|
|
|
} |
277
|
|
|
|
278
|
|
|
/** |
279
|
|
|
* Find data by multiple fields |
280
|
|
|
* |
281
|
|
|
* @param array $where |
282
|
|
|
* @param array $columns |
283
|
|
|
* |
284
|
|
|
* @return mixed |
285
|
|
|
*/ |
286
|
1 |
|
public function findWhere(array $where, $columns = ['*']) |
|
|
|
|
287
|
|
|
{ |
288
|
1 |
|
return $this->callCache(__FUNCTION__, func_get_args()); |
289
|
|
|
} |
290
|
|
|
|
291
|
|
|
/** |
292
|
|
|
* Find data by Criteria |
293
|
|
|
* |
294
|
|
|
* @param CriteriaInterface $criteria |
295
|
|
|
* |
296
|
|
|
* @return mixed |
297
|
|
|
*/ |
298
|
1 |
|
public function getByCriteria(CriteriaInterface $criteria) |
|
|
|
|
299
|
|
|
{ |
300
|
1 |
|
return $this->callCache(__FUNCTION__, func_get_args()); |
301
|
|
|
} |
302
|
|
|
|
303
|
|
|
/** |
304
|
|
|
* Pluck |
305
|
|
|
* |
306
|
|
|
* @param string $column |
307
|
|
|
* @param array $key |
308
|
|
|
* |
309
|
|
|
* @return array |
310
|
|
|
*/ |
311
|
1 |
|
public function pluck($column, $key = null) |
|
|
|
|
312
|
|
|
{ |
313
|
1 |
|
return $this->callCache(__FUNCTION__, func_get_args()); |
314
|
|
|
} |
315
|
|
|
|
316
|
|
|
/** |
317
|
|
|
* Count |
318
|
|
|
* |
319
|
|
|
* @param array $input Array Input |
320
|
|
|
* |
321
|
|
|
* @return int |
322
|
|
|
*/ |
323
|
1 |
|
public function count(array $input = array()) |
|
|
|
|
324
|
|
|
{ |
325
|
1 |
|
return $this->callCache(__FUNCTION__, func_get_args()); |
326
|
|
|
} |
327
|
|
|
|
328
|
|
|
/** |
329
|
|
|
* Max |
330
|
|
|
* |
331
|
|
|
* @param mixed $field Mixed Field |
332
|
|
|
* @param array $input Array Input |
333
|
|
|
* |
334
|
|
|
* @return mixed |
335
|
|
|
*/ |
336
|
1 |
|
public function max($field, array $input = array()) |
|
|
|
|
337
|
|
|
{ |
338
|
1 |
|
return $this->callCache(__FUNCTION__, func_get_args()); |
339
|
|
|
} |
340
|
|
|
|
341
|
|
|
/** |
342
|
|
|
* Min |
343
|
|
|
* |
344
|
|
|
* @param mixed $field Mixed Field |
345
|
|
|
* @param array $input Array Input |
346
|
|
|
* |
347
|
|
|
* @return mixed |
348
|
|
|
*/ |
349
|
1 |
|
public function min($field, array $input = array()) |
|
|
|
|
350
|
|
|
{ |
351
|
1 |
|
return $this->callCache(__FUNCTION__, func_get_args()); |
352
|
|
|
} |
353
|
|
|
|
354
|
|
|
/** |
355
|
|
|
* Sum |
356
|
|
|
* |
357
|
|
|
* @param mixed $field Mixed Field |
358
|
|
|
* @param array $input Array Input |
359
|
|
|
* |
360
|
|
|
* @return float |
361
|
|
|
*/ |
362
|
1 |
|
public function sum($field, array $input = array()) |
|
|
|
|
363
|
|
|
{ |
364
|
1 |
|
return $this->callCache(__FUNCTION__, func_get_args()); |
365
|
|
|
} |
366
|
|
|
|
367
|
|
|
/** |
368
|
|
|
* Average |
369
|
|
|
* |
370
|
|
|
* @param mixed $field Mixed Field |
371
|
|
|
* @param array $input Array Input |
372
|
|
|
* |
373
|
|
|
* @return float |
374
|
|
|
*/ |
375
|
1 |
|
public function avg($field, array $input = array()) |
|
|
|
|
376
|
|
|
{ |
377
|
1 |
|
return $this->callCache(__FUNCTION__, func_get_args()); |
378
|
|
|
} |
379
|
|
|
} |
380
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: