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