1
|
|
|
<?php namespace Sofa\Revisionable\Laravel; |
2
|
|
|
|
3
|
|
|
use Sofa\Revisionable\Logger; |
4
|
|
|
use Illuminate\Database\ConnectionInterface; |
5
|
|
|
use DateTime; |
6
|
|
|
|
7
|
|
|
class DbLogger implements Logger |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* Custom database connection. |
11
|
|
|
* |
12
|
|
|
* @var \Illuminate\Database\ConnectionInterface |
13
|
|
|
*/ |
14
|
|
|
protected $connection; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Default database connection. |
18
|
|
|
* |
19
|
|
|
* @var \Illuminate\Database\ConnectionInterface |
20
|
|
|
*/ |
21
|
|
|
protected $defaultConnection; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Revisions table name. |
25
|
|
|
* |
26
|
|
|
* @var string |
27
|
|
|
*/ |
28
|
|
|
protected $table; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Create a new DbLogger. |
32
|
|
|
* |
33
|
|
|
* @param \Illuminate\Database\ConnectionInterface $connection |
34
|
|
|
* @param string $table |
35
|
|
|
*/ |
36
|
|
|
public function __construct(ConnectionInterface $connection, $table) |
37
|
|
|
{ |
38
|
|
|
$this->defaultConnection = $connection; |
39
|
|
|
$this->table = $table; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Log data revisions in the db. |
44
|
|
|
* |
45
|
|
|
* @param string $action |
46
|
|
|
* @param string $table |
47
|
|
|
* @param int $id |
48
|
|
|
* @param array $old |
49
|
|
|
* @param array $new |
50
|
|
|
* @param string $user |
51
|
|
|
* @return void |
52
|
|
|
*/ |
53
|
|
|
public function revisionLog($action, $table, $id, array $old = [], array $new = [], $user = null) |
54
|
|
|
{ |
55
|
|
|
$user = $this->parseUser($user); |
56
|
|
|
|
57
|
|
|
$connection = $this->getCurrentConnection(); |
58
|
|
|
|
59
|
|
|
$format = $connection->getQueryGrammar()->getDateFormat(); |
60
|
|
|
|
61
|
|
|
$connection->table($this->table)->insert([ |
62
|
|
|
'action' => substr($action, 0, 255), |
63
|
|
|
'table_name' => substr($table, 0, 255), |
64
|
|
|
'row_id' => substr($id, 0, 255), |
65
|
|
|
'old' => json_encode($old), |
66
|
|
|
'new' => json_encode($new), |
67
|
|
|
'user' => substr($user, 0, 255), |
68
|
|
|
'ip' => substr($this->getFromServer('REMOTE_ADDR'), 0, 255) ?: null, |
69
|
|
|
'ip_forwarded' => substr($this->getFromServer('HTTP_X_FORWARDED_FOR'), 0, 255) ?: null, |
70
|
|
|
'created_at' => (new DateTime)->format($format), |
71
|
|
|
]); |
72
|
|
|
|
73
|
|
|
$this->resetConnection(); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Set custom connection for the next log. |
78
|
|
|
* |
79
|
|
|
* @param \Illuminate\Database\ConnectionInterface $connection |
80
|
|
|
* @return static |
81
|
|
|
*/ |
82
|
|
|
public function on(ConnectionInterface $connection) |
83
|
|
|
{ |
84
|
|
|
$this->connection = $connection; |
85
|
|
|
|
86
|
|
|
return $this; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Translate provided user to appropriate string. |
91
|
|
|
* |
92
|
|
|
* @param mixed $user |
93
|
|
|
* @return string |
94
|
|
|
*/ |
95
|
|
|
protected function parseUser($user) |
96
|
|
|
{ |
97
|
|
|
return (is_string($user) || is_numeric($user)) ? $user : ' -- '; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Return current connection instance to use for next log. |
102
|
|
|
* |
103
|
|
|
* @return \Illuminate\Database\ConnectionInterface |
104
|
|
|
*/ |
105
|
|
|
protected function getCurrentConnection() |
106
|
|
|
{ |
107
|
|
|
return ($this->connection) ?: $this->defaultConnection; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Reset custom connection. |
112
|
|
|
* |
113
|
|
|
* @return void |
114
|
|
|
*/ |
115
|
|
|
protected function resetConnection() |
116
|
|
|
{ |
117
|
|
|
$this->connection = null; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Get Server variable. |
122
|
|
|
* |
123
|
|
|
* @param string $key |
124
|
|
|
* @param mixed $default |
125
|
|
|
* @return string|array |
126
|
|
|
*/ |
127
|
|
|
protected function getFromServer($key, $default = null) |
128
|
|
|
{ |
129
|
|
|
return (isset($_SERVER[$key])) ? $_SERVER[$key] : $default; |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|