|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Staudenmeir\LaravelAdjacencyList\IdeHelper; |
|
4
|
|
|
|
|
5
|
|
|
use Barryvdh\LaravelIdeHelper\Console\ModelsCommand; |
|
6
|
|
|
use Barryvdh\LaravelIdeHelper\Contracts\ModelHookInterface; |
|
7
|
|
|
use Illuminate\Database\Eloquent\Model; |
|
8
|
|
|
use Illuminate\Support\Str; |
|
9
|
|
|
use Staudenmeir\LaravelAdjacencyList\Eloquent\Collection as TreeCollection; |
|
10
|
|
|
use Staudenmeir\LaravelAdjacencyList\Eloquent\Graph\Collection as GraphCollection; |
|
11
|
|
|
use Staudenmeir\LaravelAdjacencyList\Eloquent\HasGraphRelationships; |
|
12
|
|
|
use Staudenmeir\LaravelAdjacencyList\Eloquent\HasRecursiveRelationships; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* @template RT { name: string, manyRelation: boolean, comment: string } |
|
16
|
|
|
*/ |
|
17
|
|
|
class RecursiveRelationsHook implements ModelHookInterface |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* @var array<array<RT>> |
|
21
|
|
|
*/ |
|
22
|
|
|
protected static array $treeRelationships = [ |
|
23
|
|
|
[ |
|
24
|
|
|
'name' => 'ancestors', |
|
25
|
|
|
'manyRelation' => true, |
|
26
|
|
|
'comment' => "The model's recursive parents.", |
|
27
|
|
|
], |
|
28
|
|
|
[ |
|
29
|
|
|
'name' => 'ancestorsAndSelf', |
|
30
|
|
|
'manyRelation' => true, |
|
31
|
|
|
'comment' => "The model's recursive parents and itself.", |
|
32
|
|
|
], |
|
33
|
|
|
[ |
|
34
|
|
|
'name' => 'bloodline', |
|
35
|
|
|
'manyRelation' => true, |
|
36
|
|
|
'comment' => "The model's ancestors, descendants and itself.", |
|
37
|
|
|
], |
|
38
|
|
|
[ |
|
39
|
|
|
'name' => 'children', |
|
40
|
|
|
'manyRelation' => true, |
|
41
|
|
|
'comment' => "The model's direct children.", |
|
42
|
|
|
], |
|
43
|
|
|
[ |
|
44
|
|
|
'name' => 'childrenAndSelf', |
|
45
|
|
|
'manyRelation' => true, |
|
46
|
|
|
'comment' => "The model's direct children and itself.", |
|
47
|
|
|
], |
|
48
|
|
|
[ |
|
49
|
|
|
'name' => 'descendants', |
|
50
|
|
|
'manyRelation' => true, |
|
51
|
|
|
'comment' => "The model's recursive children.", |
|
52
|
|
|
], |
|
53
|
|
|
[ |
|
54
|
|
|
'name' => 'descendantsAndSelf', |
|
55
|
|
|
'manyRelation' => true, |
|
56
|
|
|
'comment' => "The model's recursive children and itself.", |
|
57
|
|
|
], |
|
58
|
|
|
[ |
|
59
|
|
|
'name' => 'parent', |
|
60
|
|
|
'manyRelation' => false, |
|
61
|
|
|
'comment' => "The model's direct parent.", |
|
62
|
|
|
], |
|
63
|
|
|
[ |
|
64
|
|
|
'name' => 'parentAndSelf', |
|
65
|
|
|
'manyRelation' => true, |
|
66
|
|
|
'comment' => "The model's direct parent and itself.", |
|
67
|
|
|
], |
|
68
|
|
|
[ |
|
69
|
|
|
'name' => 'rootAncestor', |
|
70
|
|
|
'manyRelation' => false, |
|
71
|
|
|
'comment' => "The model's topmost parent.", |
|
72
|
|
|
], |
|
73
|
|
|
[ |
|
74
|
|
|
'name' => 'siblings', |
|
75
|
|
|
'manyRelation' => true, |
|
76
|
|
|
'comment' => "The parent's other children.", |
|
77
|
|
|
], |
|
78
|
|
|
[ |
|
79
|
|
|
'name' => 'siblingsAndSelf', |
|
80
|
|
|
'manyRelation' => true, |
|
81
|
|
|
'comment' => "All the parent's children.", |
|
82
|
|
|
] |
|
83
|
|
|
]; |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* @var array<array<RT>> |
|
87
|
|
|
*/ |
|
88
|
|
|
protected static array $graphRelationships = [ |
|
89
|
|
|
[ |
|
90
|
|
|
'name' => 'ancestors', |
|
91
|
|
|
'manyRelation' => true, |
|
92
|
|
|
'comment' => "The node's recursive parents.", |
|
93
|
|
|
], |
|
94
|
|
|
[ |
|
95
|
|
|
'name' => 'ancestorsAndSelf', |
|
96
|
|
|
'manyRelation' => true, |
|
97
|
|
|
'comment' => "The node's recursive parents and itself.", |
|
98
|
|
|
], |
|
99
|
|
|
[ |
|
100
|
|
|
'name' => 'children', |
|
101
|
|
|
'manyRelation' => true, |
|
102
|
|
|
'comment' => "The node's direct children.", |
|
103
|
|
|
], |
|
104
|
|
|
[ |
|
105
|
|
|
'name' => 'childrenAndSelf', |
|
106
|
|
|
'manyRelation' => true, |
|
107
|
|
|
'comment' => "The node's direct children and itself.", |
|
108
|
|
|
], |
|
109
|
|
|
[ |
|
110
|
|
|
'name' => 'descendants', |
|
111
|
|
|
'manyRelation' => true, |
|
112
|
|
|
'comment' => "The node's recursive children.", |
|
113
|
|
|
], |
|
114
|
|
|
[ |
|
115
|
|
|
'name' => 'descendantsAndSelf', |
|
116
|
|
|
'manyRelation' => true, |
|
117
|
|
|
'comment' => "The node's recursive children and itself.", |
|
118
|
|
|
], |
|
119
|
|
|
[ |
|
120
|
|
|
'name' => 'parents', |
|
121
|
|
|
'manyRelation' => true, |
|
122
|
|
|
'comment' => "The node's direct parents.", |
|
123
|
|
|
], |
|
124
|
|
|
[ |
|
125
|
|
|
'name' => 'parentsAndSelf', |
|
126
|
|
|
'manyRelation' => true, |
|
127
|
|
|
'comment' => "The node's direct parents and itself.", |
|
128
|
|
|
], |
|
129
|
|
|
]; |
|
130
|
|
|
|
|
131
|
12 |
|
public function run(ModelsCommand $command, Model $model): void |
|
132
|
|
|
{ |
|
133
|
12 |
|
$traits = class_uses_recursive($model); |
|
134
|
|
|
|
|
135
|
12 |
|
if (in_array(HasRecursiveRelationships::class, $traits)) { |
|
136
|
6 |
|
foreach (static::$treeRelationships as $relationship) { |
|
137
|
6 |
|
$type = $relationship['manyRelation'] |
|
138
|
6 |
|
? '\\' . TreeCollection::class . '|\\' . $model::class . '[]' |
|
139
|
6 |
|
: '\\' . $model::class; |
|
140
|
|
|
|
|
141
|
6 |
|
$this->addRelationship($command, $relationship, $type); |
|
142
|
|
|
} |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
12 |
|
if (in_array(HasGraphRelationships::class, $traits)) { |
|
146
|
6 |
|
foreach (static::$graphRelationships as $relationship) { |
|
147
|
6 |
|
$type = '\\' . GraphCollection::class . '|\\' . $model::class . '[]'; |
|
148
|
|
|
|
|
149
|
6 |
|
$this->addRelationship($command, $relationship, $type); |
|
150
|
|
|
} |
|
151
|
|
|
} |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
12 |
|
protected function addRelationship(ModelsCommand $command, array $relationship, string $type): void |
|
155
|
|
|
{ |
|
156
|
12 |
|
$command->setProperty( |
|
157
|
12 |
|
$relationship['name'], |
|
158
|
12 |
|
$type, |
|
159
|
12 |
|
true, |
|
160
|
12 |
|
false, |
|
161
|
12 |
|
$relationship['comment'], |
|
162
|
12 |
|
!$relationship['manyRelation'] |
|
163
|
12 |
|
); |
|
164
|
|
|
|
|
165
|
12 |
|
if ($relationship['manyRelation']) { |
|
166
|
12 |
|
$command->setProperty( |
|
167
|
12 |
|
Str::snake($relationship['name']) . '_count', |
|
168
|
12 |
|
'int', |
|
169
|
12 |
|
true, |
|
170
|
12 |
|
false, |
|
171
|
12 |
|
null, |
|
172
|
12 |
|
true |
|
173
|
12 |
|
); |
|
174
|
|
|
} |
|
175
|
|
|
} |
|
176
|
|
|
} |
|
177
|
|
|
|