|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* GpsLab component. |
|
4
|
|
|
* |
|
5
|
|
|
* @author Peter Gribanov <[email protected]> |
|
6
|
|
|
* @copyright Copyright (c) 2016, Peter Gribanov |
|
7
|
|
|
* @license http://opensource.org/licenses/MIT |
|
8
|
|
|
*/ |
|
9
|
|
|
namespace GpsLab\Bundle\DomainEvent\Event\Subscriber; |
|
10
|
|
|
|
|
11
|
|
|
use Doctrine\Common\EventSubscriber; |
|
12
|
|
|
use Doctrine\Common\Persistence\Event\LifecycleEventArgs; |
|
13
|
|
|
use Doctrine\ORM\Events; |
|
14
|
|
|
use GpsLab\Domain\Event\Aggregator\AggregateEventsInterface; |
|
15
|
|
|
use GpsLab\Domain\Event\Bus\Bus; |
|
16
|
|
|
use Symfony\Bridge\Doctrine\RegistryInterface; |
|
17
|
|
|
|
|
18
|
|
|
class DoctrineSubscriber implements EventSubscriber |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* @var Bus |
|
22
|
|
|
*/ |
|
23
|
|
|
private $bus; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var array |
|
27
|
|
|
*/ |
|
28
|
|
|
private $events = []; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @param Bus $bus |
|
32
|
|
|
* @param RegistryInterface $doctrine |
|
33
|
|
|
* @param array $events |
|
34
|
|
|
* @param array $connections |
|
35
|
|
|
*/ |
|
36
|
|
|
public function __construct(Bus $bus, RegistryInterface $doctrine, array $events, array $connections) |
|
37
|
|
|
{ |
|
38
|
|
|
$this->bus = $bus; |
|
39
|
|
|
$this->events = array_intersect([ |
|
40
|
|
|
Events::prePersist, |
|
41
|
|
|
Events::preUpdate, |
|
42
|
|
|
Events::preRemove, |
|
43
|
|
|
Events::preFlush, |
|
44
|
|
|
], $events); |
|
45
|
|
|
|
|
46
|
|
|
foreach ($connections as $connection) { |
|
47
|
|
|
$doctrine |
|
48
|
|
|
->getEntityManager($connection) |
|
49
|
|
|
->getEventManager() |
|
50
|
|
|
->addEventSubscriber($this); |
|
51
|
|
|
} |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @return array |
|
56
|
|
|
*/ |
|
57
|
|
|
public function getSubscribedEvents() |
|
58
|
|
|
{ |
|
59
|
|
|
return $this->events; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @param LifecycleEventArgs $args |
|
64
|
|
|
*/ |
|
65
|
|
|
public function prePersist(LifecycleEventArgs $args) |
|
66
|
|
|
{ |
|
67
|
|
|
$this->pullAndPublish($args); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* @param LifecycleEventArgs $args |
|
72
|
|
|
*/ |
|
73
|
|
|
public function preUpdate(LifecycleEventArgs $args) |
|
74
|
|
|
{ |
|
75
|
|
|
$this->pullAndPublish($args); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* @param LifecycleEventArgs $args |
|
80
|
|
|
*/ |
|
81
|
|
|
public function preRemove(LifecycleEventArgs $args) |
|
82
|
|
|
{ |
|
83
|
|
|
$this->pullAndPublish($args); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* @param LifecycleEventArgs $args |
|
88
|
|
|
*/ |
|
89
|
|
|
public function preFlush(LifecycleEventArgs $args) |
|
90
|
|
|
{ |
|
91
|
|
|
$this->pullAndPublish($args); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* @param LifecycleEventArgs $args |
|
96
|
|
|
*/ |
|
97
|
|
|
protected function pullAndPublish(LifecycleEventArgs $args) |
|
98
|
|
|
{ |
|
99
|
|
|
$object = $args->getObject(); |
|
100
|
|
|
if ($object instanceof AggregateEventsInterface) { |
|
101
|
|
|
$this->bus->pullAndPublish($object); |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
|