This repository has been archived by the owner on Feb 16, 2022. It is now read-only.
/
BaseRepository.php
491 lines (403 loc) · 11 KB
/
BaseRepository.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
<?php
declare(strict_types=1);
namespace Rinvex\Repository\Repositories;
use Closure;
use Rinvex\Repository\Traits\Cacheable;
use Illuminate\Contracts\Container\Container;
use Rinvex\Repository\Contracts\CacheableContract;
use Rinvex\Repository\Contracts\RepositoryContract;
abstract class BaseRepository implements RepositoryContract, CacheableContract
{
use Cacheable;
/**
* The IoC container instance.
*
* @var \Illuminate\Contracts\Container\Container
*/
protected $container;
/**
* The connection name for the repository.
*
* @var string
*/
protected $connection;
/**
* The repository identifier.
*
* @var string
*/
protected $repositoryId;
/**
* The repository model.
*
* @var string
*/
protected $model;
/**
* The relations to eager load on query execution.
*
* @var array
*/
protected $relations = [];
/**
* The query where clauses.
*
* @var array
*/
protected $where = [];
/**
* The query whereIn clauses.
*
* @var array
*/
protected $whereIn = [];
/**
* The query whereNotIn clauses.
*
* @var array
*/
protected $whereNotIn = [];
/**
* The query whereHas clauses.
*
* @var array
*/
protected $whereHas = [];
/**
* The query scopes.
*
* @var array
*/
protected $scopes = [];
/**
* The "offset" value of the query.
*
* @var int
*/
protected $offset;
/**
* The "limit" value of the query.
*
* @var int
*/
protected $limit;
/**
* The column to order results by.
*
* @var array
*/
protected $orderBy = [];
/**
* The column to order results by.
*
* @var array
*/
protected $groupBy = [];
/**
* The query having clauses.
*
* @var array
*/
protected $having = [];
/**
* Execute given callback and return the result.
*
* @param string $class
* @param string $method
* @param array $args
* @param \Closure $closure
*
* @return mixed
*/
protected function executeCallback($class, $method, $args, Closure $closure)
{
$skipUri = $this->getContainer('config')->get('rinvex.repository.cache.skip_uri');
// Check if cache is enabled
if ($this->getCacheLifetime() && ! $this->getContainer('request')->has($skipUri)) {
return $this->cacheCallback($class, $method, $args, $closure);
}
// Cache disabled, just execute query & return result
$result = call_user_func($closure);
// We're done, let's clean up!
$this->resetRepository();
return $result;
}
/**
* Reset repository to its defaults.
*
* @return $this
*/
protected function resetRepository()
{
$this->relations = [];
$this->where = [];
$this->whereIn = [];
$this->whereNotIn = [];
$this->whereHas = [];
$this->scopes = [];
$this->offset = null;
$this->limit = null;
$this->orderBy = [];
$this->groupBy = [];
$this->having = [];
if (method_exists($this, 'flushCriteria')) {
$this->flushCriteria();
}
return $this;
}
/**
* Prepare query.
*
* @param object $model
*
* @return mixed
*/
protected function prepareQuery($model)
{
// Set the relationships that should be eager loaded
if (! empty($this->relations)) {
$model = $model->with($this->relations);
}
// Add a basic where clause to the query
foreach ($this->where as $where) {
[$attribute, $operator, $value, $boolean] = array_pad($where, 4, null);
$model = $model->where($attribute, $operator, $value, $boolean);
}
// Add a "where in" clause to the query
foreach ($this->whereIn as $whereIn) {
[$attribute, $values, $boolean, $not] = array_pad($whereIn, 4, null);
$model = $model->whereIn($attribute, $values, $boolean, $not);
}
// Add a "where not in" clause to the query
foreach ($this->whereNotIn as $whereNotIn) {
[$attribute, $values, $boolean] = array_pad($whereNotIn, 3, null);
$model = $model->whereNotIn($attribute, $values, $boolean);
}
// Add a "where has" clause to the query
foreach ($this->whereHas as $whereHas) {
[$relation, $callback, $operator, $count] = array_pad($whereHas, 4, null);
$model = $model->whereHas($relation, $callback, $operator, $count);
}
// Add a "scope" to the query
foreach ($this->scopes as $scope => $parameters) {
$model = $model->{$scope}(...$parameters);
}
// Set the "offset" value of the query
if ($this->offset > 0) {
$model = $model->offset($this->offset);
}
// Set the "limit" value of the query
if ($this->limit > 0) {
$model = $model->limit($this->limit);
}
// Add an "order by" clause to the query.
foreach ($this->orderBy as $orderBy) {
[$attribute, $direction] = $orderBy;
$model = $model->orderBy($attribute, $direction);
}
// Add an "group by" clause to the query.
if (! empty($this->groupBy)) {
foreach ($this->groupBy as $group) {
$model = $model->groupBy($group);
}
}
// Add a "having" clause to the query
foreach ($this->having as $having) {
[$column, $operator, $value, $boolean] = array_pad($having, 4, null);
$model = $model->having($column, $operator, $value, $boolean);
}
// Apply all criteria to the query
if (method_exists($this, 'applyCriteria')) {
$model = $this->applyCriteria($model, $this);
}
return $model;
}
/**
* {@inheritdoc}
*/
public function setContainer(Container $container)
{
$this->container = $container;
return $this;
}
/**
* {@inheritdoc}
*/
public function getContainer($service = null)
{
return is_null($service) ? ($this->container ?: app()) : ($this->container[$service] ?: app($service));
}
/**
* {@inheritdoc}
*/
public function setConnection($name)
{
$this->connection = $name;
return $this;
}
/**
* {@inheritdoc}
*/
public function getConnection(): string
{
return $this->connection;
}
/**
* {@inheritdoc}
*/
public function setRepositoryId($repositoryId)
{
$this->repositoryId = $repositoryId;
return $this;
}
/**
* {@inheritdoc}
*/
public function getRepositoryId(): string
{
return $this->repositoryId ?: get_called_class();
}
/**
* {@inheritdoc}
*/
public function setModel($model)
{
$this->model = $model;
return $this;
}
/**
* {@inheritdoc}
*/
public function getModel(): string
{
$model = $this->getContainer('config')->get('rinvex.repository.models');
return $this->model ?: str_replace(['Repositories', 'Repository'], [$model, ''], get_called_class());
}
/**
* {@inheritdoc}
*/
public function with($relations)
{
if (is_string($relations)) {
$relations = func_get_args();
}
$this->relations = $relations;
return $this;
}
/**
* {@inheritdoc}
*/
public function where($attribute, $operator = null, $value = null, $boolean = 'and')
{
// The last `$boolean` expression is intentional to fix list() & array_pad() results
$this->where[] = [$attribute, $operator, $value, $boolean ?: 'and'];
return $this;
}
/**
* {@inheritdoc}
*/
public function whereIn($attribute, $values, $boolean = 'and', $not = false)
{
// The last `$boolean` & `$not` expressions are intentional to fix list() & array_pad() results
$this->whereIn[] = [$attribute, $values, $boolean ?: 'and', (bool) $not];
return $this;
}
/**
* {@inheritdoc}
*/
public function whereNotIn($attribute, $values, $boolean = 'and')
{
// The last `$boolean` expression is intentional to fix list() & array_pad() results
$this->whereNotIn[] = [$attribute, $values, $boolean ?: 'and'];
return $this;
}
/**
* {@inheritdoc}
*/
public function whereHas($relation, Closure $callback = null, $operator = '>=', $count = 1)
{
// The last `$operator` & `$count` expressions are intentional to fix list() & array_pad() results
$this->whereHas[] = [$relation, $callback, $operator ?: '>=', $count ?: 1];
return $this;
}
/**
* {@inheritdoc}
*/
public function scope($name, array $parameters = [])
{
$this->scopes[$name] = $parameters;
return $this;
}
/**
* {@inheritdoc}
*/
public function offset($offset)
{
$this->offset = $offset;
return $this;
}
/**
* {@inheritdoc}
*/
public function limit($limit)
{
$this->limit = $limit;
return $this;
}
/**
* {@inheritdoc}
*/
public function orderBy($attribute, $direction = 'asc')
{
$this->orderBy[] = [$attribute, $direction ?: 'asc'];
return $this;
}
/**
* {@inheritdoc}
*/
public function groupBy($column)
{
$this->groupBy = array_merge((array) $this->groupBy, is_array($column) ? $column : [$column]);
return $this;
}
/**
* {@inheritdoc}
*/
public function having($column, $operator = null, $value = null, $boolean = 'and')
{
$this->having[] = [$column, $operator, $value, $boolean ?: 'and'];
return $this;
}
/**
* {@inheritdoc}
*/
public function orHaving($column, $operator = null, $value = null, $boolean = 'and')
{
return $this->having($column, $operator, $value, 'or');
}
/**
* {@inheritdoc}
*/
public function store($id, array $attributes = [], bool $syncRelations = false)
{
return ! $id ? $this->create($attributes, $syncRelations) : $this->update($id, $attributes, $syncRelations);
}
/**
* {@inheritdoc}
*/
public static function __callStatic($method, $parameters)
{
return call_user_func_array([new static(), $method], $parameters);
}
/**
* {@inheritdoc}
*/
public function __call($method, $parameters)
{
if (method_exists($model = $this->createModel(), $scope = 'scope'.ucfirst($method))) {
$this->scope($method, $parameters);
return $this;
}
return call_user_func_array([$this->createModel(), $method], $parameters);
}
}