1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace RichanFongdasen\EloquentBlameable; |
4
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Builder; |
6
|
|
|
use Illuminate\Database\Eloquent\Model; |
7
|
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo; |
8
|
|
|
use Illuminate\Database\Eloquent\SoftDeletes; |
9
|
|
|
|
10
|
|
|
trait BlameableTrait |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* Get any of override 'blameable attributes'. |
14
|
|
|
* |
15
|
|
|
* @return array |
16
|
|
|
*/ |
17
|
|
|
public function blameable(): array |
18
|
|
|
{ |
19
|
|
|
if (property_exists($this, 'blameable')) { |
20
|
|
|
return (array) static::$blameable; |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
return []; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Boot the Blameable service by attaching |
28
|
|
|
* a new observer into the current model object. |
29
|
|
|
* |
30
|
|
|
* @return void |
31
|
|
|
*/ |
32
|
|
|
public static function bootBlameableTrait(): void |
33
|
|
|
{ |
34
|
|
|
static::observe(app(BlameableObserver::class)); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Build blameable query scope. |
39
|
|
|
* |
40
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $query |
41
|
|
|
* @param mixed $userId |
42
|
|
|
* @param string $key |
43
|
|
|
* |
44
|
|
|
* @return \Illuminate\Database\Eloquent\Builder |
45
|
|
|
*/ |
46
|
|
|
private function buildBlameableScope(Builder $query, $userId, $key): Builder |
47
|
|
|
{ |
48
|
|
|
if ($userId instanceof Model) { |
49
|
|
|
$userId = $userId->getKey(); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
return $query->where($this->getTable().'.'.app(BlameableService::class)->getConfiguration($this, $key), $userId); |
|
|
|
|
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Get the user who created the record. |
57
|
|
|
* |
58
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
59
|
|
|
*/ |
60
|
|
|
public function creator(): BelongsTo |
61
|
|
|
{ |
62
|
|
|
return $this->belongsTo( |
63
|
|
|
app(BlameableService::class)->getConfiguration($this, 'user'), |
|
|
|
|
64
|
|
|
app(BlameableService::class)->getConfiguration($this, 'createdBy') |
65
|
|
|
); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Get the user who updated the record for the last time. |
70
|
|
|
* |
71
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
72
|
|
|
*/ |
73
|
|
|
public function updater(): BelongsTo |
74
|
|
|
{ |
75
|
|
|
return $this->belongsTo( |
76
|
|
|
app(BlameableService::class)->getConfiguration($this, 'user'), |
|
|
|
|
77
|
|
|
app(BlameableService::class)->getConfiguration($this, 'updatedBy') |
78
|
|
|
); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* createdBy Query Scope. |
83
|
|
|
* |
84
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $query |
85
|
|
|
* @param mixed $userId |
86
|
|
|
* |
87
|
|
|
* @return \Illuminate\Database\Eloquent\Builder |
88
|
|
|
*/ |
89
|
|
|
public function scopeCreatedBy(Builder $query, $userId): Builder |
90
|
|
|
{ |
91
|
|
|
return $this->buildBlameableScope($query, $userId, 'createdBy'); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* updatedBy Query Scope. |
96
|
|
|
* |
97
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $query |
98
|
|
|
* @param mixed $userId |
99
|
|
|
* |
100
|
|
|
* @return \Illuminate\Database\Eloquent\Builder |
101
|
|
|
*/ |
102
|
|
|
public function scopeUpdatedBy(Builder $query, $userId): Builder |
103
|
|
|
{ |
104
|
|
|
return $this->buildBlameableScope($query, $userId, 'updatedBy'); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Silently update the model without firing any |
109
|
|
|
* events. |
110
|
|
|
* |
111
|
|
|
* @return int |
112
|
|
|
*/ |
113
|
|
|
public function silentUpdate(): int |
114
|
|
|
{ |
115
|
|
|
return $this->newQueryWithoutScopes() |
116
|
|
|
->where($this->getKeyName(), $this->getKey()) |
|
|
|
|
117
|
|
|
->getQuery() |
|
|
|
|
118
|
|
|
->update($this->getDirty()); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Confirm if the current model uses SoftDeletes. |
123
|
|
|
* |
124
|
|
|
* @return bool |
125
|
|
|
*/ |
126
|
|
|
public function useSoftDeletes(): bool |
127
|
|
|
{ |
128
|
|
|
return in_array(SoftDeletes::class, class_uses_recursive($this), true); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Define an inverse one-to-one or many relationship. |
133
|
|
|
* |
134
|
|
|
* @param string $related |
135
|
|
|
* @param string|null $foreignKey |
136
|
|
|
* @param string|null $otherKey |
137
|
|
|
* @param string|null $relation |
138
|
|
|
* |
139
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
140
|
|
|
*/ |
141
|
|
|
abstract public function belongsTo($related, $foreignKey = null, $otherKey = null, $relation = null); |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Get the attributes that have been changed since last sync. |
145
|
|
|
* |
146
|
|
|
* @return array |
147
|
|
|
*/ |
148
|
|
|
abstract public function getDirty(); |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Get the primary key for the model. |
152
|
|
|
* |
153
|
|
|
* @return string |
154
|
|
|
*/ |
155
|
|
|
abstract public function getKeyName(); |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Get the value of the model's primary key. |
159
|
|
|
* |
160
|
|
|
* @return mixed |
161
|
|
|
*/ |
162
|
|
|
abstract public function getKey(); |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* Get a new query builder that doesn't have any global scopes. |
166
|
|
|
* |
167
|
|
|
* @return \Illuminate\Database\Eloquent\Builder|static |
168
|
|
|
*/ |
169
|
|
|
abstract public function newQueryWithoutScopes(); |
170
|
|
|
} |
171
|
|
|
|