1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Silverback\ApiComponentBundle\EventListener\Doctrine; |
4
|
|
|
|
5
|
|
|
use Doctrine\Common\EventSubscriber; |
6
|
|
|
use Doctrine\ORM\EntityManager; |
7
|
|
|
use Doctrine\ORM\Event\LifecycleEventArgs; |
8
|
|
|
use Doctrine\ORM\Event\PreFlushEventArgs; |
9
|
|
|
use Doctrine\ORM\Event\PreUpdateEventArgs; |
10
|
|
|
use Silverback\ApiComponentBundle\Entity\Route\Route; |
11
|
|
|
use Silverback\ApiComponentBundle\Entity\Route\RouteAwareInterface; |
12
|
|
|
use Silverback\ApiComponentBundle\Factory\Entity\Route\RouteFactory; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* |
16
|
|
|
* @author Daniel West <[email protected]> |
17
|
|
|
*/ |
18
|
|
|
class RouteAwareSubscriber implements EventSubscriber |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @var RouteFactory |
22
|
|
|
*/ |
23
|
|
|
private $routeFactory; |
24
|
|
|
|
25
|
3 |
|
public function __construct( |
26
|
|
|
RouteFactory $routeFactory |
27
|
|
|
) { |
28
|
3 |
|
$this->routeFactory = $routeFactory; |
29
|
3 |
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @return array |
33
|
|
|
*/ |
34
|
3 |
|
public function getSubscribedEvents(): array |
35
|
|
|
{ |
36
|
|
|
return [ |
37
|
3 |
|
'prePersist', |
38
|
|
|
'preUpdate', |
39
|
|
|
'preFlush' |
40
|
|
|
]; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @param LifecycleEventArgs $eventArgs |
45
|
|
|
* @throws \Doctrine\ORM\ORMException |
46
|
|
|
*/ |
47
|
1 |
|
public function prePersist(LifecycleEventArgs $eventArgs): void |
48
|
|
|
{ |
49
|
1 |
|
$entity = $eventArgs->getEntity(); |
50
|
1 |
|
$this->prePersistUpdate($entity, $eventArgs->getEntityManager()); |
51
|
1 |
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @param PreUpdateEventArgs $eventArgs |
55
|
|
|
* @throws \Doctrine\ORM\ORMException |
56
|
|
|
*/ |
57
|
|
|
public function preUpdate(PreUpdateEventArgs $eventArgs): void |
58
|
|
|
{ |
59
|
|
|
$entity = $eventArgs->getEntity(); |
60
|
|
|
$this->prePersistUpdate($entity, $eventArgs->getEntityManager()); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param mixed $entity |
65
|
|
|
* @param EntityManager $em |
66
|
|
|
* @throws \Doctrine\ORM\ORMException |
67
|
|
|
*/ |
68
|
1 |
|
public function prePersistUpdate($entity, EntityManager $em): void |
69
|
|
|
{ |
70
|
|
|
if ( |
71
|
1 |
|
$entity instanceof RouteAwareInterface && |
72
|
1 |
|
$route = $this->createPageRoute($entity) |
73
|
|
|
) { |
74
|
1 |
|
$em->persist($route); |
75
|
|
|
} |
76
|
1 |
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @param PreFlushEventArgs $eventArgs |
80
|
|
|
* @throws \Doctrine\ORM\ORMException |
81
|
|
|
*/ |
82
|
1 |
|
public function preFlush(PreFlushEventArgs $eventArgs): void |
83
|
|
|
{ |
84
|
1 |
|
$em = $eventArgs->getEntityManager(); |
85
|
1 |
|
$uow = $em->getUnitOfWork(); |
86
|
1 |
|
foreach ($uow->getScheduledEntityUpdates() as $entity) { |
87
|
|
|
if ( |
88
|
|
|
$entity instanceof RouteAwareInterface && |
89
|
|
|
$route = $this->createPageRoute($entity) |
90
|
|
|
) { |
91
|
|
|
$pageClassMetaData = $em->getClassMetadata(\get_class($entity)); |
92
|
|
|
$uow = $em->getUnitOfWork(); |
93
|
|
|
$uow->recomputeSingleEntityChangeSet($pageClassMetaData, $entity); |
94
|
|
|
$em->persist($route); |
95
|
|
|
} |
96
|
|
|
} |
97
|
1 |
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @param RouteAwareInterface $entity |
101
|
|
|
* @return null|Route |
102
|
|
|
*/ |
103
|
1 |
|
private function createPageRoute(RouteAwareInterface $entity): ?Route |
104
|
|
|
{ |
105
|
1 |
|
if (0 === $entity->getRoutes()->count()) { |
106
|
1 |
|
return $this->routeFactory->createFromRouteAwareEntity($entity); |
107
|
|
|
} |
108
|
|
|
return null; |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|