1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the XiideaEasyAuditBundle package. |
5
|
|
|
* |
6
|
|
|
* (c) Xiidea <http://www.xiidea.net> |
7
|
|
|
* |
8
|
|
|
* This source file is subject to the MIT license that is bundled |
9
|
|
|
* with this source code in the file LICENSE. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Xiidea\EasyAuditBundle\Resolver; |
13
|
|
|
|
14
|
|
|
use Doctrine\Persistence\ManagerRegistry; |
15
|
|
|
use Doctrine\Common\Util\ClassUtils; |
16
|
|
|
use Symfony\Contracts\EventDispatcher\Event; |
17
|
|
|
use Xiidea\EasyAuditBundle\Events\DoctrineObjectEvent; |
18
|
|
|
use Xiidea\EasyAuditBundle\Events\DoctrineEvents; |
19
|
|
|
|
20
|
|
|
/** Custom Event Resolver Example Class */ |
21
|
|
|
class DoctrineObjectEventResolver implements EventResolverInterface |
22
|
|
|
{ |
23
|
|
|
protected $eventShortName; |
24
|
|
|
|
25
|
|
|
/** @var $event DoctrineObjectEvent */ |
|
|
|
|
26
|
|
|
protected $event; |
27
|
|
|
|
28
|
|
|
protected $entity; |
29
|
|
|
|
30
|
|
|
protected $eventName; |
31
|
|
|
|
32
|
|
|
protected $identity = ['', '']; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var ManagerRegistry |
36
|
|
|
*/ |
37
|
|
|
protected $doctrine; |
38
|
|
|
|
39
|
|
|
protected $changeSetGetterMethods = [ |
40
|
|
|
'getEntityChangeSet', |
41
|
|
|
'getDocumentChangeSet', |
42
|
|
|
]; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @param Event|DoctrineObjectEvent $event |
46
|
|
|
* @param $eventName |
47
|
|
|
* |
48
|
|
|
* @return array |
49
|
|
|
* |
50
|
|
|
* @throws \ReflectionException |
51
|
|
|
*/ |
52
|
|
|
#[\Override] |
53
|
|
|
public function getEventLogInfo(Event $event, $eventName) |
54
|
|
|
{ |
55
|
|
|
if (!$event instanceof DoctrineObjectEvent) { |
56
|
|
|
return null; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
$this->initialize($event, $eventName); |
60
|
|
|
|
61
|
|
|
if ($this->isUpdateEvent() && null === $this->getChangeSets($this->entity)) { |
62
|
|
|
return null; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
$reflectionClass = $this->getReflectionClassFromObject($this->entity); |
66
|
|
|
|
67
|
|
|
return array( |
68
|
|
|
'description' => $this->getDescription($reflectionClass->getShortName()), |
69
|
|
|
'type' => $this->getEventType($reflectionClass->getShortName()), |
70
|
|
|
); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
protected function getSingleIdentity() |
74
|
|
|
{ |
75
|
|
|
foreach ($this->event->getIdentity() as $field => $value) { |
76
|
|
|
return [$field, $value]; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
return ['', '']; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @param DoctrineObjectEvent $event |
84
|
|
|
* @param string $eventName |
85
|
|
|
*/ |
86
|
|
|
private function initialize(DoctrineObjectEvent $event, $eventName) |
87
|
|
|
{ |
88
|
|
|
$this->eventShortName = null; |
89
|
|
|
$this->eventName = $eventName; |
90
|
|
|
$this->event = $event; |
91
|
|
|
$this->entity = $event->getLifecycleEventArgs()->getObject(); |
92
|
|
|
$this->identity = $this->getSingleIdentity(); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
private function getIdField() |
96
|
|
|
{ |
97
|
|
|
return $this->identity[0]; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
private function getIdValue() |
101
|
|
|
{ |
102
|
|
|
return $this->identity[1]; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
protected function getChangeSets($entity) |
106
|
|
|
{ |
107
|
|
|
$unitOfWork = $this->getUnitOfWork(); |
108
|
|
|
foreach ($this->changeSetGetterMethods as $method) { |
109
|
|
|
$getter = [$unitOfWork, $method]; |
110
|
|
|
if (is_callable($getter)) { |
111
|
|
|
return call_user_func($getter, $entity); |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
return null; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
protected function isUpdateEvent() |
119
|
|
|
{ |
120
|
|
|
return 'updated' === $this->getEventShortName(); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* @param string $typeName |
125
|
|
|
* |
126
|
|
|
* @return string |
127
|
|
|
*/ |
128
|
|
|
protected function getEventType($typeName) |
129
|
|
|
{ |
130
|
|
|
return $typeName.' '.$this->getEventShortName(); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* @param string $shortName |
135
|
|
|
* |
136
|
|
|
* @return string |
137
|
|
|
*/ |
138
|
|
|
protected function getDescription($shortName) |
139
|
|
|
{ |
140
|
|
|
return sprintf( |
141
|
|
|
'%s has been %s with %s = "%s"', |
142
|
|
|
$shortName, |
143
|
|
|
$this->getEventShortName(), |
144
|
|
|
$this->getIdField(), |
145
|
|
|
$this->getIdValue() |
146
|
|
|
); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* @return string |
151
|
|
|
*/ |
152
|
|
|
protected function getEventShortName() |
153
|
|
|
{ |
154
|
|
|
if (null === $this->eventShortName) { |
155
|
|
|
$this->eventShortName = DoctrineEvents::getShortEventType($this->getName()); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
return $this->eventShortName; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* @return string |
163
|
|
|
*/ |
164
|
|
|
protected function getName() |
165
|
|
|
{ |
166
|
|
|
return $this->eventName; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* @param $object |
171
|
|
|
* |
172
|
|
|
* @return \ReflectionClass |
173
|
|
|
* |
174
|
|
|
* @throws \ReflectionException |
175
|
|
|
*/ |
176
|
|
|
protected function getReflectionClassFromObject($object) |
177
|
|
|
{ |
178
|
|
|
return new \ReflectionClass(ClassUtils::getClass($object)); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* @return \Doctrine\ODM\MongoDB\UnitOfWork|\Doctrine\ORM\UnitOfWork |
|
|
|
|
183
|
|
|
*/ |
184
|
|
|
protected function getUnitOfWork() |
185
|
|
|
{ |
186
|
|
|
return $this->getDoctrine()->getManager()->getUnitOfWork(); |
|
|
|
|
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* @return ManagerRegistry|object |
191
|
|
|
*/ |
192
|
|
|
protected function getDoctrine() |
193
|
|
|
{ |
194
|
|
|
return $this->doctrine; |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* @param ManagerRegistry $doctrine |
199
|
|
|
*/ |
200
|
|
|
public function setDoctrine($doctrine) |
201
|
|
|
{ |
202
|
|
|
$this->doctrine = $doctrine; |
203
|
|
|
} |
204
|
|
|
} |
205
|
|
|
|