-
Notifications
You must be signed in to change notification settings - Fork 0
/
dispatcher.php
executable file
·182 lines (163 loc) · 4.41 KB
/
dispatcher.php
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
<?php
namespace PMVC\PlugIn\dispatcher;
use PMVC as p;
use PMVC\Event;
use SplObserver;
${_INIT_CONFIG}[_CLASS] = 'PMVC\PlugIn\dispatcher\dispatcher';
\PMVC\l(__DIR__ . '/src/Subject.php');
/**
* Const
*/
const PREP = '_prep';
const POST = '_post';
/**
* Base Observerable class
*/
class dispatcher extends p\PlugIn
{
/**
* Last config key.
*/
private $_lastConfigKey;
/**
* Private
* $observers an array of Observer objects to notify
*/
private $_subjects = [];
/**
* Notify will call all observer update() function
*
* @return void
*/
public function notify($state, $clean = null)
{
$isStop = $this->stop();
if (!$isStop) {
$state = strtolower($state);
$this->_notify($state . PREP, $clean);
$this->_notify($state, $clean);
$this->_notify($state . POST, $clean);
}
}
private function _notify($state, $clean = null)
{
if (isset($this->_subjects[$state])) {
$this->_subjects[$state]->setDoClean($clean);
$this->_subjects[$state]->notify();
}
}
/**
* Register the reference to an object object
*
* @param SplObserver $observer Trigger target.
* @param mixed $state Trigger type
* @param mixed $name user by attachBefore and attachAfter
*
* @return $this
*/
public function attach(SplObserver $observer, $state, $name = null)
{
$state = strtolower($state);
if (is_null($name)) {
$name = $state;
}
$name = strtolower($name);
if (!isset($this->_subjects[$state])) {
$this->_subjects[$state] = new Subject($name);
}
$this->_subjects[$state]->attach($observer);
return $this[\PMVC\THIS];
}
/**
* Attach Before.
*
* @return $this
*/
public function attachBefore(SplObserver $observer, $state)
{
return $this->attach($observer, $state . PREP, $state);
}
/**
* Attach After.
*
* @return $this
*/
public function attachAfter(SplObserver $observer, $state)
{
return $this->attach($observer, $state . POST, $state);
}
/**
* Deletes/deattaches an observer from the the object.
*
* @param object $observer
*/
public function detach(SplObserver $observer, $state = null)
{
if (is_null($state)) {
foreach ($this->_subjects as $subject) {
$subject->detach($observer);
}
} else {
$state = strtolower($state);
$states = [$state, $state . PREP, $state . POST];
foreach ($states as $state) {
if (isset($this->_subjects[$state])) {
$this->_subjects[$state]->detach($observer);
}
}
}
}
/**
* Contain.
*/
public function contains(SplObserver $observer, $state)
{
$subject = $this->_subjects[strtolower($state)];
return $subject->contains($observer);
}
/**
* Deletes/detaches every currently attached observer.
*/
function cleanObserver($state = null)
{
if (is_null($state)) {
foreach ($this->_subjects as $subject) {
$subject->removeAll();
}
} else {
$state = strtolower($state);
$this->_subjects[$state]->removeAll();
}
}
public function getOptionKey($key = null)
{
$new = Event\SET_CONFIG;
if (!is_null($key)) {
$new .= '_' . (string) $key;
}
return $new;
}
public function set($k, $v = null)
{
$this->_lastConfigKey = $k;
$this[$k] = $v;
$this->notify($k);
$this->notify($this->getOptionKey($v));
}
public function isSetOption($key)
{
if (Event\SET_CONFIG != $this->_lastConfigKey) {
return false;
}
$last_options = $this[Event\SET_CONFIG];
return \PMVC\hasKey($last_options, $key);
}
public function stop($bool = null)
{
static $isStop = false;
if (!is_null($bool)) {
$isStop = $bool;
}
return $isStop;
}
}