1
|
|
|
<?php |
2
|
|
|
namespace SmoothPhp\EventDispatcher; |
3
|
|
|
|
4
|
|
|
use SmoothPhp\Contracts\EventDispatcher\EventDispatcher; |
5
|
|
|
use SmoothPhp\Contracts\EventDispatcher\Projection; |
6
|
|
|
use SmoothPhp\Contracts\EventDispatcher\Subscriber; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class ProjectEnabledDispatcher |
10
|
|
|
* @package SmoothPhp\EventDispatcher |
11
|
|
|
* @author Simon Bennett <[email protected]> |
12
|
|
|
*/ |
13
|
|
|
final class ProjectEnabledDispatcher implements EventDispatcher |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var array |
17
|
|
|
*/ |
18
|
|
|
private $listeners = []; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @param string $eventName |
22
|
|
|
* @param array $arguments |
23
|
|
|
* @param bool $runProjectionsOnly |
24
|
|
|
*/ |
25
|
21 |
|
public function dispatch($eventName, array $arguments, $runProjectionsOnly = false) |
26
|
|
|
{ |
27
|
21 |
|
if (!isset($this->listeners[$eventName])) { |
28
|
3 |
|
return; |
29
|
|
|
} |
30
|
18 |
|
foreach ($this->getListenersInOrder($eventName) as $listener) { |
31
|
18 |
|
if ($this->listenerCanRun($runProjectionsOnly, $listener)) { |
32
|
18 |
|
call_user_func_array($listener, $arguments); |
33
|
18 |
|
} |
34
|
18 |
|
} |
35
|
18 |
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param string $eventName |
39
|
|
|
* @param callable $callable |
40
|
|
|
* @param int $priority |
41
|
|
|
*/ |
42
|
21 |
|
public function addListener($eventName, callable $callable, $priority = 0) |
43
|
|
|
{ |
44
|
21 |
|
$this->listeners[$eventName][$priority][] = $callable; |
45
|
21 |
|
unset($this->sorted[$eventName]); |
46
|
|
|
|
47
|
21 |
|
} |
48
|
|
|
|
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param Subscriber $subscriber |
52
|
|
|
* @return void |
53
|
|
|
*/ |
54
|
6 |
|
public function addSubscriber(Subscriber $subscriber) |
55
|
|
|
{ |
56
|
6 |
|
foreach ($subscriber->getSubscribedEvents() as $eventName => $params) { |
57
|
6 |
|
if (is_string($params)) { |
58
|
3 |
|
$this->addListener($eventName, array($subscriber, $params)); |
59
|
6 |
|
} elseif (is_string($params[0])) { |
60
|
6 |
|
$this->addListener($eventName, array($subscriber, $params[0]), isset($params[1]) ? $params[1] : 0); |
61
|
6 |
|
} else { |
62
|
3 |
|
foreach ($params as $listener) { |
63
|
3 |
|
$this->addListener($eventName, array($subscriber, $listener[0]), isset($listener[1]) ? $listener[1] : 0); |
64
|
3 |
|
} |
65
|
|
|
} |
66
|
6 |
|
} |
67
|
6 |
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @param $runProjectionsOnly |
71
|
|
|
* @param $listener |
72
|
|
|
* @return bool |
73
|
|
|
*/ |
74
|
18 |
|
protected function listenerCanRun($runProjectionsOnly, $listener) |
75
|
|
|
{ |
76
|
18 |
|
return !$runProjectionsOnly || (is_array($listener) && $listener[0] instanceof Projection); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @param $eventName |
81
|
|
|
* @return array |
82
|
|
|
*/ |
83
|
18 |
|
protected function getListenersInOrder($eventName) |
84
|
|
|
{ |
85
|
18 |
|
if (!isset($this->listeners[$eventName])) { |
86
|
|
|
return []; |
87
|
|
|
} |
88
|
18 |
|
if (!isset($this->sorted[$eventName])) { |
|
|
|
|
89
|
18 |
|
$this->sortListeners($eventName); |
90
|
18 |
|
} |
91
|
|
|
|
92
|
18 |
|
return $this->sorted[$eventName]; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Sorts the internal list of listeners for the given event by priority. |
97
|
|
|
* |
98
|
|
|
* @param string $eventName The name of the event. |
99
|
|
|
*/ |
100
|
18 |
|
private function sortListeners($eventName) |
101
|
|
|
{ |
102
|
18 |
|
$this->sorted[$eventName] = array(); |
103
|
|
|
|
104
|
18 |
|
krsort($this->listeners[$eventName]); |
105
|
18 |
|
$this->sorted[$eventName] = call_user_func_array('array_merge', $this->listeners[$eventName]); |
106
|
|
|
} |
107
|
|
|
} |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: