1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Backpack\Base\app\Models\Traits; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Str; |
6
|
|
|
use ReflectionClass; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* This trait helps a child model (ex: BackpackUser) inherit all relationships of its parent model (ex: User). |
10
|
|
|
* Laravel by default doesn't do that, so packages like Backpack\PermissionManager can't see relationships |
11
|
|
|
* on the BackpackUser model, because they haven't been inherited from User. |
12
|
|
|
* |
13
|
|
|
* The code below has been copy-pasted from https://github.com/tightenco/parental on Jul 19th 2019. |
14
|
|
|
*/ |
15
|
|
|
trait InheritsRelationsFromParentModel |
16
|
|
|
{ |
17
|
|
|
public $hasParent = true; |
18
|
|
|
|
19
|
|
|
public static function bootHasParent() |
20
|
|
|
{ |
21
|
|
|
static::creating(function ($model) { |
22
|
|
|
if ($model->parentHasHasChildrenTrait()) { |
23
|
|
|
$model->forceFill( |
24
|
|
|
[$model->getInheritanceColumn() => $model->classToAlias(get_class($model))] |
25
|
|
|
); |
26
|
|
|
} |
27
|
|
|
}); |
28
|
|
|
static::addGlobalScope(function ($query) { |
29
|
|
|
$instance = new static(); |
30
|
|
|
if ($instance->parentHasHasChildrenTrait()) { |
31
|
|
|
$query->where($instance->getTable().'.'.$instance->getInheritanceColumn(), $instance->classToAlias(get_class($instance))); |
|
|
|
|
32
|
|
|
} |
33
|
|
|
}); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function parentHasHasChildrenTrait() |
37
|
|
|
{ |
38
|
|
|
return $this->hasChildren ?? false; |
|
|
|
|
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function getTable() |
42
|
|
|
{ |
43
|
|
|
if (!isset($this->table)) { |
44
|
|
|
return str_replace('\\', '', Str::snake(Str::plural(class_basename($this->getParentClass())))); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
return $this->table; |
|
|
|
|
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function getForeignKey() |
51
|
|
|
{ |
52
|
|
|
return Str::snake(class_basename($this->getParentClass())).'_'.$this->primaryKey; |
|
|
|
|
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function joiningTable($related, $instance = null) |
|
|
|
|
56
|
|
|
{ |
57
|
|
|
$relatedClassName = method_exists((new $related()), 'getClassNameForRelationships') |
58
|
|
|
? (new $related())->getClassNameForRelationships() |
59
|
|
|
: class_basename($related); |
60
|
|
|
$models = [ |
61
|
|
|
Str::snake($relatedClassName), |
62
|
|
|
Str::snake($this->getClassNameForRelationships()), |
63
|
|
|
]; |
64
|
|
|
sort($models); |
65
|
|
|
|
66
|
|
|
return strtolower(implode('_', $models)); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function getClassNameForRelationships() |
70
|
|
|
{ |
71
|
|
|
return class_basename($this->getParentClass()); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
public function getMorphClass() |
75
|
|
|
{ |
76
|
|
|
if ($this->parentHasHasChildrenTrait()) { |
77
|
|
|
$parentClass = $this->getParentClass(); |
78
|
|
|
|
79
|
|
|
return (new $parentClass())->getMorphClass(); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
return parent::getMorphClass(); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
protected function getParentClass() |
86
|
|
|
{ |
87
|
|
|
static $parentClassName; |
88
|
|
|
|
89
|
|
|
return $parentClassName ?: $parentClassName = (new ReflectionClass($this))->getParentClass()->getName(); |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idable
provides a methodequalsId
that in turn relies on the methodgetId()
. If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()
as an abstract method to the trait will make sure it is available.