|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Spatie\Activitylog; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Auth\AuthManager; |
|
6
|
|
|
use Illuminate\Contracts\Config\Repository; |
|
7
|
|
|
use Illuminate\Database\Eloquent\Model; |
|
8
|
|
|
use Illuminate\Support\Arr; |
|
9
|
|
|
use Illuminate\Support\Carbon; |
|
10
|
|
|
use Illuminate\Support\Str; |
|
11
|
|
|
use Illuminate\Support\Traits\Macroable; |
|
12
|
|
|
use Spatie\Activitylog\Contracts\Activity as ActivityContract; |
|
13
|
|
|
use Spatie\Activitylog\Exceptions\CouldNotLogActivity; |
|
14
|
|
|
|
|
15
|
|
|
class ActivityLogger |
|
16
|
|
|
{ |
|
17
|
|
|
use Macroable; |
|
18
|
|
|
|
|
19
|
|
|
/** @var \Illuminate\Auth\AuthManager */ |
|
20
|
|
|
protected $auth; |
|
21
|
|
|
|
|
22
|
|
|
protected $defaultLogName = ''; |
|
23
|
|
|
|
|
24
|
|
|
/** @var string */ |
|
25
|
|
|
protected $authDriver; |
|
26
|
|
|
|
|
27
|
|
|
/** @var \Spatie\Activitylog\ActivityLogStatus */ |
|
28
|
|
|
protected $logStatus; |
|
29
|
|
|
|
|
30
|
|
|
/** @var \Spatie\Activitylog\Contracts\Activity */ |
|
31
|
|
|
protected $activity; |
|
32
|
340 |
|
|
|
33
|
|
|
public function __construct(AuthManager $auth, Repository $config, ActivityLogStatus $logStatus) |
|
34
|
340 |
|
{ |
|
35
|
|
|
$this->auth = $auth; |
|
36
|
340 |
|
|
|
37
|
|
|
$this->authDriver = $config['activitylog']['default_auth_driver'] ?? $auth->getDefaultDriver(); |
|
38
|
340 |
|
|
|
39
|
|
|
$this->defaultLogName = $config['activitylog']['default_log_name']; |
|
40
|
340 |
|
|
|
41
|
340 |
|
$this->logStatus = $logStatus; |
|
42
|
|
|
} |
|
43
|
132 |
|
|
|
44
|
|
|
public function setLogStatus(ActivityLogStatus $logStatus) |
|
45
|
132 |
|
{ |
|
46
|
|
|
$this->logStatus = $logStatus; |
|
47
|
132 |
|
|
|
48
|
|
|
return $this; |
|
49
|
|
|
} |
|
50
|
232 |
|
|
|
51
|
|
|
public function performedOn(Model $model) |
|
52
|
232 |
|
{ |
|
53
|
|
|
$this->getActivity()->subject()->associate($model); |
|
54
|
232 |
|
|
|
55
|
|
|
return $this; |
|
56
|
|
|
} |
|
57
|
16 |
|
|
|
58
|
|
|
public function on(Model $model) |
|
59
|
16 |
|
{ |
|
60
|
|
|
return $this->performedOn($model); |
|
61
|
|
|
} |
|
62
|
340 |
|
|
|
63
|
|
|
public function causedBy($modelOrId) |
|
64
|
340 |
|
{ |
|
65
|
332 |
|
if ($modelOrId === null) { |
|
66
|
|
|
return $this; |
|
67
|
|
|
} |
|
68
|
44 |
|
|
|
69
|
|
|
$model = $this->normalizeCauser($modelOrId); |
|
70
|
44 |
|
|
|
71
|
|
|
$this->getActivity()->causer()->associate($model); |
|
72
|
44 |
|
|
|
73
|
|
|
return $this; |
|
74
|
|
|
} |
|
75
|
20 |
|
|
|
76
|
|
|
public function by($modelOrId) |
|
77
|
20 |
|
{ |
|
78
|
|
|
return $this->causedBy($modelOrId); |
|
79
|
|
|
} |
|
80
|
8 |
|
|
|
81
|
|
|
public function causedByAnonymous() |
|
82
|
8 |
|
{ |
|
83
|
8 |
|
$this->activity->causer_id = null; |
|
|
|
|
|
|
84
|
|
|
$this->activity->causer_type = null; |
|
|
|
|
|
|
85
|
8 |
|
|
|
86
|
|
|
return $this; |
|
87
|
|
|
} |
|
88
|
4 |
|
|
|
89
|
|
|
public function byAnonymous() |
|
90
|
4 |
|
{ |
|
91
|
|
|
return $this->causedByAnonymous(); |
|
92
|
|
|
} |
|
93
|
340 |
|
|
|
94
|
|
|
public function event(string $event) |
|
95
|
340 |
|
{ |
|
96
|
|
|
return $this->setEvent($event); |
|
97
|
340 |
|
} |
|
98
|
|
|
|
|
99
|
|
|
public function setEvent(string $event) |
|
100
|
4 |
|
{ |
|
101
|
|
|
$this->activity->event = $event; |
|
|
|
|
|
|
102
|
4 |
|
|
|
103
|
|
|
return $this; |
|
104
|
4 |
|
} |
|
105
|
|
|
|
|
106
|
|
|
public function withProperties($properties) |
|
107
|
340 |
|
{ |
|
108
|
|
|
$this->getActivity()->properties = collect($properties); |
|
|
|
|
|
|
109
|
340 |
|
|
|
110
|
|
|
return $this; |
|
111
|
340 |
|
} |
|
112
|
|
|
|
|
113
|
|
|
public function withProperty(string $key, $value) |
|
114
|
|
|
{ |
|
115
|
|
|
$this->getActivity()->properties = $this->getActivity()->properties->put($key, $value); |
|
|
|
|
|
|
116
|
|
|
|
|
117
|
|
|
return $this; |
|
118
|
|
|
} |
|
119
|
12 |
|
|
|
120
|
|
|
public function createdAt(Carbon $dateTime) |
|
121
|
12 |
|
{ |
|
122
|
|
|
$this->getActivity()->created_at = $dateTime; |
|
|
|
|
|
|
123
|
12 |
|
|
|
124
|
|
|
return $this; |
|
125
|
|
|
} |
|
126
|
4 |
|
|
|
127
|
|
|
public function useLog(string $logName) |
|
128
|
4 |
|
{ |
|
129
|
|
|
$this->getActivity()->log_name = $logName; |
|
|
|
|
|
|
130
|
4 |
|
|
|
131
|
|
|
return $this; |
|
132
|
|
|
} |
|
133
|
4 |
|
|
|
134
|
|
|
public function inLog(string $logName) |
|
135
|
4 |
|
{ |
|
136
|
|
|
return $this->useLog($logName); |
|
137
|
4 |
|
} |
|
138
|
|
|
|
|
139
|
|
|
public function tap(callable $callback, string $eventName = null) |
|
140
|
340 |
|
{ |
|
141
|
|
|
call_user_func($callback, $this->getActivity(), $eventName); |
|
142
|
340 |
|
|
|
143
|
8 |
|
return $this; |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
332 |
|
public function enableLogging() |
|
147
|
|
|
{ |
|
148
|
332 |
|
$this->logStatus->enable(); |
|
149
|
332 |
|
|
|
150
|
83 |
|
return $this; |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
332 |
|
public function disableLogging() |
|
154
|
|
|
{ |
|
155
|
332 |
|
$this->logStatus->disable(); |
|
156
|
|
|
|
|
157
|
332 |
|
return $this; |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
44 |
|
public function log(string $description) |
|
161
|
|
|
{ |
|
162
|
44 |
|
if ($this->logStatus->disabled()) { |
|
163
|
40 |
|
return; |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
4 |
|
$activity = $this->activity; |
|
167
|
4 |
|
|
|
168
|
4 |
|
$activity->description = $this->replacePlaceholders( |
|
|
|
|
|
|
169
|
|
|
$activity->description ?? $description, |
|
|
|
|
|
|
170
|
4 |
|
$activity |
|
171
|
4 |
|
); |
|
172
|
|
|
|
|
173
|
|
|
$activity->save(); |
|
174
|
|
|
|
|
175
|
|
|
$this->activity = null; |
|
176
|
|
|
|
|
177
|
332 |
|
return $activity; |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
12 |
|
public function withoutLogs(callable $callback) |
|
181
|
|
|
{ |
|
182
|
12 |
|
if ($this->logStatus->disabled()) { |
|
183
|
|
|
return $callback(); |
|
184
|
12 |
|
} |
|
185
|
4 |
|
|
|
186
|
|
|
$this->logStatus->disable(); |
|
187
|
|
|
|
|
188
|
8 |
|
try { |
|
189
|
|
|
return $callback(); |
|
190
|
8 |
|
} finally { |
|
191
|
|
|
$this->logStatus->enable(); |
|
192
|
8 |
|
} |
|
193
|
4 |
|
} |
|
194
|
|
|
|
|
195
|
|
|
protected function normalizeCauser($modelOrId): Model |
|
196
|
4 |
|
{ |
|
197
|
|
|
if ($modelOrId instanceof Model) { |
|
198
|
4 |
|
return $modelOrId; |
|
199
|
332 |
|
} |
|
200
|
|
|
|
|
201
|
|
|
$guard = $this->auth->guard($this->authDriver); |
|
202
|
340 |
|
$provider = method_exists($guard, 'getProvider') ? $guard->getProvider() : null; |
|
203
|
|
|
$model = method_exists($provider, 'retrieveById') ? $provider->retrieveById($modelOrId) : null; |
|
204
|
340 |
|
|
|
205
|
340 |
|
if ($model instanceof Model) { |
|
206
|
|
|
return $model; |
|
207
|
340 |
|
} |
|
208
|
340 |
|
|
|
209
|
340 |
|
throw CouldNotLogActivity::couldNotDetermineUser($modelOrId); |
|
210
|
|
|
} |
|
211
|
|
|
|
|
212
|
340 |
|
protected function replacePlaceholders(string $description, ActivityContract $activity): string |
|
213
|
|
|
{ |
|
214
|
|
|
return preg_replace_callback('/:[a-z0-9._-]+/i', function ($match) use ($activity) { |
|
215
|
|
|
$match = $match[0]; |
|
216
|
|
|
|
|
217
|
|
|
$attribute = Str::before(Str::after($match, ':'), '.'); |
|
218
|
|
|
|
|
219
|
|
|
if (! in_array($attribute, ['subject', 'causer', 'properties'])) { |
|
220
|
|
|
return $match; |
|
221
|
|
|
} |
|
222
|
|
|
|
|
223
|
|
|
$propertyName = substr($match, strpos($match, '.') + 1); |
|
224
|
|
|
|
|
225
|
|
|
$attributeValue = $activity->$attribute; |
|
226
|
|
|
|
|
227
|
|
|
if (is_null($attributeValue)) { |
|
228
|
|
|
return $match; |
|
229
|
|
|
} |
|
230
|
|
|
|
|
231
|
|
|
$attributeValue = $attributeValue->toArray(); |
|
232
|
|
|
|
|
233
|
|
|
return Arr::get($attributeValue, $propertyName, $match); |
|
234
|
|
|
}, $description); |
|
235
|
|
|
} |
|
236
|
|
|
|
|
237
|
|
|
protected function getActivity(): ActivityContract |
|
238
|
|
|
{ |
|
239
|
|
|
if (! $this->activity instanceof ActivityContract) { |
|
240
|
|
|
$this->activity = ActivitylogServiceProvider::getActivityModelInstance(); |
|
241
|
|
|
$this |
|
242
|
|
|
->useLog($this->defaultLogName) |
|
243
|
|
|
->withProperties([]) |
|
244
|
|
|
->causedBy($this->auth->guard($this->authDriver)->user()); |
|
245
|
|
|
} |
|
246
|
|
|
|
|
247
|
|
|
return $this->activity; |
|
248
|
|
|
} |
|
249
|
|
|
} |
|
250
|
|
|
|
If you access a property on an interface, you most likely code against a concrete implementation of the interface.
Available Fixes
Adding an additional type check:
Changing the type hint: