1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the API Platform project. |
5
|
|
|
* |
6
|
|
|
* (c) Kévin Dunglas <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
declare(strict_types=1); |
13
|
|
|
|
14
|
|
|
namespace ApiPlatform\Core\Bridge\Symfony\Bundle\EventSubscriber; |
15
|
|
|
|
16
|
|
|
use ApiPlatform\Core\Event\DeserializeEvent; |
17
|
|
|
use ApiPlatform\Core\Event\FormatAddEvent; |
18
|
|
|
use ApiPlatform\Core\Event\PostDeserializeEvent; |
19
|
|
|
use ApiPlatform\Core\Event\PostReadEvent; |
20
|
|
|
use ApiPlatform\Core\Event\PreDeserializeEvent; |
21
|
|
|
use ApiPlatform\Core\Event\PreReadEvent; |
22
|
|
|
use ApiPlatform\Core\Event\QueryParameterValidateEvent; |
23
|
|
|
use ApiPlatform\Core\Event\ReadEvent; |
24
|
|
|
use ApiPlatform\Core\Events; |
25
|
|
|
use Symfony\Component\EventDispatcher\Event; |
26
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
27
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
28
|
|
|
use Symfony\Component\HttpKernel\Event\GetResponseEvent; |
29
|
|
|
use Symfony\Component\HttpKernel\KernelEvents; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @internal |
33
|
|
|
* |
34
|
|
|
* @author Alan Poulain <[email protected]> |
35
|
|
|
*/ |
36
|
|
|
final class EventDispatcher implements EventSubscriberInterface |
37
|
|
|
{ |
38
|
|
|
private const INTERNAL_EVENTS_CONFIGURATION = [ |
39
|
|
|
KernelEvents::REQUEST => [ |
40
|
|
|
QueryParameterValidateEvent::class => Events::QUERY_PARAMETER_VALIDATE, |
41
|
|
|
FormatAddEvent::class => Events::FORMAT_ADD, |
42
|
|
|
|
43
|
|
|
PreReadEvent::class => Events::PRE_READ, |
44
|
|
|
ReadEvent::class => Events::READ, |
45
|
|
|
PostReadEvent::class => Events::POST_READ, |
46
|
|
|
|
47
|
|
|
PreDeserializeEvent::class => Events::PRE_DESERIALIZE, |
48
|
|
|
DeserializeEvent::class => Events::DESERIALIZE, |
49
|
|
|
PostDeserializeEvent::class => Events::POST_DESERIALIZE, |
50
|
|
|
], |
51
|
|
|
]; |
52
|
|
|
|
53
|
|
|
private $dispatcher; |
54
|
|
|
|
55
|
|
|
public function __construct(EventDispatcherInterface $dispatcher) |
56
|
|
|
{ |
57
|
|
|
$this->dispatcher = $dispatcher; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public static function getSubscribedEvents(): array |
61
|
|
|
{ |
62
|
|
|
return [ |
63
|
|
|
KernelEvents::REQUEST => 'dispatch', |
64
|
|
|
]; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function dispatch(Event $event, string $eventName): void |
68
|
|
|
{ |
69
|
|
|
$internalEventData = null; |
70
|
|
|
$internalEventContext = []; |
71
|
|
|
|
72
|
|
|
switch (true) { |
73
|
|
|
case $event instanceof GetResponseEvent: |
74
|
|
|
$internalEventContext = ['request' => $event->getRequest()]; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
foreach ($this->getInternalEventData($eventName) as $internalEventClass => $internalEventName) { |
78
|
|
|
$internalEvent = new $internalEventClass($internalEventData, $internalEventContext); |
79
|
|
|
|
80
|
|
|
$this->dispatcher->dispatch($internalEventName, $internalEvent); |
|
|
|
|
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
private function getInternalEventData(string $eventName): \Generator |
85
|
|
|
{ |
86
|
|
|
yield from self::INTERNAL_EVENTS_CONFIGURATION[$eventName]; |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|