-
Notifications
You must be signed in to change notification settings - Fork 0
/
LogEventListener.php
201 lines (172 loc) · 6.58 KB
/
LogEventListener.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
<?php
/**
* Created by PhpStorm.
* User: kimwong
* Date: 26/8/2016
* Time: 上午10:21
*/
namespace App\BackBundle\EventListener;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Doctrine\ORM\Event\LifecycleEventArgs;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Doctrine\DBAL\DriverManager;
class LogEventListener
{
protected $container;
public function __construct(ContainerInterface $container , TokenStorageInterface $storage , ContainerInterface $containerInterface )
{
$this->container = $container;
$this->userStorage = $storage;
$this->entityCollection = array(
//YourBundle::class
);
$this->container = $containerInterface;
$this->config = new \Doctrine\DBAL\Configuration();
$this->connectionParams = array(
'dbname' => $this->container->getParameter('database_name'),
'user' => $this->container->getParameter('database_user'),
'password' => $this->container->getParameter('database_password'),
'host' => $this->container->getParameter('database_host'),
'driver' => $this->container->getParameter('database_driver'),
'charset' => 'UTF8' ,
);
}
private function isEntitySupported($entity){
foreach ( $this->entityCollection as $value) {
if ($entity instanceof $value) {
return true;
}
}
return false;
}
public function OperationLogging($message , $entity ){
if(!(is_null($message) || $message == '' )){
$conn = DriverManager::getConnection($this->connectionParams , $this->config);
$conn->beginTransaction();
$user = $this->userStorage->getToken();
$ip = $this->container->get('request')->getClientIp();
if ( !$user ) return;
try{
$conn->transactional ( function ($connection) use ($user , $message , $conn , $entity , $ip ) {
$date = new \DateTime('now');
$date = $date->format('Y-m-d H:i:s');
if ( ! $user->getUser() == "anon." ){
$OperatedBy = $user->getUsername();
}else{
$OperatedBy = "front_user";
$project = null;
}
$class_name = (new \ReflectionClass($entity))->getShortName();
$element = $class_name;
$event = $message;
$array = [
"event" => $event ,
"ip" => $ip,
"operatedBy" => $OperatedBy,
"operatedAt" => $date,
"element" => $element ,
];
$conn->insert('app_oplog', $array);
});
$conn->commit();
}catch (\Exception $e){
$conn->rollBack();
}
}
}
public function postUpdate(LifecycleEventArgs $eventArgs)
{
return $this->logChangeSet($eventArgs);
}
public function postPersist(LifecycleEventArgs $eventArgs)
{
$entity = $eventArgs->getEntity();
if ($this->isEntitySupported($entity)) {
$message = $this->getCreateLogMessage($entity);
$this->OperationLogging($message , $entity );
}
return $this->logChangeSet($eventArgs);
}
public function preRemove(LifecycleEventArgs $eventArgs)
{
$entity = $eventArgs->getEntity();
if ($this->isEntitySupported($entity)) {
$message = $this->getRemoveLogMessage($entity);
$this->OperationLogging($message , $entity);
}
}
/**
* Logs entity changeset
*
* @param LifecycleEventArgs $eventArgs
*/
public function logChangeSet(LifecycleEventArgs $eventArgs)
{
$em = $eventArgs->getEntityManager();
$uow = $em->getUnitOfWork();
$entity = $eventArgs->getEntity();
$classMetadata = $em->getClassMetadata(get_class($entity));
if ($this->isEntitySupported($entity)) {
$uow->computeChangeSet($classMetadata, $entity);
$changeSet = $uow->getEntityChangeSet($entity);
$message = $this->getUpdateLogMessage($changeSet, $entity);
foreach ($uow->getScheduledCollectionUpdates() as $key => $col) {
$oldRelations = $col->getValues();
foreach ($oldRelations as $relation) {
$message .= "\n" . sprintf(
'property "%s" owned relation "%s"',
(new \ReflectionClass($relation))->getShortName(),
$relation->getId()
);
}
}
$this->OperationLogging($message, $entity);
}
}
/**
* @return string some log informations
*/
public function getUpdateLogMessage(array $changeSets = [] , $entity )
{
$class_name = (new \ReflectionClass($entity))->getShortName();
$dirty = false;
$message = [];
$head = [];
foreach ($changeSets as $property => $changeSet) {
for ($i = 0, $s = sizeof($changeSet); $i < $s; $i++) {
if ($property === "lastLogin"){
return null;
}
if ($changeSet[$i] instanceof \DateTime) {
$changeSet[$i] = $changeSet[$i]->format("Y-m-d H:i:s");
}
}
if ($changeSet[0] != $changeSet[1]) {
$dirty = true;
$message[] = sprintf(
'property "%s" changed from "%s" to "%s"',
$property,
!is_array($changeSet[0]) ? $changeSet[0] : "an array",
!is_array($changeSet[1]) ? $changeSet[1] : "an array"
);
}
}
if($dirty) {
$head[] = sprintf('%s #%d :',
$class_name,
$entity->getId()
);
}
return implode("\n", array_merge($head, $message));
}
public function getCreateLogMessage($entity)
{
$class_name = (new \ReflectionClass($entity))->getShortName();
return sprintf('%s #%d created', $class_name, $entity->getId() );
}
public function getRemoveLogMessage($entity)
{
$class_name = (new \ReflectionClass($entity))->getShortName();
return sprintf('%s #%d removed', $class_name, $entity->getId() );
}
}