|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Metabor\Event; |
|
4
|
|
|
|
|
5
|
|
|
use MetaborStd\CallbackInterface; |
|
6
|
|
|
use MetaborStd\Event\DispatcherInterface; |
|
7
|
|
|
use MetaborStd\Event\EventInterface; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* @author Oliver Tischlinger |
|
11
|
|
|
*/ |
|
12
|
|
|
class Dispatcher implements DispatcherInterface |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* @var array |
|
16
|
|
|
*/ |
|
17
|
|
|
private $commands = array(); |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @var array |
|
21
|
|
|
*/ |
|
22
|
|
|
private $onReadyCallbacks = array(); |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var bool |
|
26
|
|
|
*/ |
|
27
|
|
|
private $isReady = false; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @param callable $command |
|
31
|
|
|
* @param array $arguments |
|
32
|
|
|
*/ |
|
33
|
3 |
|
protected function addCommand($command, array $arguments) |
|
34
|
|
|
{ |
|
35
|
3 |
|
$this->commands[] = array($command, $arguments); |
|
36
|
3 |
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @return array |
|
40
|
|
|
*/ |
|
41
|
3 |
|
protected function getCommands() |
|
42
|
|
|
{ |
|
43
|
3 |
|
return $this->commands; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @param callable $command |
|
48
|
|
|
* @param array $arguments |
|
49
|
|
|
*/ |
|
50
|
|
|
protected function removeCommand($command, array $arguments) |
|
51
|
|
|
{ |
|
52
|
|
|
$key = array_search(array($command, $arguments), $this->commands); |
|
53
|
|
|
if ($key !== false) { |
|
54
|
|
|
unset($this->commands[$key]); |
|
55
|
|
|
} |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @param EventInterface $event |
|
60
|
|
|
* @param array $arguments |
|
61
|
|
|
*/ |
|
62
|
3 |
|
protected function addEvent(EventInterface $event, array $arguments) |
|
63
|
|
|
{ |
|
64
|
3 |
|
$this->addCommand($event, $arguments); |
|
65
|
3 |
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @see \MetaborStd\Event\DispatcherInterface::dispatch() |
|
69
|
|
|
*/ |
|
70
|
3 |
|
public function dispatch( |
|
71
|
|
|
EventInterface $event, |
|
72
|
|
|
array $arguments = array(), |
|
73
|
|
|
CallbackInterface $onReadyCallback = null |
|
74
|
|
|
) { |
|
75
|
3 |
|
if (!$this->isReady) { |
|
76
|
3 |
|
$this->addEvent($event, $arguments); |
|
77
|
3 |
|
if ($onReadyCallback) { |
|
78
|
3 |
|
$this->onReadyCallbacks[] = $onReadyCallback; |
|
79
|
3 |
|
} |
|
80
|
3 |
|
} else { |
|
81
|
|
|
throw new \RuntimeException('Was already invoked!'); |
|
82
|
|
|
} |
|
83
|
3 |
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* @see \MetaborStd\CallbackInterface::__invoke() |
|
87
|
|
|
*/ |
|
88
|
3 |
|
public function __invoke() |
|
89
|
|
|
{ |
|
90
|
3 |
|
if ($this->isReady) { |
|
91
|
|
|
throw new \RuntimeException('Was already invoked!'); |
|
92
|
|
|
} else { |
|
93
|
3 |
|
foreach ($this->getCommands() as $list) { |
|
94
|
3 |
|
list($command, $arguments) = $list; |
|
95
|
3 |
|
call_user_func_array($command, $arguments); |
|
96
|
3 |
|
} |
|
97
|
3 |
|
$this->isReady = true; |
|
98
|
3 |
|
foreach ($this->onReadyCallbacks as $onReadyCallback) { |
|
99
|
3 |
|
$onReadyCallback(); |
|
100
|
3 |
|
} |
|
101
|
|
|
} |
|
102
|
3 |
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* @return bool |
|
106
|
|
|
*/ |
|
107
|
3 |
|
public function isReady() |
|
108
|
|
|
{ |
|
109
|
3 |
|
return $this->isReady; |
|
110
|
|
|
} |
|
111
|
|
|
} |
|
112
|
|
|
|