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
|
24 |
|
public function dispatch($eventName, array $arguments, $runProjectionsOnly = false) |
26
|
|
|
{ |
27
|
24 |
|
if (!isset($this->listeners[$eventName])) { |
28
|
3 |
|
return; |
29
|
|
|
} |
30
|
21 |
|
foreach ($this->getListenersInOrder($eventName) as $listener) { |
31
|
21 |
|
if ($this->listenerCanRun($runProjectionsOnly, $listener)) { |
32
|
21 |
|
call_user_func_array($listener, $arguments); |
33
|
21 |
|
} |
34
|
21 |
|
} |
35
|
21 |
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param string $eventName |
39
|
|
|
* @param callable $callable |
40
|
|
|
* @param int $priority |
41
|
|
|
*/ |
42
|
24 |
|
public function addListener($eventName, callable $callable, $priority = 0) |
43
|
|
|
{ |
44
|
24 |
|
$dotEventName = str_replace('\\', '.', $eventName); |
45
|
24 |
|
$this->listeners[$dotEventName][$priority][] = $callable; |
46
|
24 |
|
unset($this->sorted[$dotEventName]); |
47
|
|
|
|
48
|
24 |
|
} |
49
|
|
|
|
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @param Subscriber $subscriber |
53
|
|
|
* @return void |
54
|
|
|
*/ |
55
|
6 |
|
public function addSubscriber(Subscriber $subscriber) |
56
|
|
|
{ |
57
|
6 |
|
foreach ($subscriber->getSubscribedEvents() as $eventName => $params) { |
58
|
6 |
|
if (is_string($params)) { |
59
|
3 |
|
$this->addListener($eventName, array($subscriber, $params)); |
60
|
6 |
|
} elseif (is_string($params[0])) { |
61
|
6 |
|
$this->addListener($eventName, array($subscriber, $params[0]), isset($params[1]) ? $params[1] : 0); |
62
|
6 |
|
} else { |
63
|
3 |
|
foreach ($params as $listener) { |
64
|
3 |
|
$this->addListener($eventName, array($subscriber, $listener[0]), isset($listener[1]) ? $listener[1] : 0); |
65
|
3 |
|
} |
66
|
|
|
} |
67
|
6 |
|
} |
68
|
6 |
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @param $runProjectionsOnly |
72
|
|
|
* @param $listener |
73
|
|
|
* @return bool |
74
|
|
|
*/ |
75
|
21 |
|
protected function listenerCanRun($runProjectionsOnly, $listener) |
76
|
|
|
{ |
77
|
21 |
|
return !$runProjectionsOnly || (is_array($listener) && $listener[0] instanceof Projection); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @param $eventName |
82
|
|
|
* @return array |
83
|
|
|
*/ |
84
|
21 |
|
protected function getListenersInOrder($eventName) |
85
|
|
|
{ |
86
|
21 |
|
if (!isset($this->listeners[$eventName])) { |
87
|
|
|
return []; |
88
|
|
|
} |
89
|
21 |
|
if (!isset($this->sorted[$eventName])) { |
|
|
|
|
90
|
21 |
|
$this->sortListeners($eventName); |
91
|
21 |
|
} |
92
|
|
|
|
93
|
21 |
|
return $this->sorted[$eventName]; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Sorts the internal list of listeners for the given event by priority. |
98
|
|
|
* |
99
|
|
|
* @param string $eventName The name of the event. |
100
|
|
|
*/ |
101
|
21 |
|
private function sortListeners($eventName) |
102
|
|
|
{ |
103
|
21 |
|
$this->sorted[$eventName] = array(); |
104
|
|
|
|
105
|
21 |
|
krsort($this->listeners[$eventName]); |
106
|
21 |
|
$this->sorted[$eventName] = call_user_func_array('array_merge', $this->listeners[$eventName]); |
107
|
|
|
} |
108
|
|
|
} |
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: