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
137 lines (119 loc) · 2.9 KB
/
Repository.php
File metadata and controls
137 lines (119 loc) · 2.9 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
<?php
namespace Pterodactyl\Repositories;
use InvalidArgumentException;
use Illuminate\Foundation\Application;
use Pterodactyl\Contracts\Repository\RepositoryInterface;
abstract class Repository implements RepositoryInterface
{
/**
* @var \Illuminate\Foundation\Application
*/
protected $app;
/**
* @var array
*/
protected $columns = ['*'];
/**
* @var mixed
*/
protected $model;
/**
* @var bool
*/
protected $withFresh = true;
/**
* Repository constructor.
*
* @param \Illuminate\Foundation\Application $application
*/
public function __construct(Application $application)
{
$this->app = $application;
$this->initializeModel($this->model());
}
/**
* Return the model backing this repository.
*
* @return string|\Closure|object
*/
abstract public function model();
/**
* Return the model being used for this repository.
*
* @return mixed
*/
public function getModel()
{
return $this->model;
}
/**
* Setup column selection functionality.
*
* @param array|string $columns
* @return $this
*/
public function setColumns($columns = ['*'])
{
$clone = clone $this;
$clone->columns = is_array($columns) ? $columns : func_get_args();
return $clone;
}
/**
* Return the columns to be selected in the repository call.
*
* @return array
*/
public function getColumns()
{
return $this->columns;
}
/**
* Stop repository update functions from returning a fresh
* model when changes are committed.
*
* @return $this
*/
public function withoutFreshModel()
{
return $this->setFreshModel(false);
}
/**
* Return a fresh model with a repository updates a model.
*
* @return $this
*/
public function withFreshModel()
{
return $this->setFreshModel(true);
}
/**
* Set whether or not the repository should return a fresh model
* when changes are committed.
*
* @param bool $fresh
* @return $this
*/
public function setFreshModel(bool $fresh = true)
{
$clone = clone $this;
$clone->withFresh = $fresh;
return $clone;
}
/**
* Take the provided model and make it accessible to the rest of the repository.
*
* @param array $model
* @return mixed
*/
protected function initializeModel(...$model)
{
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.');
}
}
}