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