1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Kunstmaan\NodeBundle\EventListener; |
4
|
|
|
|
5
|
|
|
use Doctrine\ORM\EntityManager; |
6
|
|
|
use Kunstmaan\NodeBundle\Controller\SlugActionInterface; |
7
|
|
|
use Kunstmaan\NodeBundle\Entity\NodeTranslation; |
8
|
|
|
use Kunstmaan\NodeBundle\Event\Events; |
9
|
|
|
use Kunstmaan\NodeBundle\Event\SlugSecurityEvent; |
10
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
11
|
|
|
use Symfony\Component\EventDispatcher\LegacyEventDispatcherProxy; |
12
|
|
|
use Symfony\Component\HttpKernel\Controller\ControllerResolverInterface; |
13
|
|
|
use Symfony\Component\HttpKernel\Event\ControllerEvent; |
14
|
|
|
use Symfony\Component\HttpKernel\Event\FilterControllerEvent; |
15
|
|
|
|
16
|
|
|
class SlugListener |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var EntityManager |
20
|
|
|
*/ |
21
|
|
|
protected $em; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var ControllerResolverInterface |
25
|
|
|
*/ |
26
|
|
|
protected $resolver; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var EventDispatcherInterface |
30
|
|
|
*/ |
31
|
|
|
protected $eventDispatcher; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* SlugListener constructor. |
35
|
|
|
* |
36
|
|
|
* @param EntityManager $em |
37
|
|
|
* @param ControllerResolverInterface $resolver |
38
|
|
|
* @param EventDispatcherInterface $eventDispatcher |
39
|
|
|
*/ |
40
|
|
|
public function __construct(EntityManager $em, ControllerResolverInterface $resolver, EventDispatcherInterface $eventDispatcher) |
41
|
|
|
{ |
42
|
|
|
$this->em = $em; |
43
|
|
|
$this->resolver = $resolver; |
44
|
|
|
$this->eventDispatcher = $eventDispatcher; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param FilterControllerEvent|ControllerEvent $event |
49
|
|
|
* |
50
|
|
|
* @throws \Exception |
51
|
|
|
*/ |
52
|
|
|
public function onKernelController($event) |
53
|
|
|
{ |
54
|
|
View Code Duplication |
if (!$event instanceof FilterControllerEvent && !$event instanceof ControllerEvent) { |
55
|
|
|
throw new \InvalidArgumentException(\sprintf('Expected instance of type %s, %s given', \class_exists(ControllerEvent::class) ? ControllerEvent::class : FilterControllerEvent::class, \is_object($event) ? \get_class($event) : \gettype($event))); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
$request = $event->getRequest(); |
59
|
|
|
|
60
|
|
|
// Check if the event has a nodeTranslation, if not this method can be skipped |
61
|
|
|
if (!$request->attributes->has('_nodeTranslation')) { |
62
|
|
|
return; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
$nodeTranslation = $request->attributes->get('_nodeTranslation'); |
66
|
|
|
if (!($nodeTranslation instanceof NodeTranslation)) { |
67
|
|
|
throw new \Exception('Invalid _nodeTranslation value found in request attributes'); |
68
|
|
|
} |
69
|
|
|
$entity = $nodeTranslation->getRef($this->em); |
70
|
|
|
|
71
|
|
|
// If the entity is an instance of the SlugActionInterface, change the controller |
72
|
|
|
if ($entity instanceof SlugActionInterface) { |
73
|
|
|
$request->attributes->set('_entity', $entity); |
74
|
|
|
|
75
|
|
|
// Do security check by firing an event that gets handled by the SlugSecurityListener |
76
|
|
|
$securityEvent = new SlugSecurityEvent(); |
77
|
|
|
$securityEvent |
78
|
|
|
->setNode($nodeTranslation->getNode()) |
79
|
|
|
->setEntity($entity) |
80
|
|
|
->setRequest($request) |
81
|
|
|
->setNodeTranslation($nodeTranslation); |
82
|
|
|
|
83
|
|
|
$this->dispatch($securityEvent, Events::SLUG_SECURITY); |
84
|
|
|
|
85
|
|
|
// Set the right controller |
86
|
|
|
$request->attributes->set('_controller', $entity->getControllerAction()); |
87
|
|
|
$event->setController($this->resolver->getController($request)); |
|
|
|
|
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @param object $event |
93
|
|
|
* @param string $eventName |
94
|
|
|
* |
95
|
|
|
* @return object |
96
|
|
|
*/ |
97
|
|
View Code Duplication |
private function dispatch($event, string $eventName) |
98
|
|
|
{ |
99
|
|
|
if (class_exists(LegacyEventDispatcherProxy::class)) { |
100
|
|
|
$eventDispatcher = LegacyEventDispatcherProxy::decorate($this->eventDispatcher); |
101
|
|
|
|
102
|
|
|
return $eventDispatcher->dispatch($event, $eventName); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
return $this->eventDispatcher->dispatch($eventName, $event); |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|