|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* NOTICE OF LICENSE |
|
5
|
|
|
* |
|
6
|
|
|
* Part of the Rinvex Repository Package. |
|
7
|
|
|
* |
|
8
|
|
|
* This source file is subject to The MIT License (MIT) |
|
9
|
|
|
* that is bundled with this package in the LICENSE file. |
|
10
|
|
|
* |
|
11
|
|
|
* Package: Rinvex Repository Package |
|
12
|
|
|
* License: The MIT License (MIT) |
|
13
|
|
|
* Link: https://rinvex.com |
|
14
|
|
|
*/ |
|
15
|
|
|
|
|
16
|
|
|
namespace Rinvex\Repository\Repositories; |
|
17
|
|
|
|
|
18
|
|
|
use Illuminate\Pagination\Paginator; |
|
19
|
|
|
use Illuminate\Database\Eloquent\Model; |
|
20
|
|
|
use Rinvex\Repository\Exceptions\RepositoryException; |
|
21
|
|
|
use Rinvex\Repository\Exceptions\EntityNotFoundException; |
|
22
|
|
|
|
|
23
|
|
|
class EloquentRepository extends BaseRepository |
|
24
|
|
|
{ |
|
25
|
|
|
/** |
|
26
|
|
|
* {@inheritdoc} |
|
27
|
|
|
*/ |
|
28
|
|
|
public function createModel() |
|
29
|
|
|
{ |
|
30
|
|
|
if (is_string($model = $this->getModel())) { |
|
31
|
|
|
if (! class_exists($class = '\\'.ltrim($model, '\\'))) { |
|
32
|
|
|
throw new RepositoryException("Class {$model} does NOT exist!"); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
$model = $this->getContainer()->make($class); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
// Set the connection used by the model |
|
39
|
|
|
if (! empty($this->connection)) { |
|
40
|
|
|
$model = $model->setConnection($this->connection); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
if (! $model instanceof Model) { |
|
44
|
|
|
throw new RepositoryException("Class {$model} must be an instance of \\Illuminate\\Database\\Eloquent\\Model"); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
return $model; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* {@inheritdoc} |
|
52
|
|
|
*/ |
|
53
|
|
|
public function find($id, $attributes = ['*']) |
|
54
|
|
|
{ |
|
55
|
|
|
return $this->executeCallback(get_called_class(), __FUNCTION__, func_get_args(), function () use ($id, $attributes) { |
|
56
|
|
|
return $this->prepareQuery($this->createModel())->find($id, $attributes); |
|
57
|
|
|
}); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* {@inheritdoc} |
|
62
|
|
|
*/ |
|
63
|
|
|
public function findOrFail($id, $attributes = ['*']) |
|
64
|
|
|
{ |
|
65
|
|
|
$result = $this->find($id, $attributes); |
|
66
|
|
|
|
|
67
|
|
|
if (is_array($id)) { |
|
68
|
|
|
if (count($result) == count(array_unique($id))) { |
|
69
|
|
|
return $result; |
|
70
|
|
|
} |
|
71
|
|
|
} elseif (! is_null($result)) { |
|
72
|
|
|
return $result; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
throw new EntityNotFoundException($this->getModel(), $id); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* {@inheritdoc} |
|
80
|
|
|
*/ |
|
81
|
|
|
public function findOrNew($id, $attributes = ['*']) |
|
82
|
|
|
{ |
|
83
|
|
|
if (! is_null($entity = $this->find($id, $attributes))) { |
|
84
|
|
|
return $entity; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
return $this->createModel(); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* {@inheritdoc} |
|
92
|
|
|
*/ |
|
93
|
|
|
public function findBy($attribute, $value, $attributes = ['*']) |
|
94
|
|
|
{ |
|
95
|
|
|
return $this->executeCallback(get_called_class(), __FUNCTION__, func_get_args(), function () use ($attribute, $value, $attributes) { |
|
96
|
|
|
return $this->prepareQuery($this->createModel())->where($attribute, '=', $value)->first($attributes); |
|
97
|
|
|
}); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* {@inheritdoc} |
|
102
|
|
|
*/ |
|
103
|
|
|
public function findFirst($attributes = ['*']) |
|
104
|
|
|
{ |
|
105
|
|
|
return $this->executeCallback(get_called_class(), __FUNCTION__, func_get_args(), function () use ($attributes) { |
|
106
|
|
|
return $this->prepareQuery($this->createModel())->first($attributes); |
|
107
|
|
|
}); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* {@inheritdoc} |
|
112
|
|
|
*/ |
|
113
|
|
|
public function findAll($attributes = ['*']) |
|
114
|
|
|
{ |
|
115
|
|
|
return $this->executeCallback(get_called_class(), __FUNCTION__, func_get_args(), function () use ($attributes) { |
|
116
|
|
|
return $this->prepareQuery($this->createModel())->get($attributes); |
|
117
|
|
|
}); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* {@inheritdoc} |
|
122
|
|
|
*/ |
|
123
|
|
|
public function paginate($perPage = null, $attributes = ['*'], $pageName = 'page', $page = null) |
|
124
|
|
|
{ |
|
125
|
|
|
$page = $page ?: Paginator::resolveCurrentPage($pageName); |
|
126
|
|
|
|
|
127
|
|
|
return $this->executeCallback(get_called_class(), __FUNCTION__, array_merge(func_get_args(), compact('page')), function () use ($perPage, $attributes, $pageName, $page) { |
|
128
|
|
|
return $this->prepareQuery($this->createModel())->paginate($perPage, $attributes, $pageName, $page); |
|
129
|
|
|
}); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* {@inheritdoc} |
|
134
|
|
|
*/ |
|
135
|
|
|
public function simplePaginate($perPage = null, $attributes = ['*'], $pageName = 'page', $page = null) |
|
136
|
|
|
{ |
|
137
|
|
|
$page = $page ?: Paginator::resolveCurrentPage($pageName); |
|
138
|
|
|
|
|
139
|
|
|
return $this->executeCallback(get_called_class(), __FUNCTION__, array_merge(func_get_args(), compact('page')), function () use ($perPage, $attributes, $pageName, $page) { |
|
|
|
|
|
|
140
|
|
|
return $this->prepareQuery($this->createModel())->simplePaginate($perPage, $attributes, $pageName, $page); |
|
141
|
|
|
}); |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* {@inheritdoc} |
|
146
|
|
|
*/ |
|
147
|
|
|
public function findWhere(array $where, $attributes = ['*']) |
|
148
|
|
|
{ |
|
149
|
|
|
return $this->executeCallback(get_called_class(), __FUNCTION__, func_get_args(), function () use ($where, $attributes) { |
|
|
|
|
|
|
150
|
|
|
list($attribute, $operator, $value, $boolean) = array_pad($where, 4, null); |
|
151
|
|
|
|
|
152
|
|
|
$this->where($attribute, $operator, $value, $boolean); |
|
153
|
|
|
|
|
154
|
|
|
return $this->prepareQuery($this->createModel())->get($attributes); |
|
155
|
|
|
}); |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
/** |
|
159
|
|
|
* {@inheritdoc} |
|
160
|
|
|
*/ |
|
161
|
|
|
public function findWhereIn(array $where, $attributes = ['*']) |
|
162
|
|
|
{ |
|
163
|
|
|
return $this->executeCallback(get_called_class(), __FUNCTION__, func_get_args(), function () use ($where, $attributes) { |
|
164
|
|
|
list($attribute, $values, $boolean, $not) = array_pad($where, 4, null); |
|
165
|
|
|
|
|
166
|
|
|
$this->whereIn($attribute, $values, $boolean, $not); |
|
167
|
|
|
|
|
168
|
|
|
return $this->prepareQuery($this->createModel())->get($attributes); |
|
169
|
|
|
}); |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
/** |
|
173
|
|
|
* {@inheritdoc} |
|
174
|
|
|
*/ |
|
175
|
|
|
public function findWhereNotIn(array $where, $attributes = ['*']) |
|
176
|
|
|
{ |
|
177
|
|
|
return $this->executeCallback(get_called_class(), __FUNCTION__, func_get_args(), function () use ($where, $attributes) { |
|
178
|
|
|
list($attribute, $values, $boolean) = array_pad($where, 3, null); |
|
179
|
|
|
|
|
180
|
|
|
$this->whereNotIn($attribute, $values, $boolean); |
|
181
|
|
|
|
|
182
|
|
|
return $this->prepareQuery($this->createModel())->get($attributes); |
|
183
|
|
|
}); |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
/** |
|
187
|
|
|
* {@inheritdoc} |
|
188
|
|
|
*/ |
|
189
|
|
|
public function findWhereHas(array $where, $attributes = ['*']) |
|
190
|
|
|
{ |
|
191
|
|
|
return $this->executeCallback(get_called_class(), __FUNCTION__, func_get_args(), function () use ($where, $attributes) { |
|
|
|
|
|
|
192
|
|
|
list($relation, $callback, $operator, $count) = array_pad($where, 4, null); |
|
193
|
|
|
|
|
194
|
|
|
$this->whereHas($relation, $callback, $operator, $count); |
|
195
|
|
|
|
|
196
|
|
|
return $this->prepareQuery($this->createModel())->get($attributes); |
|
197
|
|
|
}); |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
|
/** |
|
201
|
|
|
* {@inheritdoc} |
|
202
|
|
|
*/ |
|
203
|
|
|
public function create(array $attributes = [], bool $sync = false) |
|
204
|
|
|
{ |
|
205
|
|
|
// Create a new instance |
|
206
|
|
|
$entity = $this->createModel(); |
|
207
|
|
|
|
|
208
|
|
|
// Fire the created event |
|
209
|
|
|
$this->getContainer('events')->fire($this->getRepositoryId().'.entity.creating', [$this, $entity]); |
|
210
|
|
|
|
|
211
|
|
|
// Fill instance with data |
|
212
|
|
|
$entity->fill($attributes); |
|
213
|
|
|
|
|
214
|
|
|
// Save the instance |
|
215
|
|
|
$created = $entity->save(); |
|
216
|
|
|
|
|
217
|
|
|
// Extract and sync relationships if sync true |
|
218
|
|
|
if ($sync) { |
|
219
|
|
|
$relations = $this->extractRelations($entity, $attributes); |
|
220
|
|
|
$this->syncRelations($entity, $relations); |
|
221
|
|
|
} |
|
222
|
|
|
|
|
223
|
|
|
// Fire the created event |
|
224
|
|
|
$this->getContainer('events')->fire($this->getRepositoryId().'.entity.created', [$this, $entity]); |
|
225
|
|
|
|
|
226
|
|
|
// Return instance |
|
227
|
|
|
return $created ? $entity : $created; |
|
228
|
|
|
} |
|
229
|
|
|
|
|
230
|
|
|
/** |
|
231
|
|
|
* {@inheritdoc} |
|
232
|
|
|
*/ |
|
233
|
|
|
public function update($id, array $attributes = [], bool $sync = false) |
|
234
|
|
|
{ |
|
235
|
|
|
$updated = false; |
|
236
|
|
|
|
|
237
|
|
|
// Find the given instance |
|
238
|
|
|
$entity = $id instanceof Model ? $id : $this->find($id); |
|
239
|
|
|
|
|
240
|
|
|
if ($entity) { |
|
241
|
|
|
// Fire the updated event |
|
242
|
|
|
$this->getContainer('events')->fire($this->getRepositoryId().'.entity.updating', [$this, $entity]); |
|
243
|
|
|
|
|
244
|
|
|
// Fill instance with data |
|
245
|
|
|
$entity->fill($attributes); |
|
246
|
|
|
|
|
247
|
|
|
//Check if we are updating attributes values |
|
248
|
|
|
$dirty = $entity->getDirty(); |
|
249
|
|
|
|
|
250
|
|
|
// Update the instance |
|
251
|
|
|
$updated = $entity->save(); |
|
252
|
|
|
|
|
253
|
|
|
// Extract and sync relationships if sync true |
|
254
|
|
|
if ($sync) { |
|
255
|
|
|
$relations = $this->extractRelations($entity, $attributes); |
|
256
|
|
|
$this->syncRelations($entity, $relations); |
|
257
|
|
|
} |
|
258
|
|
|
|
|
259
|
|
|
if (count($dirty) > 0) { |
|
260
|
|
|
// Fire the updated event |
|
261
|
|
|
$this->getContainer('events')->fire($this->getRepositoryId().'.entity.updated', [$this, $entity]); |
|
262
|
|
|
} |
|
263
|
|
|
} |
|
264
|
|
|
|
|
265
|
|
|
return $updated ? $entity : $updated; |
|
266
|
|
|
} |
|
267
|
|
|
|
|
268
|
|
|
/** |
|
269
|
|
|
* {@inheritdoc} |
|
270
|
|
|
*/ |
|
271
|
|
|
public function delete($id) |
|
272
|
|
|
{ |
|
273
|
|
|
$deleted = false; |
|
274
|
|
|
|
|
275
|
|
|
// Find the given instance |
|
276
|
|
|
$entity = $id instanceof Model ? $id : $this->find($id); |
|
277
|
|
|
|
|
278
|
|
|
if ($entity) { |
|
279
|
|
|
// Fire the deleted event |
|
280
|
|
|
$this->getContainer('events')->fire($this->getRepositoryId().'.entity.deleting', [$this, $entity]); |
|
281
|
|
|
|
|
282
|
|
|
// Delete the instance |
|
283
|
|
|
$deleted = $entity->delete(); |
|
284
|
|
|
|
|
285
|
|
|
// Fire the deleted event |
|
286
|
|
|
$this->getContainer('events')->fire($this->getRepositoryId().'.entity.deleted', [$this, $entity]); |
|
287
|
|
|
} |
|
288
|
|
|
|
|
289
|
|
|
return $deleted ? $entity : $deleted; |
|
290
|
|
|
} |
|
291
|
|
|
|
|
292
|
|
|
/** |
|
293
|
|
|
* {@inheritdoc} |
|
294
|
|
|
*/ |
|
295
|
|
|
public function restore($id) |
|
296
|
|
|
{ |
|
297
|
|
|
$restored = false; |
|
298
|
|
|
|
|
299
|
|
|
// Find the given instance |
|
300
|
|
|
$entity = $id instanceof Model ? $id : $this->find($id); |
|
301
|
|
|
|
|
302
|
|
|
if ($entity) { |
|
303
|
|
|
// Fire the restoring event |
|
304
|
|
|
$this->getContainer('events')->fire($this->getRepositoryId().'.entity.restoring', [$this, $entity]); |
|
305
|
|
|
|
|
306
|
|
|
// Restore the instance |
|
307
|
|
|
$restored = $entity->restore(); |
|
308
|
|
|
|
|
309
|
|
|
// Fire the restored event |
|
310
|
|
|
$this->getContainer('events')->fire($this->getRepositoryId().'.entity.restored', [$this, $entity]); |
|
311
|
|
|
} |
|
312
|
|
|
|
|
313
|
|
|
return $restored ? $entity : $restored; |
|
314
|
|
|
} |
|
315
|
|
|
|
|
316
|
|
|
/** |
|
317
|
|
|
* {@inheritdoc} |
|
318
|
|
|
*/ |
|
319
|
|
|
public function beginTransaction() |
|
320
|
|
|
{ |
|
321
|
|
|
$this->getContainer('db')->beginTransaction(); |
|
322
|
|
|
} |
|
323
|
|
|
|
|
324
|
|
|
/** |
|
325
|
|
|
* {@inheritdoc} |
|
326
|
|
|
*/ |
|
327
|
|
|
public function commit() |
|
328
|
|
|
{ |
|
329
|
|
|
$this->getContainer('db')->commit(); |
|
330
|
|
|
} |
|
331
|
|
|
|
|
332
|
|
|
/** |
|
333
|
|
|
* {@inheritdoc} |
|
334
|
|
|
*/ |
|
335
|
|
|
public function rollBack() |
|
336
|
|
|
{ |
|
337
|
|
|
$this->getContainer('db')->rollBack(); |
|
338
|
|
|
} |
|
339
|
|
|
|
|
340
|
|
|
/** |
|
341
|
|
|
* {@inheritdoc} |
|
342
|
|
|
*/ |
|
343
|
|
|
public function count($columns = '*') |
|
344
|
|
|
{ |
|
345
|
|
|
return $this->executeCallback(get_called_class(), __FUNCTION__, func_get_args(), function () use ($columns) { |
|
346
|
|
|
return $this->prepareQuery($this->createModel())->count($columns); |
|
347
|
|
|
}); |
|
348
|
|
|
} |
|
349
|
|
|
|
|
350
|
|
|
/** |
|
351
|
|
|
* {@inheritdoc} |
|
352
|
|
|
*/ |
|
353
|
|
|
public function min($column) |
|
354
|
|
|
{ |
|
355
|
|
|
return $this->executeCallback(get_called_class(), __FUNCTION__, func_get_args(), function () use ($column) { |
|
356
|
|
|
return $this->prepareQuery($this->createModel())->min($column); |
|
357
|
|
|
}); |
|
358
|
|
|
} |
|
359
|
|
|
|
|
360
|
|
|
/** |
|
361
|
|
|
* {@inheritdoc} |
|
362
|
|
|
*/ |
|
363
|
|
|
public function max($column) |
|
364
|
|
|
{ |
|
365
|
|
|
return $this->executeCallback(get_called_class(), __FUNCTION__, func_get_args(), function () use ($column) { |
|
366
|
|
|
return $this->prepareQuery($this->createModel())->max($column); |
|
367
|
|
|
}); |
|
368
|
|
|
} |
|
369
|
|
|
|
|
370
|
|
|
/** |
|
371
|
|
|
* {@inheritdoc} |
|
372
|
|
|
*/ |
|
373
|
|
|
public function avg($column) |
|
374
|
|
|
{ |
|
375
|
|
|
return $this->executeCallback(get_called_class(), __FUNCTION__, func_get_args(), function () use ($column) { |
|
376
|
|
|
return $this->prepareQuery($this->createModel())->avg($column); |
|
377
|
|
|
}); |
|
378
|
|
|
} |
|
379
|
|
|
|
|
380
|
|
|
/** |
|
381
|
|
|
* {@inheritdoc} |
|
382
|
|
|
*/ |
|
383
|
|
|
public function sum($column) |
|
384
|
|
|
{ |
|
385
|
|
|
return $this->executeCallback(get_called_class(), __FUNCTION__, func_get_args(), function () use ($column) { |
|
386
|
|
|
return $this->prepareQuery($this->createModel())->sum($column); |
|
387
|
|
|
}); |
|
388
|
|
|
} |
|
389
|
|
|
|
|
390
|
|
|
/** |
|
391
|
|
|
* Extract relationships. |
|
392
|
|
|
* |
|
393
|
|
|
* @param mixed $entity |
|
394
|
|
|
* @param array $attributes |
|
395
|
|
|
* |
|
396
|
|
|
* @return array |
|
397
|
|
|
*/ |
|
398
|
|
|
protected function extractRelations($entity, array $attributes) |
|
399
|
|
|
{ |
|
400
|
|
|
$relations = []; |
|
401
|
|
|
$potential = array_diff(array_keys($attributes), $entity->getFillable()); |
|
402
|
|
|
|
|
403
|
|
|
array_walk($potential, function ($item) use ($entity, $attributes, &$relations) { |
|
404
|
|
|
if (method_exists($entity, $item)) { |
|
405
|
|
|
$relations[$item] = [ |
|
406
|
|
|
'values' => $attributes[$item], |
|
407
|
|
|
'class' => get_class($entity->$item()), |
|
408
|
|
|
]; |
|
409
|
|
|
} |
|
410
|
|
|
}); |
|
411
|
|
|
|
|
412
|
|
|
return $relations; |
|
413
|
|
|
} |
|
414
|
|
|
|
|
415
|
|
|
/** |
|
416
|
|
|
* Sync relationships. |
|
417
|
|
|
* |
|
418
|
|
|
* @param mixed $entity |
|
419
|
|
|
* @param array $relations |
|
420
|
|
|
* @param bool $detaching |
|
421
|
|
|
* |
|
422
|
|
|
* @return void |
|
423
|
|
|
*/ |
|
424
|
|
|
protected function syncRelations($entity, array $relations, $detaching = true) |
|
425
|
|
|
{ |
|
426
|
|
|
foreach ($relations as $method => $relation) { |
|
427
|
|
|
switch ($relation['class']) { |
|
428
|
|
|
case 'Illuminate\Database\Eloquent\Relations\BelongsToMany': |
|
429
|
|
|
default: |
|
430
|
|
|
$entity->$method()->sync($relation['values'], $detaching); |
|
431
|
|
|
break; |
|
432
|
|
|
} |
|
433
|
|
|
} |
|
434
|
|
|
} |
|
435
|
|
|
} |
|
436
|
|
|
|
Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.