/
AbstractModel.php
266 lines (222 loc) · 6.25 KB
/
AbstractModel.php
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
<?php
/*
* This file is part of Flarum.
*
* For detailed copyright and license information, please view the
* LICENSE file that was distributed with this source code.
*/
namespace Flarum\Database;
use Flarum\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model as Eloquent;
use Illuminate\Database\Eloquent\Relations\Relation;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
use LogicException;
/**
* Base model class, building on Eloquent.
*
* Adds the ability for custom relations to be added to a model during runtime.
* These relations behave in the same way that you would expect; they can be
* queried, eager loaded, and accessed as an attribute.
*
* @property-read int|null $id
*/
abstract class AbstractModel extends Eloquent
{
/**
* Indicates if the model should be timestamped. Turn off by default.
*
* @var bool
*/
public $timestamps = false;
/**
* An array of callbacks to be run once after the model is saved.
*
* @var callable[]
*/
protected $afterSaveCallbacks = [];
/**
* An array of callbacks to be run once after the model is deleted.
*
* @var callable[]
*/
protected $afterDeleteCallbacks = [];
/**
* @internal
*/
public static $customRelations = [];
/**
* @internal
*/
public static $customCasts = [];
/**
* @internal
*/
public static $defaults = [];
/**
* An alias for the table name, used in queries.
*
* @var string|null
* @internal
*/
protected $tableAlias = null;
/**
* {@inheritdoc}
*/
public static function boot()
{
parent::boot();
static::saved(function (self $model) {
foreach ($model->releaseAfterSaveCallbacks() as $callback) {
$callback($model);
}
});
static::deleted(function (self $model) {
foreach ($model->releaseAfterDeleteCallbacks() as $callback) {
$callback($model);
}
});
}
/**
* {@inheritdoc}
*/
public function __construct(array $attributes = [])
{
$this->attributes = [];
foreach (array_merge(array_reverse(class_parents($this)), [static::class]) as $class) {
$this->attributes = array_merge($this->attributes, Arr::get(static::$defaults, $class, []));
}
$this->attributes = array_map(function ($item) {
return is_callable($item) ? $item($this) : $item;
}, $this->attributes);
parent::__construct($attributes);
}
/**
* {@inheritdoc}
*/
public function getCasts()
{
$casts = parent::getCasts();
foreach (array_merge(array_reverse(class_parents($this)), [static::class]) as $class) {
$casts = array_merge($casts, Arr::get(static::$customCasts, $class, []));
}
return $casts;
}
/**
* Get an attribute from the model. If nothing is found, attempt to load
* a custom relation method with this key.
*
* @param string $key
* @return mixed
*/
public function getAttribute($key)
{
if (! is_null($value = parent::getAttribute($key))) {
return $value;
}
// If a custom relation with this key has been set up, then we will load
// and return results from the query and hydrate the relationship's
// value on the "relationships" array.
if (! $this->relationLoaded($key) && ($relation = $this->getCustomRelation($key))) {
if (! $relation instanceof Relation) {
throw new LogicException(
'Relationship method must return an object of type '.Relation::class
);
}
return $this->relations[$key] = $relation->getResults();
}
}
/**
* Get a custom relation object.
*
* @param string $name
* @return mixed
*/
protected function getCustomRelation($name)
{
foreach (array_merge([static::class], class_parents($this)) as $class) {
$relation = Arr::get(static::$customRelations, $class.".$name", null);
if (! is_null($relation)) {
return $relation($this);
}
}
}
/**
* Register a callback to be run once after the model is saved.
*
* @param callable $callback
* @return void
*/
public function afterSave($callback)
{
$this->afterSaveCallbacks[] = $callback;
}
/**
* Register a callback to be run once after the model is deleted.
*
* @param callable $callback
* @return void
*/
public function afterDelete($callback)
{
$this->afterDeleteCallbacks[] = $callback;
}
/**
* @return callable[]
*/
public function releaseAfterSaveCallbacks()
{
$callbacks = $this->afterSaveCallbacks;
$this->afterSaveCallbacks = [];
return $callbacks;
}
/**
* @return callable[]
*/
public function releaseAfterDeleteCallbacks()
{
$callbacks = $this->afterDeleteCallbacks;
$this->afterDeleteCallbacks = [];
return $callbacks;
}
/**
* {@inheritdoc}
*/
public function __call($method, $arguments)
{
if ($relation = $this->getCustomRelation($method)) {
return $relation;
}
return parent::__call($method, $arguments);
}
public function newModelQuery()
{
$query = parent::newModelQuery();
if ($this->tableAlias) {
$query->from($this->getTable().' as '.$this->tableAlias);
}
return $query;
}
public function qualifyColumn($column)
{
if (Str::contains($column, '.')) {
return $column;
}
return ($this->tableAlias ?? $this->getTable()).'.'.$column;
}
public function withTableAlias(callable $callback)
{
static $aliasCount = 0;
$this->tableAlias = 'flarum_reserved_'.++$aliasCount;
$result = $callback();
$this->tableAlias = null;
return $result;
}
/**
* @param \Illuminate\Support\Collection|array $models
*/
public function newCollection($models = [])
{
return new Collection($models);
}
}