-
Notifications
You must be signed in to change notification settings - Fork 105
Expand file tree
/
Copy pathHasChildren.php
More file actions
267 lines (223 loc) · 7.04 KB
/
HasChildren.php
File metadata and controls
267 lines (223 loc) · 7.04 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
<?php
namespace Parental;
use Closure;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Support\Str;
trait HasChildren
{
/**
* @var bool
*/
protected static $parentBootMethods;
/**
* @var bool
*/
protected $hasChildren = true;
/**
* Register a model event with the dispatcher.
*
* @param string $event
* @param Closure|string $callback
* @return void
*/
protected static function registerModelEvent($event, $callback): void
{
parent::registerModelEvent($event, $callback);
$childTypes = (new self)->getChildTypes();
if (static::class === self::class && $childTypes !== []) {
// We don't want to register the callbacks that happen in the boot method of the parent, as they'll be called
// from the child's boot method as well.
if (! self::parentIsBooting()) {
foreach ($childTypes as $childClass) {
if ($childClass !== self::class) {
$childClass::registerModelEvent($event, $callback);
}
}
}
}
}
/**
* @return bool
*/
protected static function parentIsBooting(): bool
{
if (! isset(self::$parentBootMethods)) {
self::$parentBootMethods[] = 'boot';
foreach (class_uses_recursive(self::class) as $trait) {
self::$parentBootMethods[] = 'boot'.class_basename($trait);
}
self::$parentBootMethods = array_flip(self::$parentBootMethods);
}
foreach (debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS) as $trace) {
$class = isset($trace['class']) ? $trace['class'] : null;
$function = isset($trace['function']) ? $trace['function'] : '';
if ($class === self::class && isset(self::$parentBootMethods[$function])) {
return true;
}
}
return false;
}
/**
* Create a new instance of the given model.
*
* @param array $attributes
* @param bool $exists
* @return static
*/
public function newInstance($attributes = [], $exists = false): self
{
$model = isset($attributes[$this->getInheritanceColumn()])
? $this->getChildModel($attributes)
: new static(((array) $attributes));
$model->exists = $exists;
$model->setConnection(
$this->getConnectionName()
);
return $model;
}
/**
* Create a new model instance that is existing.
*
* @param array $attributes
* @param string|null $connection
* @return static
*/
public function newFromBuilder($attributes = [], $connection = null): self
{
$attributes = (array) $attributes;
$inheritanceAttributes = [];
$inheritanceColumn = $this->getInheritanceColumn();
if (isset($attributes[$inheritanceColumn])) {
$inheritanceAttributes[$inheritanceColumn] = $attributes[$inheritanceColumn];
}
$model = $this->newInstance($inheritanceAttributes, true);
$model->setRawAttributes($attributes, true);
$model->setConnection($connection ?: $this->getConnectionName());
$model->fireModelEvent('retrieved', false);
return $model;
}
/**
* Define an inverse one-to-one or many relationship.
*
* @param string $related
* @param string|null $foreignKey
* @param string|null $ownerKey
* @param string|null $relation
* @return BelongsTo
*/
public function belongsTo($related, $foreignKey = null, $ownerKey = null, $relation = null): BelongsTo
{
$instance = $this->newRelatedInstance($related);
if (is_null($foreignKey) && $instance->hasParent) {
$foreignKey = Str::snake($instance->getClassNameForHasChildrenRelationships()).'_'.$instance->getKeyName();
}
if (is_null($relation)) {
$relation = $this->guessBelongsToRelation();
}
return parent::belongsTo($related, $foreignKey, $ownerKey, $relation);
}
/**
* Define a one-to-many relationship.
*
* @param string $related
* @param string|null $foreignKey
* @param string|null $localKey
* @return HasMany
*/
public function hasMany($related, $foreignKey = null, $localKey = null): HasMany
{
return parent::hasMany($related, $foreignKey, $localKey);
}
/**
* Define a many-to-many relationship.
*
* @param string $related
* @param string|null $table
* @param string|null $foreignPivotKey
* @param string|null $relatedPivotKey
* @param string|null $parentKey
* @param string|null $relatedKey
* @param string|null $relation
* @return BelongsToMany
*/
public function belongsToMany(
$related, $table = null,
$foreignPivotKey = null,
$relatedPivotKey = null,
$parentKey = null,
$relatedKey = null,
$relation = null
): BelongsToMany {
$instance = $this->newRelatedInstance($related);
if (is_null($table) && $instance->hasParent) {
$table = $this->joiningTable($instance->getClassNameForHasChildrenRelationships());
}
return parent::belongsToMany(
$related,
$table,
$foreignPivotKey,
$relatedPivotKey,
$parentKey,
$relatedKey,
$relation,
);
}
/**
* @return string
*/
public function getClassNameForHasChildrenRelationships(): string
{
return class_basename($this);
}
/**
* @return string
*/
public function getInheritanceColumn(): string
{
return property_exists($this, 'childColumn') ? $this->childColumn : 'type';
}
/**
* @param array $attributes
* @return mixed
*/
protected function getChildModel(array $attributes)
{
$className = $this->classFromAlias(
$attributes[$this->getInheritanceColumn()]
);
return new $className((array) $attributes);
}
/**
* @param mixed $aliasOrClass
* @return string
*/
public function classFromAlias($aliasOrClass): string
{
$childTypes = $this->getChildTypes();
if (isset($childTypes[$aliasOrClass])) {
return $childTypes[$aliasOrClass];
}
return $aliasOrClass;
}
/**
* @param string $className
* @return string
*/
public function classToAlias(string $className): string
{
$childTypes = $this->getChildTypes();
if (in_array($className, $childTypes)) {
return array_search($className, $childTypes);
}
return $className;
}
/**
* @return array
*/
public function getChildTypes(): array
{
return property_exists($this, 'childTypes') ? $this->childTypes : [];
}
}