-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEventManager.php
More file actions
165 lines (145 loc) · 4.88 KB
/
EventManager.php
File metadata and controls
165 lines (145 loc) · 4.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
<?php
/**
* AttwFramework
*
* @author Gabriel Jacinto <gamjj74@hotmail.com>
* @license MIT License
* @link http://attwframework.github.io
*/
namespace Attw\Event;
use Attw\Event\EventDispacherInterface;
use Attw\Event\Exception\EventException;
use Attw\Event\Event;
use \InvalidArgumentException;
/**
* Event manager, to add and throw events
*/
class EventManager implements EventManagerInterface
{
/**
* All listeners
*
* @var array
*/
private $events = array();
/**
* Add a listener
*
* @param string $name
* @param array|callable $listener
* @param integer $priority
* @throws \InvalidArgumentException case $name is not a string
* @throws \Attw\Event\Exception\EventException case $listener is string
* and params is not correctly setted
* @throws \Attw\Event\Exception\EventException case $listener does not exists
* @throws \Attw\Event\Exception\EventException case $listener does not implements
* Attw\Event\EventListenerInterface
* @throws \Attw\Event\Exception\EventException case method of listener does not exists
* @throws \InvalidArgumentException case $priority is not an integer
*/
public function listen($name, $listener, $priority = 0)
{
if (!is_string($name)) {
throw new InvalidArgumentException('Name must be integer');
}
if (is_string($listener)) {
$listenerParams = explode('.', $listener);
if (!isset($listenerParams[1])) {
throw new EventException('Choose the listener and the method to event');
}
if (!class_exists($listenerParams[0])) {
throw new EventException('Listener not found: ' . $listenerParams[0]);
}
$listenerClass = $listenerParams[0];
if (!(new $listenerClass() instanceof EventListenerInterface)) {
throw new EventException('Listener must implements Attw\Event\EventListenerInterface: ' . $listenerParams[0]);
}
if (!method_exists(new $listenerClass(), $listenerParams[1])) {
throw new EventException(sprintf('Method not found in listener %s: %s',
$listenerClass,
$listenerParams[1]));
}
}
if (!is_int($priority)) {
throw new InvalidArgumentException('Priority must be integer');
}
$this->events[ $name ][] = array(
'listener' => $listener,
'priority' => $priority
);
}
/**
* Remove a listener
*
* @param string $name
* @param callable|string $listener
*/
public function unlisten($name = null, $listener = null)
{
if (!is_null($name) && is_null($listener)) {
unset($this->events[ $name ]);
} elseif (is_null($name) && !is_null($listener)) {
foreach ($this->events as $eName => $eEvent) {
foreach ($eEvent as $key => $eventP) {
if ($listener == $eventP['listener']) {
unset($this->events[ $eName ][ $key ]);
}
}
}
} elseif (!is_null($name) && !is_null($listener)) {
foreach ($this->events as $eName => $eEvent) {
foreach ($eEvent as $key => $eventP) {
if ($eName == $name && $listener == $eventP['listener']) {
unset($this->events[ $eName ][ $key ]);
}
}
}
}
}
/**
* Throw a listener
*
* @param string $name
* @param \Attw\Event\Event
*/
public function trigger($name, Event $event)
{
if (isset( $this->events[$name])) {
$events = $this->getSorted($this->events[$name]);
foreach ($events as $eventP) {
$listener = $eventP['listener'];
if (is_callable( $listener ) ) {
$listener($event);
} elseif ( is_string($listener)) {
$listenerParams = explode('.', $listener);
$listener = $listenerParams[0];
$method = $listenerParams[1];
$listenerInstance = new $listener();
$listenerInstance->{$method}($event);
}
}
}
}
/**
* Sorts events by priority
*
* @param array $arr
* @return array
*/
private function getSorted(array $arr)
{
$events = array();
$arrToSort = array();
foreach ($arr as $key => $eventP) {
$arrToSort[$key] = $eventP['priority'];
}
arsort($arrToSort);
foreach ($arrToSort as $key => $priority) {
$events[$key] = array(
'listener' => $arr[ $key ]['listener'],
'priority' => $priority
);
}
return $events;
}
}