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