1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Iatstuti\Database\Support; |
4
|
|
|
|
5
|
|
|
use LogicException; |
6
|
|
|
use Illuminate\Support\Str; |
7
|
|
|
use Illuminate\Database\Eloquent\Model; |
8
|
|
|
use Illuminate\Database\Eloquent\Relations\BelongsToMany; |
9
|
|
|
use Illuminate\Database\Eloquent\Relations\Relation; |
10
|
|
|
|
11
|
|
|
trait CascadeSoftDeletes |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* Boot the trait. |
15
|
|
|
* |
16
|
|
|
* Listen for the deleting event of a soft deleting model, and run |
17
|
|
|
* the delete operation for any configured relationship methods. |
18
|
|
|
* |
19
|
10 |
|
* @throws \LogicException |
20
|
|
|
*/ |
21
|
|
|
protected static function bootCascadeSoftDeletes() |
22
|
10 |
|
{ |
23
|
1 |
|
static::deleting(function ($model) { |
24
|
1 |
|
$model->validateCascadingSoftDelete(); |
25
|
1 |
|
|
26
|
1 |
|
$model->runCascadingDeletes(); |
27
|
|
|
}); |
28
|
|
|
} |
29
|
9 |
|
|
30
|
3 |
|
|
31
|
3 |
|
/** |
32
|
3 |
|
* Validate that the calling model is correctly setup for cascading soft deletes. |
33
|
3 |
|
* |
34
|
3 |
|
* @throws \Iatstuti\Database\Support\CascadeSoftDeleteException |
35
|
|
|
*/ |
36
|
|
|
protected function validateCascadingSoftDelete() |
37
|
6 |
|
{ |
38
|
|
|
if (! $this->implementsSoftDeletes()) { |
39
|
6 |
|
throw CascadeSoftDeleteException::softDeleteNotImplemented(get_called_class()); |
40
|
6 |
|
} |
41
|
1 |
|
|
42
|
1 |
|
if ($invalidCascadingRelationships = $this->hasInvalidCascadingRelationships()) { |
43
|
6 |
|
throw CascadeSoftDeleteException::invalidRelationships($invalidCascadingRelationships); |
44
|
5 |
|
} |
45
|
6 |
|
} |
46
|
|
|
|
47
|
6 |
|
|
48
|
9 |
|
/** |
49
|
7 |
|
* Run the cascading soft delete for this model. |
50
|
|
|
* |
51
|
|
|
* @return void |
52
|
|
|
*/ |
53
|
|
|
protected function runCascadingDeletes() |
54
|
|
|
{ |
55
|
|
|
foreach ($this->getActiveCascadingDeletes() as $relationship) { |
56
|
|
|
$this->cascadeSoftDeletes($relationship); |
57
|
10 |
|
} |
58
|
|
|
} |
59
|
10 |
|
|
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Cascade delete the given relationship on the given mode. |
63
|
|
|
* |
64
|
|
|
* @param string $relationship |
65
|
|
|
* @return return |
|
|
|
|
66
|
|
|
*/ |
67
|
|
|
protected function cascadeSoftDeletes($relationship) |
68
|
|
|
{ |
69
|
|
|
$delete = $this->forceDeleting ? 'forceDelete' : 'delete'; |
|
|
|
|
70
|
|
|
|
71
|
9 |
|
foreach ($this->{$relationship}()->get() as $model) { |
72
|
|
|
$model->pivot ? $model->pivot->{$delete}() : $model->{$delete}(); |
73
|
|
|
} |
74
|
9 |
|
} |
75
|
9 |
|
|
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Determine if the current model implements soft deletes. |
79
|
|
|
* |
80
|
|
|
* @return bool |
81
|
|
|
*/ |
82
|
|
|
protected function implementsSoftDeletes() |
83
|
|
|
{ |
84
|
9 |
|
return method_exists($this, 'runSoftDelete'); |
85
|
|
|
} |
86
|
9 |
|
|
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Determine if the current model has any invalid cascading relationships defined. |
90
|
|
|
* |
91
|
|
|
* A relationship is considered invalid when the method does not exist, or the relationship |
92
|
|
|
* method does not return an instance of Illuminate\Database\Eloquent\Relations\Relation. |
93
|
|
|
* |
94
|
|
|
* @return array |
95
|
|
|
*/ |
96
|
|
|
protected function hasInvalidCascadingRelationships() |
97
|
6 |
|
{ |
98
|
6 |
|
return array_filter($this->getCascadingDeletes(), function ($relationship) { |
99
|
6 |
|
return ! method_exists($this, $relationship) || ! $this->{$relationship}() instanceof Relation; |
100
|
|
|
}); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Fetch the defined cascading soft deletes for this model. |
106
|
|
|
* |
107
|
|
|
* @return array |
108
|
|
|
*/ |
109
|
|
|
protected function getCascadingDeletes() |
110
|
|
|
{ |
111
|
|
|
return isset($this->cascadeDeletes) ? (array) $this->cascadeDeletes : []; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* For the cascading deletes defined on the model, return only those that are not null. |
117
|
|
|
* |
118
|
|
|
* @return array |
119
|
|
|
*/ |
120
|
|
|
protected function getActiveCascadingDeletes() |
121
|
|
|
{ |
122
|
|
|
return array_filter($this->getCascadingDeletes(), function ($relationship) { |
123
|
|
|
return ! is_null($this->{$relationship}); |
124
|
|
|
}); |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|
This check compares the return type specified in the
@return
annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.