|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace PEIP\Dispatcher; |
|
4
|
|
|
|
|
5
|
|
|
namespace PEIP\Dispatcher; |
|
6
|
|
|
|
|
7
|
|
|
/* |
|
8
|
|
|
* This file is part of the PEIP package. |
|
9
|
|
|
* (c) 2009-2016 Timo Michna <timomichna/yahoo.de> |
|
10
|
|
|
* |
|
11
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
12
|
|
|
* file that was distributed with this source code. |
|
13
|
|
|
*/ |
|
14
|
|
|
|
|
15
|
|
|
/* |
|
16
|
|
|
* IteratingDispatcher |
|
17
|
|
|
* Dispatcher implementation which notifies only one listener at a time |
|
18
|
|
|
* by iterating over registered listeners. |
|
19
|
|
|
* |
|
20
|
|
|
* @author Timo Michna <timomichna/yahoo.de> |
|
21
|
|
|
* @package PEIP |
|
22
|
|
|
* @subpackage dispatcher |
|
23
|
|
|
* @implements \PEIP\INF\Dispatcher\Dispatcher |
|
24
|
|
|
*/ |
|
25
|
|
|
|
|
26
|
|
|
use PEIP\Util\Test; |
|
27
|
|
|
|
|
28
|
|
|
class IteratingDispatcher extends \PEIP\ABS\Dispatcher\Dispatcher implements |
|
29
|
|
|
\PEIP\INF\Dispatcher\Dispatcher, |
|
30
|
|
|
\PEIP\INF\Base\Plugable |
|
31
|
|
|
{ |
|
32
|
|
|
protected $listeners; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* constructor. |
|
36
|
|
|
* |
|
37
|
|
|
* @param mixed array|ArrayAccess array with values to iterate over |
|
38
|
|
|
* |
|
39
|
|
|
* @return |
|
40
|
|
|
*/ |
|
41
|
|
|
public function __construct($array = []) |
|
42
|
|
|
{ |
|
43
|
|
|
$this->init($array); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
protected function init($array = []) |
|
47
|
|
|
{ |
|
48
|
|
|
$array = Test::assertArrayAccess($array) |
|
49
|
|
|
? $array |
|
50
|
|
|
: []; |
|
51
|
|
|
$this->listeners = new \ArrayIterator($array); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Registers a listener. |
|
56
|
|
|
* |
|
57
|
|
|
* @param mixed $listener the listener to register |
|
58
|
|
|
* |
|
59
|
|
|
* @return |
|
60
|
|
|
*/ |
|
61
|
|
|
public function connect($listener) |
|
62
|
|
|
{ |
|
63
|
|
|
$this->listeners[] = $listener; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Unregisters a listener. |
|
68
|
|
|
* |
|
69
|
|
|
* @param mixed $listener the listener to unregister |
|
70
|
|
|
* |
|
71
|
|
|
* @return |
|
72
|
|
|
*/ |
|
73
|
|
|
public function disconnect($listener) |
|
74
|
|
|
{ |
|
75
|
|
|
foreach ($this->listeners as $i => $callable) { |
|
76
|
|
|
if ($listener === $callable) { |
|
77
|
|
|
unset($this->listeners[$i]); |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Unregisters all listeners. |
|
84
|
|
|
* |
|
85
|
|
|
* @return |
|
86
|
|
|
*/ |
|
87
|
|
|
public function disconnectAll() |
|
88
|
|
|
{ |
|
89
|
|
|
$this->init(); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Check wether any listener is registered. |
|
94
|
|
|
* |
|
95
|
|
|
* @return bool wether any listener is registered |
|
96
|
|
|
*/ |
|
97
|
|
|
public function hasListeners() |
|
98
|
|
|
{ |
|
99
|
|
|
return (bool) $this->listeners->count(); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* Notifies one listener about a subject. |
|
104
|
|
|
* Iterates to the next registered listener any time method |
|
105
|
|
|
* is called - Rewinds if end is reached. |
|
106
|
|
|
* |
|
107
|
|
|
* @param mixed $subject the subject to notify about |
|
108
|
|
|
* |
|
109
|
|
|
* @return |
|
110
|
|
|
*/ |
|
111
|
|
|
public function notify($subject) |
|
112
|
|
|
{ |
|
113
|
|
|
$res = null; |
|
114
|
|
|
if ($this->hasListeners()) { |
|
115
|
|
|
if (!$this->listeners->valid()) { |
|
116
|
|
|
$this->listeners->rewind(); |
|
117
|
|
|
} |
|
118
|
|
|
$res = self::doNotifyOne($this->listeners->current(), $subject); |
|
119
|
|
|
$this->listeners->next(); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
return $res; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* Returns all registered listeners of the dispatcher. |
|
127
|
|
|
* |
|
128
|
|
|
* @return array registered listeners |
|
129
|
|
|
*/ |
|
130
|
|
|
public function getListeners() |
|
131
|
|
|
{ |
|
132
|
|
|
return $this->listeners->getArrayCopy(); |
|
133
|
|
|
} |
|
134
|
|
|
} |
|
135
|
|
|
|