|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of the sauls/object-registry-bundle package. |
|
4
|
|
|
* |
|
5
|
|
|
* @author Saulius Vaičeliūnas <[email protected]> |
|
6
|
|
|
* @link http://saulius.vaiceliunas.lt |
|
7
|
|
|
* @copyright 2018 |
|
8
|
|
|
* |
|
9
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
10
|
|
|
* file that was distributed with this source code. |
|
11
|
|
|
*/ |
|
12
|
|
|
|
|
13
|
|
|
namespace Sauls\Bundle\ObjectRegistryBundle\EvenListener; |
|
14
|
|
|
|
|
15
|
|
|
use Doctrine\ORM\Event\LifecycleEventArgs; |
|
16
|
|
|
use Doctrine\ORM\Event\PreUpdateEventArgs; |
|
17
|
|
|
use Doctrine\ORM\Events; |
|
18
|
|
|
use Sauls\Bundle\ObjectRegistryBundle\Event\DoctrineObjectEvents; |
|
19
|
|
|
use Sauls\Bundle\ObjectRegistryBundle\Event\GenericDoctrineObjectEvent; |
|
20
|
|
|
use Sauls\Bundle\ObjectRegistryBundle\EventDispatcher\EventDispatcherInterface; |
|
21
|
|
|
use Sauls\Bundle\ObjectRegistryBundle\Factory\EventNameFactoryInterface; |
|
22
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
|
23
|
|
|
|
|
24
|
|
|
class DoctrineEventsSubscriber implements EventSubscriberInterface |
|
25
|
|
|
{ |
|
26
|
|
|
/** |
|
27
|
|
|
* @var EventDispatcherInterface |
|
28
|
|
|
*/ |
|
29
|
|
|
private $eventDispatcher; |
|
30
|
|
|
/** |
|
31
|
|
|
* @var EventNameFactoryInterface |
|
32
|
|
|
*/ |
|
33
|
|
|
private $eventNameFactory; |
|
34
|
|
|
|
|
35
|
6 |
|
public function __construct(EventDispatcherInterface $eventDispatcher, EventNameFactoryInterface $eventNameFactory) |
|
36
|
|
|
{ |
|
37
|
6 |
|
$this->eventDispatcher = $eventDispatcher; |
|
38
|
6 |
|
$this->eventNameFactory = $eventNameFactory; |
|
39
|
6 |
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* {@inheritdoc} |
|
43
|
|
|
*/ |
|
44
|
1 |
|
public static function getSubscribedEvents(): array |
|
45
|
|
|
{ |
|
46
|
|
|
return [ |
|
47
|
1 |
|
Events::prePersist => ['onPrePersist'], |
|
48
|
1 |
|
Events::postPersist => ['onPostPersist'], |
|
49
|
1 |
|
Events::preUpdate => ['onPreUpdate'], |
|
50
|
1 |
|
Events::postUpdate => ['onPostUpdate'], |
|
51
|
1 |
|
Events::preRemove => ['onPreRemove'], |
|
52
|
1 |
|
Events::postRemove => ['onPostRemove'], |
|
53
|
|
|
]; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
1 |
|
public function onPrePersist(LifecycleEventArgs $event): void |
|
57
|
|
|
{ |
|
58
|
1 |
|
$this->process(DoctrineObjectEvents::PRE_PERSIST, $event); |
|
59
|
1 |
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @param string $eventName |
|
63
|
|
|
* @param LifecycleEventArgs|PreUpdateEventArgs $event |
|
64
|
|
|
*/ |
|
65
|
6 |
|
private function process(string $eventName, LifecycleEventArgs $event): void |
|
66
|
|
|
{ |
|
67
|
6 |
|
$entity = $event->getEntity(); |
|
68
|
6 |
|
$eventName = $this->eventNameFactory->create($eventName, $entity); |
|
69
|
6 |
|
$newEvent = new GenericDoctrineObjectEvent($entity, $event); |
|
70
|
|
|
|
|
71
|
6 |
|
$this->eventDispatcher->dispatch($eventName, $newEvent); |
|
72
|
6 |
|
} |
|
73
|
|
|
|
|
74
|
1 |
|
public function onPostPersist(LifecycleEventArgs $event): void |
|
75
|
|
|
{ |
|
76
|
1 |
|
$this->process(DoctrineObjectEvents::POST_PERSIST, $event); |
|
77
|
1 |
|
} |
|
78
|
|
|
|
|
79
|
1 |
|
public function onPreUpdate(PreUpdateEventArgs $event): void |
|
80
|
|
|
{ |
|
81
|
1 |
|
$this->process(DoctrineObjectEvents::PRE_UPDATE, $event); |
|
82
|
1 |
|
} |
|
83
|
|
|
|
|
84
|
1 |
|
public function onPostUpdate(LifecycleEventArgs $event): void |
|
85
|
|
|
{ |
|
86
|
1 |
|
$this->process(DoctrineObjectEvents::POST_UPDATE, $event); |
|
87
|
1 |
|
} |
|
88
|
|
|
|
|
89
|
1 |
|
public function onPreRemove(LifecycleEventArgs $event): void |
|
90
|
|
|
{ |
|
91
|
1 |
|
$this->process(DoctrineObjectEvents::PRE_REMOVE, $event); |
|
92
|
1 |
|
} |
|
93
|
|
|
|
|
94
|
1 |
|
public function onPostRemove(LifecycleEventArgs $event): void |
|
95
|
|
|
{ |
|
96
|
1 |
|
$this->process(DoctrineObjectEvents::POST_REMOVE, $event); |
|
97
|
1 |
|
} |
|
98
|
|
|
} |
|
99
|
|
|
|