1 | <?php |
||
26 | class Listener |
||
27 | { |
||
28 | /** |
||
29 | * Object persister. |
||
30 | * |
||
31 | * @var ObjectPersisterInterface |
||
32 | */ |
||
33 | protected $objectPersister; |
||
34 | |||
35 | /** |
||
36 | * Configuration for the listener. |
||
37 | * |
||
38 | * @var array |
||
39 | */ |
||
40 | private $config; |
||
41 | |||
42 | /** |
||
43 | * Objects scheduled for insertion. |
||
44 | * |
||
45 | * @var array |
||
46 | */ |
||
47 | public $scheduledForInsertion = []; |
||
48 | |||
49 | /** |
||
50 | * Objects scheduled to be updated or removed. |
||
51 | * |
||
52 | * @var array |
||
53 | */ |
||
54 | public $scheduledForUpdate = []; |
||
55 | |||
56 | /** |
||
57 | * IDs of objects scheduled for removal. |
||
58 | * |
||
59 | * @var array |
||
60 | */ |
||
61 | public $scheduledForDeletion = []; |
||
62 | |||
63 | /** |
||
64 | * PropertyAccessor instance. |
||
65 | * |
||
66 | * @var PropertyAccessorInterface |
||
67 | */ |
||
68 | protected $propertyAccessor; |
||
69 | |||
70 | /** |
||
71 | * @var IndexableInterface |
||
72 | */ |
||
73 | private $indexable; |
||
74 | |||
75 | /** |
||
76 | * Constructor. |
||
77 | * |
||
78 | * @param ObjectPersisterInterface $objectPersister |
||
79 | * @param IndexableInterface $indexable |
||
80 | * @param array $config |
||
81 | * @param LoggerInterface $logger |
||
82 | */ |
||
83 | 17 | public function __construct( |
|
84 | ObjectPersisterInterface $objectPersister, |
||
85 | IndexableInterface $indexable, |
||
86 | array $config = [], |
||
87 | LoggerInterface $logger = null |
||
88 | ) { |
||
89 | 17 | $this->config = array_merge([ |
|
90 | 17 | 'identifier' => 'id', |
|
91 | 'defer' => false, |
||
92 | 17 | ], $config); |
|
93 | 17 | $this->indexable = $indexable; |
|
94 | 17 | $this->objectPersister = $objectPersister; |
|
95 | 17 | $this->propertyAccessor = PropertyAccess::createPropertyAccessor(); |
|
96 | |||
97 | 17 | if ($logger && $this->objectPersister instanceof ObjectPersister) { |
|
98 | $this->objectPersister->setLogger($logger); |
||
99 | } |
||
100 | 17 | } |
|
101 | |||
102 | /** |
||
103 | * Handler for the "kernel.terminate" and "console.terminate" Symfony events. |
||
104 | * These event are subscribed to if the listener is configured to persist asynchronously. |
||
105 | */ |
||
106 | 1 | public function onTerminate() |
|
107 | { |
||
108 | 1 | if ($this->config['defer']) { |
|
109 | 1 | $this->config['defer'] = false; |
|
110 | 1 | $this->persistScheduled(); |
|
111 | } |
||
112 | 1 | } |
|
113 | |||
114 | /** |
||
115 | * Looks for new objects that should be indexed. |
||
116 | * |
||
117 | * @param LifecycleEventArgs $eventArgs |
||
118 | */ |
||
119 | 3 | public function postPersist(LifecycleEventArgs $eventArgs) |
|
127 | |||
128 | /** |
||
129 | * Looks for objects being updated that should be indexed or removed from the index. |
||
130 | * |
||
131 | * @param LifecycleEventArgs $eventArgs |
||
132 | */ |
||
133 | 2 | public function postUpdate(LifecycleEventArgs $eventArgs) |
|
146 | |||
147 | /** |
||
148 | * Delete objects preRemove instead of postRemove so that we have access to the id. Because this is called |
||
149 | * preRemove, first check that the entity is managed by Doctrine. |
||
150 | * |
||
151 | * @param LifecycleEventArgs $eventArgs |
||
152 | */ |
||
153 | 2 | public function preRemove(LifecycleEventArgs $eventArgs) |
|
161 | |||
162 | /** |
||
163 | * Determines whether or not it is okay to persist now. |
||
164 | * |
||
165 | * @return bool |
||
166 | */ |
||
167 | 8 | private function shouldPersist() |
|
171 | |||
172 | /** |
||
173 | * Persist scheduled objects to ElasticSearch |
||
174 | * After persisting, clear the scheduled queue to prevent multiple data updates when using multiple flush calls. |
||
175 | */ |
||
176 | 8 | private function persistScheduled() |
|
193 | |||
194 | /** |
||
195 | * Iterate through scheduled actions before flushing to emulate 2.x behavior. |
||
196 | * Note that the ElasticSearch index will fall out of sync with the source |
||
197 | * data in the event of a crash during flush. |
||
198 | * |
||
199 | * This method is only called in legacy configurations of the listener. |
||
200 | * |
||
201 | * @deprecated This method should only be called in applications that depend |
||
202 | * on the behaviour that entities are indexed regardless of if a |
||
203 | * flush is successful |
||
204 | */ |
||
205 | public function preFlush() |
||
209 | |||
210 | /** |
||
211 | * Iterating through scheduled actions *after* flushing ensures that the |
||
212 | * ElasticSearch index will be affected only if the query is successful. |
||
213 | */ |
||
214 | 7 | public function postFlush() |
|
218 | |||
219 | /** |
||
220 | * Record the specified identifier to delete. Do not need to entire object. |
||
221 | * |
||
222 | * @param object $object |
||
223 | */ |
||
224 | 3 | private function scheduleForDeletion($object) |
|
230 | |||
231 | /** |
||
232 | * Checks if the object is indexable or not. |
||
233 | * |
||
234 | * @param object $object |
||
235 | * |
||
236 | * @return bool |
||
237 | */ |
||
238 | 5 | private function isObjectIndexable($object) |
|
246 | } |
||
247 |