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\Traits\Macroable; |
9
|
|
|
use Spatie\Activitylog\Exceptions\CouldNotLogActivity; |
10
|
|
|
use Spatie\Activitylog\Models\Activity; |
11
|
|
|
|
12
|
|
|
class ActivityLogger |
13
|
|
|
{ |
14
|
|
|
use Macroable; |
15
|
|
|
|
16
|
|
|
/** @var \Illuminate\Auth\AuthManager */ |
17
|
|
|
protected $auth; |
18
|
|
|
|
19
|
|
|
protected $logName = ''; |
20
|
|
|
|
21
|
|
|
/** @var bool */ |
22
|
|
|
protected $logEnabled; |
23
|
|
|
|
24
|
|
|
/** @var \Illuminate\Database\Eloquent\Model */ |
25
|
|
|
protected $performedOn; |
26
|
|
|
|
27
|
|
|
/** @var \Illuminate\Database\Eloquent\Model */ |
28
|
|
|
protected $causedBy; |
29
|
|
|
|
30
|
|
|
/** @var \Illuminate\Support\Collection */ |
31
|
|
|
protected $properties; |
32
|
|
|
|
33
|
|
|
public function __construct(AuthManager $auth, Repository $config) |
34
|
|
|
{ |
35
|
|
|
$this->auth = $auth; |
36
|
|
|
|
37
|
|
|
$this->properties = collect(); |
38
|
|
|
|
39
|
|
|
$authDriver = $config['laravel-activitylog']['default_auth_driver'] ?? $auth->getDefaultDriver(); |
40
|
|
|
|
41
|
|
|
$this->causedBy = $auth->guard($authDriver)->user(); |
42
|
|
|
|
43
|
|
|
$this->logName = $config['laravel-activitylog']['default_log_name']; |
44
|
|
|
|
45
|
|
|
$this->logEnabled = $config['laravel-activitylog']['enabled'] ?? true; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function performedOn(Model $model) |
49
|
|
|
{ |
50
|
|
|
$this->performedOn = $model; |
51
|
|
|
|
52
|
|
|
return $this; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function on(Model $model) |
56
|
|
|
{ |
57
|
|
|
return $this->performedOn($model); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param \Illuminate\Database\Eloquent\Model|int|string $modelOrId |
62
|
|
|
* |
63
|
|
|
* @return $this |
64
|
|
|
*/ |
65
|
|
|
public function causedBy($modelOrId) |
66
|
|
|
{ |
67
|
|
|
$model = $this->normalizeCauser($modelOrId); |
68
|
|
|
|
69
|
|
|
$this->causedBy = $model; |
70
|
|
|
|
71
|
|
|
return $this; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
public function by($modelOrId) |
75
|
|
|
{ |
76
|
|
|
return $this->causedBy($modelOrId); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @param array|\Illuminate\Support\Collection $properties |
81
|
|
|
* |
82
|
|
|
* @return $this |
83
|
|
|
*/ |
84
|
|
|
public function withProperties($properties) |
85
|
|
|
{ |
86
|
|
|
$this->properties = collect($properties); |
87
|
|
|
|
88
|
|
|
return $this; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @param string $key |
93
|
|
|
* @param mixed $value |
94
|
|
|
* |
95
|
|
|
* @return $this |
96
|
|
|
*/ |
97
|
|
|
public function withProperty(string $key, $value) |
98
|
|
|
{ |
99
|
|
|
$this->properties->put($key, $value); |
100
|
|
|
|
101
|
|
|
return $this; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
public function useLog(string $logName) |
105
|
|
|
{ |
106
|
|
|
$this->logName = $logName; |
107
|
|
|
|
108
|
|
|
return $this; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
public function inLog(string $logName) |
112
|
|
|
{ |
113
|
|
|
return $this->useLog($logName); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @param string $description |
118
|
|
|
* |
119
|
|
|
* @return null|mixed |
120
|
|
|
*/ |
121
|
|
|
public function log(string $description) |
122
|
|
|
{ |
123
|
|
|
if (! $this->logEnabled) { |
124
|
|
|
return; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
$activity = ActivitylogServiceProvider::getActivityModelInstance(); |
128
|
|
|
|
129
|
|
|
if ($this->performedOn) { |
130
|
|
|
$activity->subject()->associate($this->performedOn); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
if ($this->causedBy) { |
134
|
|
|
$activity->causer()->associate($this->causedBy); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
$activity->properties = $this->properties; |
138
|
|
|
|
139
|
|
|
$activity->description = $this->replacePlaceholders($description, $activity); |
140
|
|
|
|
141
|
|
|
$activity->log_name = $this->logName; |
142
|
|
|
|
143
|
|
|
$activity->save(); |
144
|
|
|
|
145
|
|
|
return $activity; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* @param \Illuminate\Database\Eloquent\Model|int|string $modelOrId |
150
|
|
|
* |
151
|
|
|
* @throws \Spatie\Activitylog\Exceptions\CouldNotLogActivity |
152
|
|
|
* |
153
|
|
|
* @return \Illuminate\Database\Eloquent\Model |
154
|
|
|
*/ |
155
|
|
|
protected function normalizeCauser($modelOrId): Model |
156
|
|
|
{ |
157
|
|
|
if ($modelOrId instanceof Model) { |
158
|
|
|
return $modelOrId; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
if ($model = $this->auth->getProvider()->retrieveById($modelOrId)) { |
|
|
|
|
162
|
|
|
return $model; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
throw CouldNotLogActivity::couldNotDetermineUser($modelOrId); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
protected function replacePlaceholders(string $description, Activity $activity): string |
169
|
|
|
{ |
170
|
|
|
return preg_replace_callback('/:[a-z0-9._-]+/i', function ($match) use ($activity) { |
171
|
|
|
$match = $match[0]; |
172
|
|
|
|
173
|
|
|
$attribute = (string) string($match)->between(':', '.'); |
174
|
|
|
|
175
|
|
|
if (! in_array($attribute, ['subject', 'causer', 'properties'])) { |
176
|
|
|
return $match; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
$propertyName = substr($match, strpos($match, '.') + 1); |
180
|
|
|
|
181
|
|
|
$attributeValue = $activity->$attribute; |
182
|
|
|
|
183
|
|
|
$attributeValue = $attributeValue->toArray(); |
184
|
|
|
|
185
|
|
|
return array_get($attributeValue, $propertyName, $match); |
186
|
|
|
}, $description); |
187
|
|
|
} |
188
|
|
|
} |
189
|
|
|
|
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.