1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of the Laravel Platfourm package. |
4
|
|
|
* |
5
|
|
|
* (c) Avtandil Kikabidze aka LONGMAN <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Longman\Platfourm\Database\Eloquent\Traits; |
12
|
|
|
|
13
|
|
|
use Illuminate\Database\Eloquent\Builder; |
14
|
|
|
use Illuminate\Database\Eloquent\Model; |
15
|
|
|
use Illuminate\Database\Eloquent\Scope; |
16
|
|
|
use Longman\Platfourm\Contracts\Auth\AuthUserService; |
17
|
|
|
|
18
|
|
|
class SoftDeletingScope implements Scope |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* All of the extensions to be added to the builder. |
22
|
|
|
* |
23
|
|
|
* @var array |
24
|
|
|
*/ |
25
|
|
|
protected $extensions = ['ForceDelete', 'Restore', 'WithTrashed', 'OnlyTrashed']; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Apply the scope to a given Eloquent query builder. |
29
|
|
|
* |
30
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $builder |
31
|
|
|
* @param \Illuminate\Database\Eloquent\Model $model |
32
|
|
|
* @return void |
33
|
|
|
*/ |
34
|
|
|
public function apply(Builder $builder, Model $model) |
35
|
|
|
{ |
36
|
|
|
$builder->whereNull($model->getQualifiedDeletedAtColumn()); |
|
|
|
|
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Extend the query builder with the needed functions. |
41
|
|
|
* |
42
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $builder |
43
|
|
|
* @return void |
44
|
|
|
*/ |
45
|
|
|
public function extend(Builder $builder) |
46
|
|
|
{ |
47
|
|
|
foreach ($this->extensions as $extension) { |
48
|
|
|
$this->{"add{$extension}"}($builder); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
$builder->onDelete(function (Builder $builder) { |
52
|
|
|
|
53
|
|
|
$update = [ |
54
|
|
|
$this->getDeletedAtColumn($builder) => $builder->getModel()->freshTimestampString(), |
55
|
|
|
]; |
56
|
|
|
|
57
|
|
|
$userService = app()->make(AuthUserService::class); |
58
|
|
|
if ($userService->check()) { |
59
|
|
|
$update[$this->getDeletedByColumn($builder)] = $userService->user()->id; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
return $builder->update($update); |
63
|
|
|
}); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Get the "deleted at" column for the builder. |
68
|
|
|
* |
69
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $builder |
70
|
|
|
* @return string |
71
|
|
|
*/ |
72
|
|
|
protected function getDeletedAtColumn(Builder $builder) |
73
|
|
|
{ |
74
|
|
|
if (count($builder->getQuery()->joins) > 0) { |
75
|
|
|
return $builder->getModel()->getQualifiedDeletedAtColumn(); |
|
|
|
|
76
|
|
|
} else { |
77
|
|
|
return $builder->getModel()->getDeletedAtColumn(); |
|
|
|
|
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Get the "deleted by" column for the builder. |
83
|
|
|
* |
84
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $builder |
85
|
|
|
* @return string |
86
|
|
|
*/ |
87
|
|
|
protected function getDeletedByColumn(Builder $builder) |
88
|
|
|
{ |
89
|
|
|
if (count($builder->getQuery()->joins) > 0) { |
90
|
|
|
return $builder->getModel()->getQualifiedDeletedByColumn(); |
|
|
|
|
91
|
|
|
} else { |
92
|
|
|
return $builder->getModel()->getDeletedByColumn(); |
|
|
|
|
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Add the force delete extension to the builder. |
98
|
|
|
* |
99
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $builder |
100
|
|
|
* @return void |
101
|
|
|
*/ |
102
|
|
|
protected function addForceDelete(Builder $builder) |
103
|
|
|
{ |
104
|
|
|
$builder->macro('forceDelete', function (Builder $builder) { |
105
|
|
|
return $builder->getQuery()->delete(); |
106
|
|
|
}); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Add the restore extension to the builder. |
111
|
|
|
* |
112
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $builder |
113
|
|
|
* @return void |
114
|
|
|
*/ |
115
|
|
|
protected function addRestore(Builder $builder) |
116
|
|
|
{ |
117
|
|
|
$builder->macro('restore', function (Builder $builder) { |
118
|
|
|
$builder->withTrashed(); |
119
|
|
|
|
120
|
|
|
return $builder->update([$builder->getModel()->getDeletedAtColumn() => null]); |
|
|
|
|
121
|
|
|
}); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Add the with-trashed extension to the builder. |
126
|
|
|
* |
127
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $builder |
128
|
|
|
* @return void |
129
|
|
|
*/ |
130
|
|
|
protected function addWithTrashed(Builder $builder) |
131
|
|
|
{ |
132
|
|
|
$builder->macro('withTrashed', function (Builder $builder) { |
133
|
|
|
return $builder->withoutGlobalScope($this); |
134
|
|
|
}); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Add the only-trashed extension to the builder. |
139
|
|
|
* |
140
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $builder |
141
|
|
|
* @return void |
142
|
|
|
*/ |
143
|
|
|
protected function addOnlyTrashed(Builder $builder) |
144
|
|
|
{ |
145
|
|
|
$builder->macro('onlyTrashed', function (Builder $builder) { |
146
|
|
|
$model = $builder->getModel(); |
147
|
|
|
|
148
|
|
|
$builder->withoutGlobalScope($this)->whereNotNull( |
149
|
|
|
$model->getQualifiedDeletedAtColumn() |
|
|
|
|
150
|
|
|
); |
151
|
|
|
|
152
|
|
|
return $builder; |
153
|
|
|
}); |
154
|
|
|
} |
155
|
|
|
} |
156
|
|
|
|
This check marks calls to methods that do not seem to exist on an object.
This is most likely the result of a method being renamed without all references to it being renamed likewise.