Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pterodactyl
GitHub Repository: pterodactyl/panel
Path: blob/1.0-develop/app/Repositories/Repository.php
7432 views
1
<?php
2
3
namespace Pterodactyl\Repositories;
4
5
use Illuminate\Foundation\Application;
6
use Illuminate\Database\Eloquent\Model;
7
use Pterodactyl\Contracts\Repository\RepositoryInterface;
8
9
abstract class Repository implements RepositoryInterface
10
{
11
protected array $columns = ['*'];
12
13
protected Model $model;
14
15
protected bool $withFresh = true;
16
17
/**
18
* Repository constructor.
19
*/
20
public function __construct(protected Application $app)
21
{
22
$this->initializeModel($this->model());
23
}
24
25
/**
26
* Return the model backing this repository.
27
*/
28
abstract public function model(): string;
29
30
/**
31
* Return the model being used for this repository.
32
*/
33
public function getModel(): Model
34
{
35
return $this->model;
36
}
37
38
/**
39
* Setup column selection functionality.
40
*
41
* @param array|string $columns
42
*/
43
public function setColumns($columns = ['*']): self
44
{
45
$clone = clone $this;
46
$clone->columns = is_array($columns) ? $columns : func_get_args();
47
48
return $clone;
49
}
50
51
/**
52
* Return the columns to be selected in the repository call.
53
*/
54
public function getColumns(): array
55
{
56
return $this->columns;
57
}
58
59
/**
60
* Stop repository update functions from returning a fresh
61
* model when changes are committed.
62
*/
63
public function withoutFreshModel(): self
64
{
65
return $this->setFreshModel(false);
66
}
67
68
/**
69
* Return a fresh model with a repository updates a model.
70
*/
71
public function withFreshModel(): self
72
{
73
return $this->setFreshModel();
74
}
75
76
/**
77
* Set whether the repository should return a fresh model
78
* when changes are committed.
79
*/
80
public function setFreshModel(bool $fresh = true): self
81
{
82
$clone = clone $this;
83
$clone->withFresh = $fresh;
84
85
return $clone;
86
}
87
88
/**
89
* Take the provided model and make it accessible to the rest of the repository.
90
*/
91
protected function initializeModel(string ...$model): mixed
92
{
93
switch (count($model)) {
94
case 1:
95
return $this->model = $this->app->make($model[0]);
96
case 2:
97
return $this->model = call_user_func([$this->app->make($model[0]), $model[1]]);
98
default:
99
throw new \InvalidArgumentException('Model must be a FQDN or an array with a count of two.');
100
}
101
}
102
}
103
104