1 | <?php |
||
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) |
||
122 | |||
123 | /** |
||
124 | * @param LifecycleEventArgs $args |
||
125 | * @return array |
||
126 | */ |
||
127 | protected function storeEntityIdentifier(LifecycleEventArgs $args) |
||
137 | |||
138 | /** |
||
139 | * @param object $entity |
||
140 | */ |
||
141 | protected function restoreEntityIdentifier($entity) |
||
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.