1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace FOS\ElasticaBundle\Doctrine; |
4
|
|
|
|
5
|
|
|
use Doctrine\Common\Persistence\Event\LifecycleEventArgs; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Automatically update ElasticSearch based on changes to the Doctrine source |
9
|
|
|
* data. One listener is generated for each Doctrine entity / ElasticSearch type. |
10
|
|
|
*/ |
11
|
|
|
class Listener extends AbstractListenerBase |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* Looks for new objects that should be indexed. |
15
|
|
|
* |
16
|
|
|
* @param LifecycleEventArgs $eventArgs |
17
|
|
|
*/ |
18
|
|
|
public function postPersist(LifecycleEventArgs $eventArgs) |
19
|
|
|
{ |
20
|
|
|
$this->doPostPersist($eventArgs->getObject()); |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Looks for objects being updated that should be indexed or removed from the index. |
25
|
|
|
* |
26
|
|
|
* @param LifecycleEventArgs $eventArgs |
27
|
|
|
*/ |
28
|
|
|
public function postUpdate(LifecycleEventArgs $eventArgs) |
29
|
|
|
{ |
30
|
|
|
$this->doPostUpdate($eventArgs->getObject()); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Delete objects preRemove instead of postRemove so that we have access to the id. Because this is called |
35
|
|
|
* preRemove, first check that the entity is managed by Doctrine. |
36
|
|
|
* |
37
|
|
|
* @param LifecycleEventArgs $eventArgs |
38
|
|
|
*/ |
39
|
|
|
public function preRemove(LifecycleEventArgs $eventArgs) |
40
|
|
|
{ |
41
|
|
|
$this->doPreRemove($eventArgs->getObject()); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Iterate through scheduled actions before flushing to emulate 2.x behavior. |
46
|
|
|
* Note that the ElasticSearch index will fall out of sync with the source |
47
|
|
|
* data in the event of a crash during flush. |
48
|
|
|
* |
49
|
|
|
* This method is only called in legacy configurations of the listener. |
50
|
|
|
* |
51
|
|
|
* @deprecated This method should only be called in applications that depend |
52
|
|
|
* on the behaviour that entities are indexed regardless of if a |
53
|
|
|
* flush is successful. |
54
|
|
|
*/ |
55
|
|
|
public function preFlush() |
56
|
|
|
{ |
57
|
|
|
$this->doPostFlush(); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Iterating through scheduled actions *after* flushing ensures that the |
62
|
|
|
* ElasticSearch index will be affected only if the query is successful. |
63
|
|
|
*/ |
64
|
|
|
public function postFlush() |
65
|
|
|
{ |
66
|
|
|
$this->doPostFlush(); |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|