1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Async sockets |
4
|
|
|
* |
5
|
|
|
* @copyright Copyright (c) 2015-2017, Efimov Evgenij <[email protected]> |
6
|
|
|
* |
7
|
|
|
* This source file is subject to the MIT license that is bundled |
8
|
|
|
* with this source code in the file LICENSE. |
9
|
|
|
*/ |
10
|
|
|
namespace AsyncSockets\RequestExecutor; |
11
|
|
|
|
12
|
|
|
use AsyncSockets\Event\Event; |
13
|
|
|
use AsyncSockets\Socket\SocketInterface; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Class EventMultiHandler |
17
|
|
|
*/ |
18
|
|
|
class EventMultiHandler implements EventHandlerInterface |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* List of handlers |
22
|
|
|
* |
23
|
|
|
* @var EventHandlerInterface[] |
24
|
|
|
*/ |
25
|
|
|
private $handlers = []; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* EventMultiHandler constructor. |
29
|
|
|
* |
30
|
|
|
* @param EventHandlerInterface[] $handlers List of handlers |
31
|
|
|
*/ |
32
|
4 |
|
public function __construct(array $handlers = []) |
33
|
|
|
{ |
34
|
4 |
|
foreach ($handlers as $handler) { |
35
|
1 |
|
$this->addHandler($handler); |
36
|
4 |
|
} |
37
|
4 |
|
} |
38
|
|
|
|
39
|
|
|
/** {@inheritdoc} */ |
40
|
4 |
|
public function invokeEvent( |
41
|
|
|
Event $event, |
42
|
|
|
RequestExecutorInterface $executor, |
43
|
|
|
SocketInterface $socket, |
44
|
|
|
ExecutionContext $context |
45
|
|
|
) { |
46
|
4 |
|
foreach ($this->handlers as $handler) { |
47
|
3 |
|
$handler->invokeEvent($event, $executor, $socket, $context); |
48
|
4 |
|
} |
49
|
4 |
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Add handler to list |
53
|
|
|
* |
54
|
|
|
* @param EventHandlerInterface $handler Handler to add |
55
|
|
|
* |
56
|
|
|
* @return void |
57
|
|
|
*/ |
58
|
4 |
|
public function addHandler(EventHandlerInterface $handler) |
59
|
|
|
{ |
60
|
4 |
|
$key = $this->getHandlerKey($handler); |
61
|
4 |
|
$this->handlers[$key] = $handler; |
62
|
4 |
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Remove handler from list |
66
|
|
|
* |
67
|
|
|
* @param EventHandlerInterface $handler Handler to remove |
68
|
|
|
* |
69
|
|
|
* @return void |
70
|
|
|
*/ |
71
|
1 |
|
public function removeHandler(EventHandlerInterface $handler) |
72
|
|
|
{ |
73
|
1 |
|
$key = $this->getHandlerKey($handler); |
74
|
1 |
|
if (isset($this->handlers[$key])) { |
75
|
1 |
|
unset($this->handlers[$key]); |
76
|
1 |
|
} |
77
|
1 |
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Return key for given handler |
81
|
|
|
* |
82
|
|
|
* @param EventHandlerInterface $handler Handler to get key for |
83
|
|
|
* |
84
|
|
|
* @return string |
85
|
|
|
*/ |
86
|
4 |
|
private function getHandlerKey(EventHandlerInterface $handler) |
87
|
|
|
{ |
88
|
4 |
|
return spl_object_hash($handler); |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|