1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Nimble\ElasticBundle\Doctrine\ORM; |
4
|
|
|
|
5
|
|
|
use Doctrine\Common\EventSubscriber; |
6
|
|
|
use Doctrine\ORM\Event\LifecycleEventArgs; |
7
|
|
|
use Doctrine\ORM\Event\PostFlushEventArgs; |
8
|
|
|
use Doctrine\ORM\Events; |
9
|
|
|
use Doctrine\ORM\Mapping\ClassMetadata; |
10
|
|
|
use Nimble\ElasticBundle\Synchronizer\SynchronizerManager; |
11
|
|
|
|
12
|
|
|
class LifecycleEventSubscriber implements EventSubscriber |
13
|
|
|
{ |
14
|
|
|
const ACTION_UPDATE = "UPDATE"; |
15
|
|
|
const ACTION_CREATE = "CREATE"; |
16
|
|
|
const ACTION_DELETE = "DELETE"; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var array |
20
|
|
|
*/ |
21
|
|
|
protected $buffer = []; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var array |
25
|
|
|
*/ |
26
|
|
|
protected $identifiers = []; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var SynchronizerManager |
30
|
|
|
*/ |
31
|
|
|
private $synchronizer; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param SynchronizerManager $synchronizer |
35
|
|
|
*/ |
36
|
|
|
public function __construct(SynchronizerManager $synchronizer) |
37
|
|
|
{ |
38
|
|
|
$this->synchronizer = $synchronizer; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* {@inheritdoc} |
43
|
|
|
*/ |
44
|
|
|
public function getSubscribedEvents() |
45
|
|
|
{ |
46
|
|
|
return [ |
47
|
|
|
Events::postUpdate, |
48
|
|
|
Events::preRemove, |
49
|
|
|
Events::postPersist, |
50
|
|
|
Events::postFlush |
51
|
|
|
]; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @param PostFlushEventArgs $args |
56
|
|
|
*/ |
57
|
|
|
public function postFlush(PostFlushEventArgs $args) |
|
|
|
|
58
|
|
|
{ |
59
|
|
|
/* Restore identifiers of entities being removed, because doctrine clears them. |
60
|
|
|
* This may have unintended consequences. |
61
|
|
|
* See http://www.doctrine-project.org/jira/browse/DDC-1680 |
62
|
|
|
* and https://groups.google.com/forum/#!topic/doctrine-user/bTukzq0QrSE (Pavel Horal's msg) */ |
63
|
|
|
foreach ($this->buffer as $event) { |
64
|
|
|
$this->restoreEntityIdentifier($event['entity']); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
$this->identifiers = []; |
68
|
|
|
|
69
|
|
|
foreach ($this->buffer as $event) { |
70
|
|
|
switch ($event['action']) { |
71
|
|
|
case self::ACTION_CREATE: |
72
|
|
|
$this->synchronizer->synchronizeCreate($event['entity']); |
73
|
|
|
break; |
74
|
|
|
|
75
|
|
|
case self::ACTION_UPDATE: |
76
|
|
|
$this->synchronizer->synchronizeUpdate($event['entity']); |
77
|
|
|
break; |
78
|
|
|
|
79
|
|
|
case self::ACTION_DELETE: |
80
|
|
|
$this->synchronizer->synchronizeDelete($event['entity']); |
81
|
|
|
break; |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
$this->buffer = []; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @param LifecycleEventArgs $args |
90
|
|
|
*/ |
91
|
|
|
public function postUpdate(LifecycleEventArgs $args) |
92
|
|
|
{ |
93
|
|
|
$this->buffer[] = [ |
94
|
|
|
'action' => self::ACTION_UPDATE, |
95
|
|
|
'entity' => $args->getEntity() |
96
|
|
|
]; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @param LifecycleEventArgs $args |
101
|
|
|
*/ |
102
|
|
|
public function postPersist(LifecycleEventArgs $args) |
103
|
|
|
{ |
104
|
|
|
$this->buffer[] = [ |
105
|
|
|
'action' => self::ACTION_CREATE, |
106
|
|
|
'entity' => $args->getEntity() |
107
|
|
|
]; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @param LifecycleEventArgs $args |
112
|
|
|
*/ |
113
|
|
|
public function preRemove(LifecycleEventArgs $args) |
114
|
|
|
{ |
115
|
|
|
$this->storeEntityIdentifier($args); |
116
|
|
|
|
117
|
|
|
$this->buffer[] = [ |
118
|
|
|
'action' => self::ACTION_DELETE, |
119
|
|
|
'entity' => $args->getEntity(), |
120
|
|
|
]; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* @param LifecycleEventArgs $args |
125
|
|
|
* @return array |
126
|
|
|
*/ |
127
|
|
|
protected function storeEntityIdentifier(LifecycleEventArgs $args) |
128
|
|
|
{ |
129
|
|
|
$em = $args->getEntityManager(); |
130
|
|
|
$entity = $args->getEntity(); |
131
|
|
|
|
132
|
|
|
$this->identifiers[spl_object_hash($entity)] = [ |
133
|
|
|
$em->getUnitOfWork()->getEntityIdentifier($entity), |
134
|
|
|
$em->getClassMetadata(get_class($entity)) |
135
|
|
|
]; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* @param object $entity |
140
|
|
|
*/ |
141
|
|
|
protected function restoreEntityIdentifier($entity) |
142
|
|
|
{ |
143
|
|
|
$hash = spl_object_hash($entity); |
144
|
|
|
|
145
|
|
|
if (!isset($this->identifiers[$hash])) { |
146
|
|
|
return; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* @var ClassMetadata $class |
151
|
|
|
* @var array $identifier |
152
|
|
|
*/ |
153
|
|
|
list($identifier, $class) = $this->identifiers[$hash]; |
154
|
|
|
|
155
|
|
|
$class->setIdentifierValues($entity, $identifier); |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.