1
|
|
|
<?php namespace Anomaly\Streams\Platform\Model; |
2
|
|
|
|
3
|
|
|
use Anomaly\Streams\Platform\Entry\EntryModel; |
4
|
|
|
use Anomaly\Streams\Platform\Model\Contract\EloquentRepositoryInterface; |
5
|
|
|
use Anomaly\Streams\Platform\Traits\FiresCallbacks; |
6
|
|
|
use Anomaly\Streams\Platform\Traits\Hookable; |
7
|
|
|
use Illuminate\Contracts\Pagination\LengthAwarePaginator; |
8
|
|
|
use Illuminate\Database\Eloquent\Builder; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class EloquentRepository |
12
|
|
|
* |
13
|
|
|
* @property EloquentModel|EntryModel $model |
14
|
|
|
* |
15
|
|
|
* @link http://pyrocms.com/ |
16
|
|
|
* @author PyroCMS, Inc. <[email protected]> |
17
|
|
|
* @author Ryan Thompson <[email protected]> |
18
|
|
|
*/ |
19
|
|
|
class EloquentRepository implements EloquentRepositoryInterface |
20
|
|
|
{ |
21
|
|
|
|
22
|
|
|
use FiresCallbacks; |
23
|
|
|
use Hookable; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Return all records. |
27
|
|
|
* |
28
|
|
|
* @return EloquentCollection |
29
|
|
|
*/ |
30
|
|
|
public function all() |
31
|
|
|
{ |
32
|
|
|
return $this->model->all(); |
|
|
|
|
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Find a record by it's ID. |
37
|
|
|
* |
38
|
|
|
* @param $id |
39
|
|
|
* @return EloquentModel |
40
|
|
|
*/ |
41
|
|
|
public function find($id) |
42
|
|
|
{ |
43
|
|
|
return $this->model->find($id); |
|
|
|
|
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Find all records by IDs. |
48
|
|
|
* |
49
|
|
|
* @param array $ids |
50
|
|
|
* @return EloquentCollection |
51
|
|
|
*/ |
52
|
|
|
public function findAll(array $ids) |
53
|
|
|
{ |
54
|
|
|
return $this->model->whereIn('id', $ids)->get(); |
|
|
|
|
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Find a trashed record by it's ID. |
59
|
|
|
* |
60
|
|
|
* @param $id |
61
|
|
|
* @return null|EloquentModel |
62
|
|
|
*/ |
63
|
|
|
public function findTrashed($id) |
64
|
|
|
{ |
65
|
|
|
return $this->model |
|
|
|
|
66
|
|
|
->trashed() |
67
|
|
|
->orderBy('id', 'ASC') |
68
|
|
|
->where('id', $id) |
69
|
|
|
->first(); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Create a new record. |
74
|
|
|
* |
75
|
|
|
* @param array $attributes |
76
|
|
|
* @return EloquentModel |
77
|
|
|
*/ |
78
|
|
|
public function create(array $attributes) |
79
|
|
|
{ |
80
|
|
|
return $this->model->create($attributes); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Return a new query builder. |
85
|
|
|
* |
86
|
|
|
* @return Builder |
87
|
|
|
*/ |
88
|
|
|
public function newQuery() |
89
|
|
|
{ |
90
|
|
|
return $this->model->newQuery(); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Return a new instance. |
95
|
|
|
* |
96
|
|
|
* @param array $attributes |
97
|
|
|
* @return EloquentModel |
98
|
|
|
*/ |
99
|
|
|
public function newInstance(array $attributes = []) |
100
|
|
|
{ |
101
|
|
|
return $this->model->newInstance($attributes); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Count all records. |
106
|
|
|
* |
107
|
|
|
* @return int |
108
|
|
|
*/ |
109
|
|
|
public function count() |
110
|
|
|
{ |
111
|
|
|
return $this->model->count(); |
|
|
|
|
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Return a paginated collection. |
116
|
|
|
* |
117
|
|
|
* @param array $parameters |
118
|
|
|
* @return LengthAwarePaginator |
119
|
|
|
*/ |
120
|
|
|
public function paginate(array $parameters = []) |
121
|
|
|
{ |
122
|
|
|
$paginator = array_pull($parameters, 'paginator'); |
123
|
|
|
$perPage = array_pull($parameters, 'per_page', config('streams::system.per_page', 15)); |
124
|
|
|
|
125
|
|
|
/* @var Builder $query */ |
126
|
|
|
$query = $this->model->newQuery(); |
127
|
|
|
|
128
|
|
|
/* |
129
|
|
|
* First apply any desired scope. |
130
|
|
|
*/ |
131
|
|
|
if ($scope = array_pull($parameters, 'scope')) { |
132
|
|
|
call_user_func([$query, camel_case($scope)], array_pull($parameters, 'scope_arguments', [])); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/* |
136
|
|
|
* Lastly we need to loop through all of the |
137
|
|
|
* parameters and assume the rest are methods |
138
|
|
|
* to call on the query builder. |
139
|
|
|
*/ |
140
|
|
|
foreach ($parameters as $method => $arguments) { |
141
|
|
|
$method = camel_case($method); |
142
|
|
|
|
143
|
|
|
if (in_array($method, ['update', 'delete'])) { |
144
|
|
|
continue; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
if (is_array($arguments)) { |
148
|
|
|
call_user_func_array([$query, $method], $arguments); |
149
|
|
|
} else { |
150
|
|
|
call_user_func_array([$query, $method], [$arguments]); |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
if ($paginator === 'simple') { |
155
|
|
|
$pagination = $query->simplePaginate($perPage); |
156
|
|
|
} else { |
157
|
|
|
$pagination = $query->paginate($perPage); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
return $pagination; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* Save a record. |
165
|
|
|
* |
166
|
|
|
* @param EloquentModel $entry |
167
|
|
|
* @return bool |
168
|
|
|
*/ |
169
|
|
|
public function save(EloquentModel $entry) |
170
|
|
|
{ |
171
|
|
|
return $entry->save(); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* Update multiple records. |
176
|
|
|
* |
177
|
|
|
* @param array $attributes |
178
|
|
|
* @return bool |
179
|
|
|
*/ |
180
|
|
|
public function update(array $attributes = []) |
181
|
|
|
{ |
182
|
|
|
return $this->model->update($attributes); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* Delete a record. |
187
|
|
|
* |
188
|
|
|
* @param EloquentModel $entry |
189
|
|
|
* @return bool |
190
|
|
|
*/ |
191
|
|
|
public function delete(EloquentModel $entry) |
192
|
|
|
{ |
193
|
|
|
return $entry->delete(); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* Force delete a record. |
198
|
|
|
* |
199
|
|
|
* @param EloquentModel $entry |
200
|
|
|
* @return bool |
201
|
|
|
*/ |
202
|
|
|
public function forceDelete(EloquentModel $entry) |
203
|
|
|
{ |
204
|
|
|
$entry->forceDelete(); |
205
|
|
|
|
206
|
|
|
/* |
207
|
|
|
* If we were not able to force delete |
208
|
|
|
*/ |
209
|
|
|
|
210
|
|
|
return !$entry->exists; |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* Restore a trashed record. |
215
|
|
|
* |
216
|
|
|
* @param EloquentModel $entry |
217
|
|
|
* @return bool |
218
|
|
|
*/ |
219
|
|
|
public function restore(EloquentModel $entry) |
220
|
|
|
{ |
221
|
|
|
return $entry->restore(); |
|
|
|
|
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* Truncate the entries. |
226
|
|
|
* |
227
|
|
|
* @return $this |
228
|
|
|
*/ |
229
|
|
|
public function truncate() |
230
|
|
|
{ |
231
|
|
|
$this->model->flushCache(); |
232
|
|
|
|
233
|
|
|
foreach ($this->model->all() as $entry) { |
234
|
|
|
$this->delete($entry); |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
$this->model->truncate(); // Clear trash |
|
|
|
|
238
|
|
|
|
239
|
|
|
if ($this->model->isTranslatable() && $translation = $this->model->getTranslationModel()) { |
240
|
|
|
$translation->flushCache(); |
241
|
|
|
|
242
|
|
|
foreach ($translation->all() as $entry) { |
243
|
|
|
$this->delete($entry); |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
$translation->truncate(); // Clear trash |
|
|
|
|
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
return $this; |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
/** |
253
|
|
|
* Cache a value in the |
254
|
|
|
* model's cache collection. |
255
|
|
|
* |
256
|
|
|
* @param $key |
257
|
|
|
* @param $ttl |
258
|
|
|
* @param $value |
259
|
|
|
* @return mixed |
260
|
|
|
*/ |
261
|
|
|
public function cache($key, $ttl, $value) |
262
|
|
|
{ |
263
|
|
|
return $this->model->cache($key, $ttl, $value); |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
/** |
267
|
|
|
* Guard the model. |
268
|
|
|
* |
269
|
|
|
* @return $this |
270
|
|
|
*/ |
271
|
|
|
public function guard() |
272
|
|
|
{ |
273
|
|
|
$this->model->reguard(); |
274
|
|
|
|
275
|
|
|
return $this; |
276
|
|
|
} |
277
|
|
|
|
278
|
|
|
/** |
279
|
|
|
* Unguard the model. |
280
|
|
|
* |
281
|
|
|
* @return $this |
282
|
|
|
*/ |
283
|
|
|
public function unguard() |
284
|
|
|
{ |
285
|
|
|
$this->model->unguard(); |
286
|
|
|
|
287
|
|
|
return $this; |
288
|
|
|
} |
289
|
|
|
|
290
|
|
|
/** |
291
|
|
|
* Set the model. |
292
|
|
|
* |
293
|
|
|
* @param EloquentModel $model |
294
|
|
|
* @return $this |
295
|
|
|
*/ |
296
|
|
|
public function setModel(EloquentModel $model) |
297
|
|
|
{ |
298
|
|
|
$this->model = $model; |
299
|
|
|
|
300
|
|
|
return $this; |
301
|
|
|
} |
302
|
|
|
|
303
|
|
|
/** |
304
|
|
|
* Get the model. |
305
|
|
|
* |
306
|
|
|
* @return EloquentModel |
307
|
|
|
*/ |
308
|
|
|
public function getModel() |
309
|
|
|
{ |
310
|
|
|
return $this->model; |
311
|
|
|
} |
312
|
|
|
|
313
|
|
|
/** |
314
|
|
|
* Pipe non-existing calls through hooks. |
315
|
|
|
* |
316
|
|
|
* @param $method |
317
|
|
|
* @param $parameters |
318
|
|
|
* @return mixed|null |
319
|
|
|
*/ |
320
|
|
View Code Duplication |
public function __call($method, $parameters) |
|
|
|
|
321
|
|
|
{ |
322
|
|
|
if ($this->hasHook($hook = snake_case($method))) { |
323
|
|
|
return $this->call($hook, $parameters); |
324
|
|
|
} |
325
|
|
|
|
326
|
|
|
return null; |
327
|
|
|
} |
328
|
|
|
} |
329
|
|
|
|