|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Iatstuti\Database\Support; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Model; |
|
6
|
|
|
use Illuminate\Database\Eloquent\Relations\Relation; |
|
7
|
|
|
use LogicException; |
|
8
|
|
|
|
|
9
|
|
|
trait CascadeSoftDeletes |
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* Boot the trait. |
|
13
|
|
|
* |
|
14
|
|
|
* Listen for the deleting event of a soft deleting model, and run |
|
15
|
|
|
* the delete operation for any configured relationship methods. |
|
16
|
|
|
* |
|
17
|
|
|
* @throws \LogicException |
|
18
|
|
|
*/ |
|
19
|
10 |
|
protected static function bootCascadeSoftDeletes() |
|
20
|
|
|
{ |
|
21
|
|
|
static::deleting(function ($model) { |
|
22
|
10 |
|
if (! $model->implementsSoftDeletes()) { |
|
23
|
1 |
|
throw new LogicException(sprintf( |
|
24
|
1 |
|
'%s does not implement Illuminate\Database\Eloquent\SoftDeletes', |
|
25
|
1 |
|
get_called_class() |
|
26
|
1 |
|
)); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
9 |
|
if ($invalidCascadingRelationships = $model->hasInvalidCascadingRelationships()) { |
|
30
|
3 |
|
throw new LogicException(sprintf( |
|
31
|
3 |
|
'%s [%s] must exist and return an object of type Illuminate\Database\Eloquent\Relations\Relation', |
|
32
|
3 |
|
str_plural('Relationship', count($invalidCascadingRelationships)), |
|
33
|
3 |
|
join(', ', $invalidCascadingRelationships) |
|
34
|
3 |
|
)); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
6 |
|
$delete = $model->forceDeleting ? 'forceDelete' : 'delete'; |
|
38
|
|
|
|
|
39
|
6 |
|
foreach ($model->getActiveCascadingDeletes() as $relationship) { |
|
40
|
6 |
|
if ($model->{$relationship} instanceof Model) { |
|
41
|
1 |
|
$model->{$relationship}->{$delete}(); |
|
42
|
1 |
|
} else { |
|
43
|
6 |
|
foreach ($model->{$relationship} as $child) { |
|
44
|
5 |
|
if ($child->pivot){ |
|
45
|
|
|
$child->pivot->delete(); |
|
46
|
|
|
} |
|
47
|
|
|
else{ |
|
48
|
5 |
|
$child->{$delete}(); |
|
49
|
|
|
} |
|
50
|
6 |
|
} |
|
51
|
|
|
} |
|
52
|
6 |
|
} |
|
53
|
9 |
|
}); |
|
54
|
7 |
|
} |
|
55
|
|
|
|
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Determine if the current model implements soft deletes. |
|
59
|
|
|
* |
|
60
|
|
|
* @return bool |
|
61
|
|
|
*/ |
|
62
|
10 |
|
protected function implementsSoftDeletes() |
|
63
|
|
|
{ |
|
64
|
10 |
|
return method_exists($this, 'runSoftDelete'); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Determine if the current model has any invalid cascading relationships defined. |
|
70
|
|
|
* |
|
71
|
|
|
* A relationship is considered invalid when the method does not exist, or the relationship |
|
72
|
|
|
* method does not return an instance of Illuminate\Database\Eloquent\Relations\Relation. |
|
73
|
|
|
* |
|
74
|
|
|
* @return array |
|
75
|
|
|
*/ |
|
76
|
9 |
|
protected function hasInvalidCascadingRelationships() |
|
77
|
|
|
{ |
|
78
|
|
|
return array_filter($this->getCascadingDeletes(), function ($relationship) { |
|
79
|
9 |
|
return ! method_exists($this, $relationship) || ! $this->{$relationship}() instanceof Relation; |
|
80
|
9 |
|
}); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Fetch the defined cascading soft deletes for this model. |
|
86
|
|
|
* |
|
87
|
|
|
* @return array |
|
88
|
|
|
*/ |
|
89
|
9 |
|
protected function getCascadingDeletes() |
|
90
|
|
|
{ |
|
91
|
9 |
|
return isset($this->cascadeDeletes) ? (array) $this->cascadeDeletes : []; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* For the cascading deletes defined on the model, return only those that are not null. |
|
97
|
|
|
* |
|
98
|
|
|
* @return array |
|
99
|
|
|
*/ |
|
100
|
|
|
protected function getActiveCascadingDeletes() |
|
101
|
|
|
{ |
|
102
|
6 |
|
return array_filter($this->getCascadingDeletes(), function ($relationship) { |
|
103
|
6 |
|
return ! is_null($this->{$relationship}); |
|
104
|
6 |
|
}); |
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
|