|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Victoire\Bundle\ViewReferenceBundle\EventSubscriber; |
|
4
|
|
|
|
|
5
|
|
|
use Doctrine\ORM\Event\LifecycleEventArgs; |
|
6
|
|
|
use Doctrine\ORM\Events; |
|
7
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
|
8
|
|
|
use Victoire\Bundle\CoreBundle\Entity\WebViewInterface; |
|
9
|
|
|
use Victoire\Bundle\I18nBundle\Entity\ViewTranslation; |
|
10
|
|
|
use Victoire\Bundle\ViewReferenceBundle\Event\ViewReferenceEvent; |
|
11
|
|
|
use Victoire\Bundle\ViewReferenceBundle\ViewReferenceEvents; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Tracks if a slug changed and re-compute the view cache |
|
15
|
|
|
* ref: victoire_view_reference.event_subscriber. |
|
16
|
|
|
*/ |
|
17
|
|
|
class ViewReferenceSubscriber implements \Doctrine\Common\EventSubscriber |
|
18
|
|
|
{ |
|
19
|
|
|
protected $businessPageBuilder; |
|
20
|
|
|
protected $viewReferenceProvider; |
|
21
|
|
|
protected $viewReferenceHelper; |
|
22
|
|
|
protected $dispatcher; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* ViewReferenceSubscriber constructor. |
|
26
|
|
|
* |
|
27
|
|
|
* @param EventDispatcherInterface $dispatcher |
|
28
|
|
|
*/ |
|
29
|
|
|
public function __construct(EventDispatcherInterface $dispatcher) |
|
30
|
|
|
{ |
|
31
|
|
|
$this->dispatcher = $dispatcher; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @return string[] |
|
36
|
|
|
*/ |
|
37
|
|
|
public function getSubscribedEvents() |
|
38
|
|
|
{ |
|
39
|
|
|
return [ |
|
40
|
|
|
Events::postUpdate, |
|
41
|
|
|
Events::postPersist, |
|
42
|
|
|
Events::preRemove, |
|
43
|
|
|
]; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @param LifecycleEventArgs $eventArgs |
|
48
|
|
|
*/ |
|
49
|
|
|
public function postUpdate(LifecycleEventArgs $eventArgs) |
|
50
|
|
|
{ |
|
51
|
|
|
$this->updateViewReference($eventArgs); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @param LifecycleEventArgs $eventArgs |
|
56
|
|
|
*/ |
|
57
|
|
|
public function postPersist(LifecycleEventArgs $eventArgs) |
|
58
|
|
|
{ |
|
59
|
|
|
$this->updateViewReference($eventArgs); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @param LifecycleEventArgs $eventArgs |
|
64
|
|
|
*/ |
|
65
|
|
|
public function preRemove(LifecycleEventArgs $eventArgs) |
|
66
|
|
|
{ |
|
67
|
|
|
$entity = $eventArgs->getEntity(); |
|
68
|
|
|
// if a page is remove we remove his viewRef |
|
69
|
|
|
if ($entity instanceof WebViewInterface) { |
|
70
|
|
|
$event = new ViewReferenceEvent($entity); |
|
71
|
|
|
$this->dispatcher->dispatch(ViewReferenceEvents::REMOVE_VIEW_REFERENCE, $event); |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* This method dispatch the event that the view must be build/rebuild. |
|
77
|
|
|
* |
|
78
|
|
|
* @param LifecycleEventArgs $eventArgs |
|
79
|
|
|
*/ |
|
80
|
|
|
private function updateViewReference(LifecycleEventArgs $eventArgs) |
|
81
|
|
|
{ |
|
82
|
|
|
$entity = $eventArgs->getEntity(); |
|
83
|
|
|
$view = null; |
|
84
|
|
|
$translations = []; |
|
85
|
|
|
if ($entity instanceof WebViewInterface) { |
|
86
|
|
|
$translations = $entity->getTranslations(); |
|
87
|
|
|
$view = $entity; |
|
88
|
|
|
} elseif ($entity instanceof ViewTranslation && $entity->getTranslatable() instanceof WebViewInterface) { |
|
89
|
|
|
$translations = [$entity]; |
|
90
|
|
|
$view = $entity->getTranslatable(); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
// if a page is persisted we rebuild his viewRef |
|
94
|
|
|
foreach ($translations as $translation) { |
|
95
|
|
|
$view->setCurrentLocale($translation->getLocale()); |
|
96
|
|
|
$event = new ViewReferenceEvent($view); |
|
|
|
|
|
|
97
|
|
|
$this->dispatcher->dispatch(ViewReferenceEvents::UPDATE_VIEW_REFERENCE, $event); |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
|
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: