1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PEIP\Dispatcher; |
4
|
|
|
|
5
|
|
|
use PEIP\Util\Reflection; |
6
|
|
|
use PEIP\Util\Test; |
7
|
|
|
|
8
|
|
|
class ClassDispatcher extends \PEIP\ABS\Dispatcher\MapDispatcher |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* Connects a listener to a given event-name. |
12
|
|
|
* |
13
|
|
|
* @param string $name name of the class |
14
|
|
|
* @param callable|PEIP\INF\Handler\Handler $listener listener to connect |
15
|
|
|
* |
16
|
|
|
* @return |
17
|
|
|
*/ |
18
|
|
|
public function connect($name, $listener) |
19
|
|
|
{ |
20
|
|
|
$name = is_object($name) ? get_class($name) : (string) $name; |
21
|
|
|
if (Test::assertClassOrInterfaceExists($name)) { |
22
|
|
|
parent::connect($name, $listener); |
23
|
|
|
} else { |
24
|
|
|
throw new \InvalidArgumentException($name.' is not an Class nor Interface'); |
25
|
|
|
} |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* notifies all listeners on a event on a subject. |
30
|
|
|
* |
31
|
|
|
* @param string $name name of the class |
32
|
|
|
* @param mixed $subject the subject |
33
|
|
|
* |
34
|
|
|
* @return |
35
|
|
|
*/ |
36
|
|
View Code Duplication |
public function notify($name, $subject) |
|
|
|
|
37
|
|
|
{ |
38
|
|
|
$res = false; |
39
|
|
|
foreach (Reflection::getImplementedClassesAndInterfaces($name) as $cls) { |
40
|
|
|
if (parent::hasListeners($cls)) { |
|
|
|
|
41
|
|
|
self::doNotify($this->getListeners($cls), $subject); |
42
|
|
|
$res = true; |
43
|
|
|
} |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
return $res; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* notifies all listeners on a event on a subject until one returns a boolean true value. |
51
|
|
|
* |
52
|
|
|
* @param string $name name of the event |
53
|
|
|
* @param mixed $subject the subject |
54
|
|
|
* |
55
|
|
|
* @return \PEIP\INF\Handler\Handler listener which returned a boolean true value |
56
|
|
|
*/ |
57
|
|
View Code Duplication |
public function notifyUntil($name, $subject) |
|
|
|
|
58
|
|
|
{ |
59
|
|
|
$res = null; |
60
|
|
|
foreach (Reflection::getImplementedClassesAndInterfaces($name) as $cls) { |
61
|
|
|
if (!$res && parent::hasListeners($cls)) { |
|
|
|
|
62
|
|
|
$res = self::doNotifyUntil($this->getListeners($cls), $subject); |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
return $res; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* notifies all listeners on a event on a subject. |
71
|
|
|
* |
72
|
|
|
* @param mixed $subject the subject |
73
|
|
|
* |
74
|
|
|
* @return |
75
|
|
|
*/ |
76
|
|
|
public function notifyOfInstance($subject) |
77
|
|
|
{ |
78
|
|
|
return $this->notify(get_class($subject), $subject); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Checks wether any listener is registered for a given event-name. |
83
|
|
|
* |
84
|
|
|
* @param string $name name of the event |
85
|
|
|
* |
86
|
|
|
* @return bool wether any listener is registered for event-name |
87
|
|
|
*/ |
88
|
|
|
public function hasListeners($name) |
89
|
|
|
{ |
90
|
|
|
foreach (Reflection::getImplementedClassesAndInterfaces($name) as $cls) { |
91
|
|
|
if (parent::hasListeners($cls)) { |
92
|
|
|
return true; |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
return false; |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.