1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Staudenmeir\LaravelAdjacencyList; |
4
|
|
|
|
5
|
|
|
use Barryvdh\LaravelIdeHelper\Console\ModelsCommand; |
6
|
|
|
use Barryvdh\LaravelIdeHelper\Contracts\ModelHookInterface; |
7
|
|
|
use Illuminate\Database\Eloquent\Model; |
8
|
|
|
use Staudenmeir\LaravelAdjacencyList\Eloquent\Collection; |
9
|
|
|
use Staudenmeir\LaravelAdjacencyList\Eloquent\HasRecursiveRelationships; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* @template RT { name: string, manyRelation: boolean, comment: string, relationClass: class-string|null } |
13
|
|
|
*/ |
14
|
|
|
class RecursiveRelationsHook implements ModelHookInterface |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var array<array<RT>> |
18
|
|
|
*/ |
19
|
|
|
private static array $relationMap = [ |
20
|
|
|
[ |
21
|
|
|
'name' => 'ancestors', |
22
|
|
|
'manyRelation' => true, |
23
|
|
|
'comment' => 'The model\'s recursive parents.' |
24
|
|
|
], |
25
|
|
|
[ |
26
|
|
|
'name' => 'ancestorsAndSelf', |
27
|
|
|
'manyRelation' => true, |
28
|
|
|
'comment' => 'The model\'s recursive parents and itself.' |
29
|
|
|
], |
30
|
|
|
[ |
31
|
|
|
'name' => 'bloodline', |
32
|
|
|
'manyRelation' => true, |
33
|
|
|
'comment' => 'The model\'s ancestors, descendants and itself.' |
34
|
|
|
], |
35
|
|
|
[ |
36
|
|
|
'name' => 'children', |
37
|
|
|
'manyRelation' => true, |
38
|
|
|
'comment' => 'The model\'s direct children.' |
39
|
|
|
], |
40
|
|
|
[ |
41
|
|
|
'name' => 'childrenAndSelf', |
42
|
|
|
'manyRelation' => true, |
43
|
|
|
'comment' => 'The model\'s direct children and itself.' |
44
|
|
|
], |
45
|
|
|
[ |
46
|
|
|
'name' => 'descendants', |
47
|
|
|
'manyRelation' => true, |
48
|
|
|
'comment' => 'The model\'s recursive children.' |
49
|
|
|
], |
50
|
|
|
[ |
51
|
|
|
'name' => 'descendantsAndSelf', |
52
|
|
|
'manyRelation' => true, |
53
|
|
|
'comment' => 'The model\'s recursive children and itself.' |
54
|
|
|
], |
55
|
|
|
[ |
56
|
|
|
'name' => 'parent', |
57
|
|
|
'manyRelation' => false, |
58
|
|
|
'comment' => 'The model\'s direct parent.' |
59
|
|
|
], |
60
|
|
|
[ |
61
|
|
|
'name' => 'parentAndSelf', |
62
|
|
|
'manyRelation' => true, |
63
|
|
|
'comment' => 'The model\'s direct parent and itself.' |
64
|
|
|
], |
65
|
|
|
[ |
66
|
|
|
'name' => 'rootAncestor', |
67
|
|
|
'manyRelation' => false, |
68
|
|
|
'comment' => 'The model\'s topmost parent.' |
69
|
|
|
], |
70
|
|
|
[ |
71
|
|
|
'name' => 'siblings', |
72
|
|
|
'manyRelation' => true, |
73
|
|
|
'comment' => 'The parent\'s other children.' |
74
|
|
|
], |
75
|
|
|
[ |
76
|
|
|
'name' => 'siblingsAndSelf', |
77
|
|
|
'manyRelation' => true, |
78
|
|
|
'comment' => 'All the parent\'s children.' |
79
|
|
|
] |
80
|
|
|
]; |
81
|
|
|
|
82
|
|
|
public function run(ModelsCommand $command, Model $model): void |
83
|
|
|
{ |
84
|
|
|
$traits = collect(class_uses_recursive($model)); |
|
|
|
|
85
|
|
|
|
86
|
|
|
if ($traits->doesntContain(HasRecursiveRelationships::class)) { |
87
|
|
|
return; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
foreach (self::$relationMap as $relationDefinition) { |
91
|
|
|
$type = $relationDefinition['manyRelation'] |
92
|
|
|
? '\\' . Collection::class . '|' . class_basename($model) . '[]' |
93
|
|
|
: class_basename($model); |
94
|
|
|
|
95
|
|
|
$command->setProperty( |
96
|
|
|
$relationDefinition['name'], |
97
|
|
|
$type, |
98
|
|
|
true, |
99
|
|
|
false, |
100
|
|
|
$relationDefinition['comment'], |
101
|
|
|
!$relationDefinition['manyRelation'] |
102
|
|
|
); |
103
|
|
|
|
104
|
|
|
if ($relationDefinition['manyRelation']) { |
105
|
|
|
$command->setProperty( |
106
|
|
|
$relationDefinition['name'] . '_count', |
107
|
|
|
'int', |
108
|
|
|
true, |
109
|
|
|
false, |
110
|
|
|
null, |
111
|
|
|
true |
112
|
|
|
); |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|