|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Victoire\Bundle\CoreBundle\EventSubscriber; |
|
4
|
|
|
|
|
5
|
|
|
use Doctrine\Common\EventSubscriber; |
|
6
|
|
|
use Doctrine\ORM\EntityManager; |
|
7
|
|
|
use Doctrine\ORM\Event\OnFlushEventArgs; |
|
8
|
|
|
use Doctrine\ORM\UnitOfWork; |
|
9
|
|
|
use Victoire\Bundle\CoreBundle\Builder\ViewCssBuilder; |
|
10
|
|
|
use Victoire\Bundle\CoreBundle\Entity\View; |
|
11
|
|
|
use Victoire\Bundle\CoreBundle\Repository\ViewRepository; |
|
12
|
|
|
use Victoire\Bundle\TemplateBundle\Entity\Template; |
|
13
|
|
|
use Victoire\Bundle\WidgetBundle\Entity\Widget; |
|
14
|
|
|
use Victoire\Bundle\WidgetBundle\Repository\WidgetRepository; |
|
15
|
|
|
use Victoire\Bundle\WidgetMapBundle\Builder\WidgetMapBuilder; |
|
16
|
|
|
use Victoire\Bundle\WidgetMapBundle\Entity\WidgetMap; |
|
17
|
|
|
|
|
18
|
|
|
class WidgetSubscriber implements EventSubscriber |
|
19
|
|
|
{ |
|
20
|
|
|
private $viewCssBuilder; |
|
21
|
|
|
private $widgetMapBuilder; |
|
22
|
|
|
/* @var UnitOfWork $uow */ |
|
23
|
|
|
private $uow; |
|
24
|
|
|
/* @var EntityManager $em */ |
|
25
|
|
|
private $em; |
|
26
|
|
|
/* @var WidgetRepository $widgetRepo */ |
|
27
|
|
|
private $widgetRepo; |
|
28
|
|
|
/* @var ViewRepository $viewRepo */ |
|
29
|
|
|
private $viewRepo; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Construct. |
|
33
|
|
|
* |
|
34
|
|
|
* @param ViewCssBuilder $viewCssBuilder |
|
35
|
|
|
*/ |
|
36
|
|
|
public function __construct(ViewCssBuilder $viewCssBuilder, WidgetMapBuilder $widgetMapBuilder) |
|
37
|
|
|
{ |
|
38
|
|
|
$this->viewCssBuilder = $viewCssBuilder; |
|
39
|
|
|
$this->widgetMapBuilder = $widgetMapBuilder; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Get SubscribedEvents. |
|
44
|
|
|
* |
|
45
|
|
|
* @return array |
|
46
|
|
|
*/ |
|
47
|
|
|
public function getSubscribedEvents() |
|
48
|
|
|
{ |
|
49
|
|
|
return [ |
|
50
|
|
|
'onFlush', |
|
51
|
|
|
]; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Change cssHash of views when a widget is updated or deleted. |
|
56
|
|
|
* |
|
57
|
|
|
* @param OnFlushEventArgs $args |
|
58
|
|
|
*/ |
|
59
|
|
|
public function onFlush(OnFlushEventArgs $args) |
|
60
|
|
|
{ |
|
61
|
|
|
$this->em = $args->getEntityManager(); |
|
62
|
|
|
$this->uow = $this->em->getUnitOfWork(); |
|
63
|
|
|
$this->widgetRepo = $this->em->getRepository('Victoire\Bundle\WidgetBundle\Entity\Widget'); |
|
64
|
|
|
$this->viewRepo = $this->em->getRepository('Victoire\Bundle\CoreBundle\Entity\View'); |
|
65
|
|
|
|
|
66
|
|
|
$updatedEntities = $this->uow->getScheduledEntityUpdates(); |
|
67
|
|
|
$deletedEntities = $this->uow->getScheduledEntityDeletions(); |
|
68
|
|
|
|
|
69
|
|
|
//Update View's CSS and inheritors of updated and deleted widgets |
|
70
|
|
|
foreach (array_merge($updatedEntities, $deletedEntities) as $entity) { |
|
71
|
|
|
if (!($entity instanceof Widget)) { |
|
72
|
|
|
continue; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** @var Widget $entity */ |
|
76
|
|
|
foreach ($entity->getWidgetMaps() as $widgetMap) { |
|
77
|
|
|
if ($widgetMap->getAction() !== WidgetMap::ACTION_DELETE) { |
|
78
|
|
|
$view = $widgetMap->getView(); |
|
79
|
|
|
$this->updateViewCss($view); |
|
80
|
|
|
$this->setTemplateInheritorsCssToUpdate($view); |
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
//Remove CSS of deleted View and update its inheritors |
|
86
|
|
|
foreach ($deletedEntities as $entity) { |
|
87
|
|
|
if (!($entity instanceof View)) { |
|
88
|
|
|
continue; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
$this->viewCssBuilder->removeCssFile($entity->getCssHash()); |
|
92
|
|
|
$this->setTemplateInheritorsCssToUpdate($entity); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
//Update CSS of updated View and its inheritors |
|
96
|
|
|
foreach ($updatedEntities as $entity) { |
|
97
|
|
|
if (!($entity instanceof View)) { |
|
98
|
|
|
continue; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
$this->updateViewCss($entity); |
|
102
|
|
|
$this->setTemplateInheritorsCssToUpdate($entity); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
$this->uow->computeChangeSets(); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* Change view cssHash, update css file and persist new cssHash. |
|
110
|
|
|
* |
|
111
|
|
|
* @param View $view |
|
112
|
|
|
*/ |
|
113
|
|
|
public function updateViewCss(View $view) |
|
114
|
|
|
{ |
|
115
|
|
|
$view->changeCssHash(); |
|
116
|
|
|
|
|
117
|
|
|
//Update css file |
|
118
|
|
|
$this->widgetMapBuilder->build($view, $this->em, true); |
|
119
|
|
|
$widgets = $this->widgetRepo->findAllWidgetsForView($view); |
|
120
|
|
|
|
|
121
|
|
|
//Generate CSS file and set View's CSS as up to date |
|
122
|
|
|
$oldHash = $view->getCssHash(); |
|
123
|
|
|
$view->changeCssHash(); |
|
124
|
|
|
$this->viewCssBuilder->updateViewCss($oldHash, $view, $widgets); |
|
125
|
|
|
$view->setCssUpToDate(true); |
|
126
|
|
|
|
|
127
|
|
|
//Persist new hash and upToDate bool |
|
128
|
|
|
$metadata = $this->em->getClassMetadata(get_class($view)); |
|
129
|
|
|
$this->uow->recomputeSingleEntityChangeSet($metadata, $view); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* Set a Template inheritors (View) as not up to date. |
|
134
|
|
|
* |
|
135
|
|
|
* @param View $view |
|
136
|
|
|
*/ |
|
137
|
|
|
public function setTemplateInheritorsCssToUpdate(View $view) |
|
138
|
|
|
{ |
|
139
|
|
|
if (!($view instanceof Template)) { |
|
140
|
|
|
return; |
|
141
|
|
|
} |
|
142
|
|
|
foreach ($view->getInheritors() as $inheritor) { |
|
|
|
|
|
|
143
|
|
|
$inheritor->setCssUpToDate(false); |
|
144
|
|
|
$this->uow->persist($inheritor); |
|
145
|
|
|
$this->setTemplateInheritorsCssToUpdate($inheritor); |
|
146
|
|
|
} |
|
147
|
|
|
} |
|
148
|
|
|
} |
|
149
|
|
|
|