forked from pterodactyl/panel
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRepository.php
More file actions
102 lines (86 loc) · 2.42 KB
/
Repository.php
File metadata and controls
102 lines (86 loc) · 2.42 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
<?php
namespace Pterodactyl\Repositories;
use Illuminate\Foundation\Application;
use Illuminate\Database\Eloquent\Model;
use Pterodactyl\Contracts\Repository\RepositoryInterface;
abstract class Repository implements RepositoryInterface
{
protected array $columns = ['*'];
protected Model $model;
protected bool $withFresh = true;
/**
* Repository constructor.
*/
public function __construct(protected Application $app)
{
$this->initializeModel($this->model());
}
/**
* Return the model backing this repository.
*/
abstract public function model(): string;
/**
* Return the model being used for this repository.
*/
public function getModel(): Model
{
return $this->model;
}
/**
* Setup column selection functionality.
*
* @param array|string $columns
*/
public function setColumns($columns = ['*']): self
{
$clone = clone $this;
$clone->columns = is_array($columns) ? $columns : func_get_args();
return $clone;
}
/**
* Return the columns to be selected in the repository call.
*/
public function getColumns(): array
{
return $this->columns;
}
/**
* Stop repository update functions from returning a fresh
* model when changes are committed.
*/
public function withoutFreshModel(): self
{
return $this->setFreshModel(false);
}
/**
* Return a fresh model with a repository updates a model.
*/
public function withFreshModel(): self
{
return $this->setFreshModel();
}
/**
* Set whether the repository should return a fresh model
* when changes are committed.
*/
public function setFreshModel(bool $fresh = true): self
{
$clone = clone $this;
$clone->withFresh = $fresh;
return $clone;
}
/**
* Take the provided model and make it accessible to the rest of the repository.
*/
protected function initializeModel(string ...$model): mixed
{
switch (count($model)) {
case 1:
return $this->model = $this->app->make($model[0]);
case 2:
return $this->model = call_user_func([$this->app->make($model[0]), $model[1]]);
default:
throw new \InvalidArgumentException('Model must be a FQDN or an array with a count of two.');
}
}
}