1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by PhpStorm. |
4
|
|
|
* User: Lai Vu |
5
|
|
|
* Date: 10/24/2016 |
6
|
|
|
* Time: 4:14 PM |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace LaiVu\ActivityLog; |
10
|
|
|
|
11
|
|
|
|
12
|
|
|
use Illuminate\Auth\AuthManager; |
13
|
|
|
use Illuminate\Database\Eloquent\Model; |
14
|
|
|
use Illuminate\Support\Traits\Macroable; |
15
|
|
|
use LaiVu\ActivityLog\Exceptions\InvalidConfiguration; |
16
|
|
|
use LaiVu\ActivityLog\Handlers\ActivityLogHandlerInterface; |
17
|
|
|
use LaiVu\ActivityLog\Models\Activity; |
18
|
|
|
|
19
|
|
|
class ActivityLogger |
20
|
|
|
{ |
21
|
|
|
use Macroable; |
22
|
|
|
|
23
|
|
|
/** @var \Illuminate\Auth\AuthManager */ |
24
|
|
|
protected $auth; |
25
|
|
|
|
26
|
|
|
/** @var ActivityLogHandlerInterface[] */ |
27
|
|
|
protected $logHandlers = []; |
28
|
|
|
|
29
|
|
|
protected $logName = ''; |
30
|
|
|
/** @var bool */ |
31
|
|
|
protected $logEnabled; |
32
|
|
|
/** @var \Illuminate\Database\Eloquent\Model */ |
33
|
|
|
protected $performedOn; |
34
|
|
|
/** @var \Illuminate\Database\Eloquent\Model */ |
35
|
|
|
protected $causedBy; |
36
|
|
|
/** @var \Illuminate\Support\Collection */ |
37
|
|
|
protected $properties; |
38
|
|
|
|
39
|
|
|
public function __construct(AuthManager $auth) |
40
|
|
|
{ |
41
|
|
|
$this->auth = $auth; |
42
|
|
|
$this->properties = collect(); |
43
|
|
|
$authDriver = config('activitylog.default_auth_driver', $auth->getDefaultDriver()); |
44
|
|
|
$this->causedBy = $auth->guard($authDriver)->user(); |
45
|
|
|
$this->logName = config('activitylog.default_log_name'); |
46
|
|
|
$this->logEnabled = config('activitylog.enabled', true); |
47
|
|
|
|
48
|
|
|
$handlers = config('activitylog.default', ['log']); |
49
|
|
|
|
50
|
|
|
foreach ($handlers as $handler) { |
51
|
|
|
$handlerObject = $this->determineHandler($handler); |
52
|
|
|
$this->logHandlers[] = $handlerObject; |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function performedOn(Model $model) |
57
|
|
|
{ |
58
|
|
|
$this->performedOn = $model; |
59
|
|
|
return $this; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function on(Model $model) |
63
|
|
|
{ |
64
|
|
|
return $this->performedOn($model); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function causedBy(Model $model) |
68
|
|
|
{ |
69
|
|
|
$this->causedBy = $model; |
70
|
|
|
return $this; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function by($modelOrId) |
74
|
|
|
{ |
75
|
|
|
return $this->causedBy($modelOrId); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @param array|\Illuminate\Support\Collection $properties |
80
|
|
|
* |
81
|
|
|
* @return $this |
82
|
|
|
*/ |
83
|
|
|
public function withProperties($properties) |
84
|
|
|
{ |
85
|
|
|
$this->properties = collect($properties); |
86
|
|
|
return $this; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @param string $key |
91
|
|
|
* @param mixed $value |
92
|
|
|
* |
93
|
|
|
* @return $this |
94
|
|
|
*/ |
95
|
|
|
public function withProperty($key, $value) |
96
|
|
|
{ |
97
|
|
|
$this->properties->put($key, $value); |
98
|
|
|
return $this; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
public function useLog($logName) |
102
|
|
|
{ |
103
|
|
|
$this->logName = $logName; |
104
|
|
|
return $this; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
public function inLog($logName) |
108
|
|
|
{ |
109
|
|
|
return $this->useLog($logName); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
public function log($description) |
113
|
|
|
{ |
114
|
|
|
if (!$this->logEnabled) { |
115
|
|
|
return; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
foreach ($this->logHandlers as $handler) { |
119
|
|
|
$description = $this->replacePlaceholders($description); |
120
|
|
|
$handler->log($this->performedOn, $this->causedBy, $this->properties, $this->logName, $description); |
|
|
|
|
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @param $handler |
126
|
|
|
* @return ActivityLogHandlerInterface |
127
|
|
|
* @throws InvalidConfiguration |
128
|
|
|
*/ |
129
|
|
|
private function determineHandler($handler) |
130
|
|
|
{ |
131
|
|
|
$handlerConfig = config("activitylog.handlers.$handler"); |
132
|
|
|
if (!$handlerConfig) { |
133
|
|
|
throw InvalidConfiguration::handlerNotFound($handler); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
$handlerDriver = $handlerConfig['driver']; |
137
|
|
|
if (!is_a($handlerDriver, ActivityLogHandlerInterface::class, true)) { |
138
|
|
|
throw InvalidConfiguration::handlerTypeNotValid($handlerDriver); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
return new $handlerDriver(); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Clean out old entries in the log. |
146
|
|
|
* |
147
|
|
|
* @return bool |
148
|
|
|
*/ |
149
|
|
|
public function cleanLog() |
150
|
|
|
{ |
151
|
|
|
$maxAgeInDays = config('activitylog.delete_records_older_than_days'); |
152
|
|
|
|
153
|
|
|
if(is_null($maxAgeInDays) || $maxAgeInDays ==''){ |
154
|
|
|
return false; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
foreach ($this->logHandlers as $logHandler) { |
158
|
|
|
$logHandler->cleanLog($maxAgeInDays); |
159
|
|
|
} |
160
|
|
|
return true; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
protected function replacePlaceholders($description) |
164
|
|
|
{ |
165
|
|
|
return preg_replace_callback('/:[a-z0-9._-]+/i', function ($match) { |
166
|
|
|
$match = $match[0]; |
167
|
|
|
$attribute =$this->getBetween($match,':', '.'); |
168
|
|
|
if (! in_array($attribute, ['performedOn', 'causedBy', 'properties'])) { |
169
|
|
|
return $match; |
170
|
|
|
} |
171
|
|
|
$propertyName = substr($match, strpos($match, '.') + 1); |
172
|
|
|
return isset($this->{$attribute})&&isset($this->{$attribute}->{$propertyName})?$this->{$attribute}->{$propertyName}:$match; |
173
|
|
|
}, $description); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
function getBetween($content,$start,$end){ |
177
|
|
|
$r = explode($start, $content); |
178
|
|
|
if (isset($r[1])){ |
179
|
|
|
$r = explode($end, $r[1]); |
180
|
|
|
return $r[0]; |
181
|
|
|
} |
182
|
|
|
return ''; |
183
|
|
|
} |
184
|
|
|
} |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: