forked from pterodactyl/panel
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEloquentRepository.php
More file actions
288 lines (247 loc) · 8.68 KB
/
EloquentRepository.php
File metadata and controls
288 lines (247 loc) · 8.68 KB
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
<?php
namespace Pterodactyl\Repositories\Eloquent;
use Illuminate\Http\Request;
use Webmozart\Assert\Assert;
use Illuminate\Support\Collection;
use Illuminate\Database\Eloquent\Model;
use Pterodactyl\Repositories\Repository;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Query\Expression;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
use Pterodactyl\Contracts\Repository\RepositoryInterface;
use Pterodactyl\Exceptions\Model\DataValidationException;
use Pterodactyl\Exceptions\Repository\RecordNotFoundException;
abstract class EloquentRepository extends Repository implements RepositoryInterface
{
protected bool $useRequestFilters = false;
/**
* Determines if the repository function should use filters off the request object
* present when returning results. This allows repository methods to be called in API
* context's such that we can pass through ?filter[name]=Dane&sort=desc for example.
*/
public function usingRequestFilters(bool $usingFilters = true): self
{
$this->useRequestFilters = $usingFilters;
return $this;
}
/**
* Returns the request instance.
*/
protected function request(): Request
{
return $this->app->make(Request::class);
}
/**
* Paginate the response data based on the page para.
*/
protected function paginate(Builder $instance, int $default = 50): LengthAwarePaginator
{
if (!$this->useRequestFilters) {
return $instance->paginate($default);
}
return $instance->paginate($this->request()->query('per_page', $default));
}
/**
* Return an instance of the eloquent model bound to this
* repository instance.
*/
public function getModel(): Model
{
return $this->model;
}
/**
* Return an instance of the builder to use for this repository.
*/
public function getBuilder(): Builder
{
return $this->getModel()->newQuery();
}
/**
* Create a new record in the database and return the associated model.
*
* @throws DataValidationException
*/
public function create(array $fields, bool $validate = true, bool $force = false): Model|bool
{
$instance = $this->getBuilder()->newModelInstance();
($force) ? $instance->forceFill($fields) : $instance->fill($fields);
if (!$validate) {
$saved = $instance->skipValidation()->save();
} else {
if (!$saved = $instance->save()) {
throw new DataValidationException($instance->getValidator(), $instance);
}
}
return ($this->withFresh) ? $instance->fresh() : $saved;
}
/**
* Find a model that has the specific ID passed.
*
* @throws RecordNotFoundException
*/
public function find(int $id): Model
{
try {
return $this->getBuilder()->findOrFail($id, $this->getColumns());
} catch (ModelNotFoundException) {
throw new RecordNotFoundException();
}
}
/**
* Find a model matching an array of where clauses.
*/
public function findWhere(array $fields): Collection
{
return $this->getBuilder()->where($fields)->get($this->getColumns());
}
/**
* Find and return the first matching instance for the given fields.
*
* @throws RecordNotFoundException
*/
public function findFirstWhere(array $fields): Model
{
try {
return $this->getBuilder()->where($fields)->firstOrFail($this->getColumns());
} catch (ModelNotFoundException) {
throw new RecordNotFoundException();
}
}
/**
* Return a count of records matching the passed arguments.
*/
public function findCountWhere(array $fields): int
{
return $this->getBuilder()->where($fields)->count($this->getColumns());
}
/**
* Delete a given record from the database.
*/
public function delete(int $id, bool $destroy = false): int
{
return $this->deleteWhere(['id' => $id], $destroy);
}
/**
* Delete records matching the given attributes.
*/
public function deleteWhere(array $attributes, bool $force = false): int
{
$instance = $this->getBuilder()->where($attributes);
return ($force) ? $instance->forceDelete() : $instance->delete();
}
/**
* Update a given ID with the passed array of fields.
*
* @throws DataValidationException
* @throws RecordNotFoundException
*/
public function update(int $id, array $fields, bool $validate = true, bool $force = false): Model|bool
{
try {
$instance = $this->getBuilder()->where('id', $id)->firstOrFail();
} catch (ModelNotFoundException) {
throw new RecordNotFoundException();
}
($force) ? $instance->forceFill($fields) : $instance->fill($fields);
if (!$validate) {
$saved = $instance->skipValidation()->save();
} else {
if (!$saved = $instance->save()) {
throw new DataValidationException($instance->getValidator(), $instance);
}
}
return ($this->withFresh) ? $instance->fresh() : $saved;
}
/**
* Update a model using the attributes passed.
*/
public function updateWhere(array $attributes, array $values): int
{
return $this->getBuilder()->where($attributes)->update($values);
}
/**
* Perform a mass update where matching records are updated using whereIn.
* This does not perform any model data validation.
*/
public function updateWhereIn(string $column, array $values, array $fields): int
{
Assert::notEmpty($column, 'First argument passed to updateWhereIn must be a non-empty string.');
return $this->getBuilder()->whereIn($column, $values)->update($fields);
}
/**
* Update a record if it exists in the database, otherwise create it.
*
* @throws DataValidationException
* @throws RecordNotFoundException
*/
public function updateOrCreate(array $where, array $fields, bool $validate = true, bool $force = false): Model|bool
{
foreach ($where as $item) {
Assert::true(is_scalar($item) || is_null($item), 'First argument passed to updateOrCreate should be an array of scalar or null values, received an array value of %s.');
}
try {
$instance = $this->setColumns('id')->findFirstWhere($where);
} catch (RecordNotFoundException) {
return $this->create(array_merge($where, $fields), $validate, $force);
}
return $this->update($instance->id, $fields, $validate, $force);
}
/**
* Return all records associated with the given model.
*
* @deprecated Just use the model
*/
public function all(): Collection
{
return $this->getBuilder()->get($this->getColumns());
}
/**
* Return a paginated result set using a search term if set on the repository.
*/
public function paginated(int $perPage): LengthAwarePaginator
{
return $this->getBuilder()->paginate($perPage, $this->getColumns());
}
/**
* Insert a single or multiple records into the database at once skipping
* validation and mass assignment checking.
*/
public function insert(array $data): bool
{
return $this->getBuilder()->insert($data);
}
/**
* Insert multiple records into the database and ignore duplicates.
*/
public function insertIgnore(array $values): bool
{
if (empty($values)) {
return true;
}
foreach ($values as $key => $value) {
ksort($value);
$values[$key] = $value;
}
$bindings = array_values(array_filter(array_flatten($values, 1), function ($binding) {
return !$binding instanceof Expression;
}));
$grammar = $this->getBuilder()->toBase()->getGrammar();
$table = $grammar->wrapTable($this->getModel()->getTable());
$columns = $grammar->columnize(array_keys(reset($values)));
$parameters = collect($values)->map(function ($record) use ($grammar) {
return sprintf('(%s)', $grammar->parameterize($record));
})->implode(', ');
$statement = "insert ignore into $table ($columns) values $parameters";
return $this->getBuilder()->getConnection()->statement($statement, $bindings);
}
/**
* Get the amount of entries in the database.
*
* @deprecated just use the count method off a model
*/
public function count(): int
{
return $this->getBuilder()->count();
}
}