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