1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of CaptainHook. |
5
|
|
|
* |
6
|
|
|
* (c) Sebastian Feldmann <[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
|
|
|
namespace CaptainHook\App\Event; |
13
|
|
|
|
14
|
|
|
use CaptainHook\App\Config; |
15
|
|
|
use CaptainHook\App\Console\IO; |
16
|
|
|
use SebastianFeldmann\Git\Repository; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Event Dispatcher |
20
|
|
|
* |
21
|
|
|
* This allows the user to hook into the Cap'n on a deeper level. For example execute code if the hook execution fails. |
22
|
|
|
* |
23
|
|
|
* @package CaptainHook |
24
|
|
|
* @author Sebastian Feldmann <[email protected]> |
25
|
|
|
* @link https://github.com/captainhook-git/captainhook |
26
|
|
|
* @since Class available since Release 5.11.0 |
27
|
|
|
*/ |
28
|
|
|
class Dispatcher |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* List of all registered handlers |
32
|
|
|
* |
33
|
|
|
* @var array<string, array<int, \CaptainHook\App\Event\Handler>> |
34
|
|
|
*/ |
35
|
|
|
private $config = []; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Event factory to create all necessary events |
39
|
|
|
* |
40
|
|
|
* @var \CaptainHook\App\Event\Factory |
41
|
|
|
*/ |
42
|
|
|
private $factory; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Event Dispatcher |
46
|
|
|
* |
47
|
|
|
* @param \CaptainHook\App\Console\IO $io |
48
|
|
|
* @param \CaptainHook\App\Config $config |
49
|
|
|
* @param \SebastianFeldmann\Git\Repository $repository |
50
|
|
|
*/ |
51
|
56 |
|
public function __construct(IO $io, Config $config, Repository $repository) |
52
|
|
|
{ |
53
|
56 |
|
$this->factory = new Factory($io, $config, $repository); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Register handlers received from a Listener to the dispatcher |
58
|
|
|
* |
59
|
|
|
* @param array<string, array<int, \CaptainHook\App\Event\Handler>> $eventConfig |
60
|
|
|
* @return void |
61
|
|
|
*/ |
62
|
2 |
|
public function subscribeHandlers(array $eventConfig): void |
63
|
|
|
{ |
64
|
2 |
|
foreach ($eventConfig as $event => $handlers) { |
65
|
1 |
|
foreach ($handlers as $handler) { |
66
|
1 |
|
$this->subscribeHandlerToEvent($handler, $event); |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Register a single event handler to an event |
73
|
|
|
* |
74
|
|
|
* @param \CaptainHook\App\Event\Handler $handler |
75
|
|
|
* @param string $event |
76
|
|
|
* @return void |
77
|
|
|
*/ |
78
|
1 |
|
public function subscribeHandlerToEvent(Handler $handler, string $event): void |
79
|
|
|
{ |
80
|
1 |
|
$this->config[$event][] = $handler; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Trigger all event handlers registered for a given event |
85
|
|
|
* |
86
|
|
|
* @param string $eventName |
87
|
|
|
* @throws \Exception |
88
|
|
|
* @return void |
89
|
|
|
*/ |
90
|
20 |
|
public function dispatch(string $eventName): void |
91
|
|
|
{ |
92
|
20 |
|
$event = $this->factory->createEvent($eventName); |
93
|
|
|
|
94
|
20 |
|
foreach ($this->handlersFor($event->name()) as $handler) { |
95
|
1 |
|
$handler->handle($event); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Return a list of handlers for a given event |
101
|
|
|
* |
102
|
|
|
* @param string $event |
103
|
|
|
* @return \CaptainHook\App\Event\Handler[]; |
104
|
|
|
*/ |
105
|
20 |
|
private function handlersFor(string $event): array |
106
|
|
|
{ |
107
|
20 |
|
return $this->config[$event] ?? []; |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|