|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Silverback\ApiComponentBundle\EventSubscriber\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
|
|
|
public function __construct( |
|
26
|
|
|
RouteFactory $routeFactory |
|
27
|
|
|
) { |
|
28
|
|
|
$this->routeFactory = $routeFactory; |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @return array |
|
33
|
|
|
*/ |
|
34
|
|
|
public function getSubscribedEvents(): array |
|
35
|
|
|
{ |
|
36
|
|
|
return [ |
|
37
|
|
|
'prePersist', |
|
38
|
|
|
'preUpdate', |
|
39
|
|
|
'preFlush' |
|
40
|
|
|
]; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @param LifecycleEventArgs $eventArgs |
|
45
|
|
|
* @throws \Doctrine\ORM\ORMException |
|
46
|
|
|
*/ |
|
47
|
|
|
public function prePersist(LifecycleEventArgs $eventArgs): void |
|
48
|
|
|
{ |
|
49
|
|
|
$entity = $eventArgs->getEntity(); |
|
50
|
|
|
$this->prePersistUpdate($entity, $eventArgs->getEntityManager()); |
|
51
|
|
|
} |
|
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
|
|
|
public function prePersistUpdate($entity, EntityManager $em): void |
|
69
|
|
|
{ |
|
70
|
|
|
if ( |
|
71
|
|
|
$entity instanceof RouteAwareInterface && |
|
72
|
|
|
$route = $this->createPageRoute($entity) |
|
73
|
|
|
) { |
|
74
|
|
|
$em->persist($route); |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* @param PreFlushEventArgs $eventArgs |
|
80
|
|
|
* @throws \Doctrine\ORM\ORMException |
|
81
|
|
|
*/ |
|
82
|
|
|
public function preFlush(PreFlushEventArgs $eventArgs): void |
|
83
|
|
|
{ |
|
84
|
|
|
$em = $eventArgs->getEntityManager(); |
|
85
|
|
|
$uow = $em->getUnitOfWork(); |
|
86
|
|
|
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
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* @param RouteAwareInterface $entity |
|
101
|
|
|
* @return null|Route |
|
102
|
|
|
*/ |
|
103
|
|
|
private function createPageRoute(RouteAwareInterface $entity): ?Route |
|
104
|
|
|
{ |
|
105
|
|
|
if (0 === $entity->getRoutes()->count()) { |
|
106
|
|
|
return $this->routeFactory->createFromRouteAwareEntity($entity); |
|
107
|
|
|
} |
|
108
|
|
|
return null; |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
|