|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Lit\Bolt\Middlewares; |
|
6
|
|
|
|
|
7
|
|
|
use Lit\Bolt\BoltEvent; |
|
8
|
|
|
use Lit\Nimo\Middlewares\AbstractMiddleware; |
|
9
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
10
|
|
|
use Symfony\Component\EventDispatcher\Event; |
|
11
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcher; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Middleware for pub-sub pattern |
|
15
|
|
|
* @deprecated |
|
16
|
|
|
*/ |
|
17
|
|
|
class EventsHub extends AbstractMiddleware |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* @var EventDispatcher |
|
21
|
|
|
*/ |
|
22
|
|
|
protected $eventDispatcher; |
|
23
|
|
|
|
|
24
|
2 |
|
public function __construct(EventDispatcher $eventDispatcher) |
|
25
|
|
|
{ |
|
26
|
2 |
|
$this->eventDispatcher = $eventDispatcher; |
|
27
|
2 |
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @return EventDispatcher |
|
31
|
|
|
*/ |
|
32
|
|
|
public function getEventDispatcher(): EventDispatcher |
|
33
|
|
|
{ |
|
34
|
|
|
@trigger_error('EventsHub is deprecated', E_USER_DEPRECATED); |
|
35
|
|
|
return $this->eventDispatcher; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
public function addListener($eventName, $listener, $priority = 0) |
|
39
|
|
|
{ |
|
40
|
|
|
@trigger_error('EventsHub is deprecated', E_USER_DEPRECATED); |
|
41
|
|
|
$this->eventDispatcher->addListener($eventName, $listener, $priority); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
public function dispatch($eventName, Event $event = null) |
|
45
|
|
|
{ |
|
46
|
|
|
@trigger_error('EventsHub is deprecated', E_USER_DEPRECATED); |
|
47
|
|
|
$this->eventDispatcher->dispatch($eventName, $event); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
2 |
|
protected function main(): ResponseInterface |
|
51
|
|
|
{ |
|
52
|
2 |
|
$this->attachToRequest(); |
|
53
|
|
|
|
|
54
|
2 |
|
$beforeEvent = BoltEvent::of($this, [ |
|
55
|
2 |
|
'request' => $this->request, |
|
56
|
|
|
]); |
|
57
|
2 |
|
$this->eventDispatcher->dispatch(BoltEvent::EVENT_BEFORE_LOGIC, $beforeEvent); |
|
58
|
|
|
|
|
59
|
2 |
|
$this->request = $beforeEvent->getRequest() ?: $this->request; |
|
60
|
2 |
|
$interceptedResponse = $beforeEvent->getResponse(); |
|
61
|
2 |
|
if ($interceptedResponse) { |
|
62
|
|
|
$response = $interceptedResponse; |
|
63
|
|
|
} else { |
|
64
|
2 |
|
$response = $this->delegate(); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
2 |
|
$afterEvent = BoltEvent::of($this, [ |
|
68
|
2 |
|
'request' => $this->request, |
|
69
|
2 |
|
'response' => $response, |
|
70
|
|
|
]); |
|
71
|
2 |
|
$this->eventDispatcher->dispatch(BoltEvent::EVENT_AFTER_LOGIC, $afterEvent); |
|
72
|
|
|
|
|
73
|
2 |
|
return $afterEvent->getResponse() ?: $response; |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
|