|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace BFW; |
|
4
|
|
|
|
|
5
|
|
|
use \SplSubject; |
|
6
|
|
|
use \SplObserver; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Class to manage subject in observers systems |
|
10
|
|
|
*/ |
|
11
|
|
|
class Subjects implements SplSubject |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* @var \SplObserver[] $observers List of all observers |
|
15
|
|
|
*/ |
|
16
|
|
|
protected $observers = []; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @var \stdClass[] $notifyHeap List of notify to send |
|
20
|
|
|
*/ |
|
21
|
|
|
protected $notifyHeap = []; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @var string $action The current action to send to observers |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $action = ''; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @var mixed $context The current context to send to observers |
|
30
|
|
|
*/ |
|
31
|
|
|
protected $context = null; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Return list of all observers |
|
35
|
|
|
* |
|
36
|
|
|
* @return \SplObserver[] |
|
37
|
|
|
*/ |
|
38
|
|
|
public function getObservers() |
|
39
|
|
|
{ |
|
40
|
|
|
return $this->observers; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Return the action |
|
45
|
|
|
* |
|
46
|
|
|
* @return string |
|
47
|
|
|
*/ |
|
48
|
|
|
public function getAction() |
|
49
|
|
|
{ |
|
50
|
|
|
return $this->action; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Return the context |
|
55
|
|
|
* |
|
56
|
|
|
* @return mixed |
|
57
|
|
|
*/ |
|
58
|
|
|
public function getContext() |
|
59
|
|
|
{ |
|
60
|
|
|
return $this->context; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Attach a new observer to the list |
|
65
|
|
|
* |
|
66
|
|
|
* @param \SplObserver $observer The new observer |
|
67
|
|
|
* |
|
68
|
|
|
* @return \BFW\Subjects The current instance of this class |
|
69
|
|
|
*/ |
|
70
|
|
|
public function attach(SplObserver $observer) |
|
71
|
|
|
{ |
|
72
|
|
|
$this->observers[] = $observer; |
|
73
|
|
|
|
|
74
|
|
|
return $this; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Detach a observer to the list |
|
79
|
|
|
* |
|
80
|
|
|
* @param \SplObserver $observer The observer instance to detach |
|
81
|
|
|
* |
|
82
|
|
|
* @return \BFW\Subjects The current instance of this class |
|
83
|
|
|
*/ |
|
84
|
|
|
public function detach(SplObserver $observer) |
|
85
|
|
|
{ |
|
86
|
|
|
$key = array_search($observer, $this->observers, true); |
|
87
|
|
|
|
|
88
|
|
|
if ($key !== false) { |
|
89
|
|
|
unset($this->observers[$key]); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
return $this; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Send a notification to all observers |
|
97
|
|
|
* |
|
98
|
|
|
* @return \BFW\Subjects The current instance of this class |
|
99
|
|
|
*/ |
|
100
|
|
|
public function notify() |
|
101
|
|
|
{ |
|
102
|
|
|
foreach ($this->observers as $observer) { |
|
103
|
|
|
$observer->update($this); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
return $this; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
public function readNotifyHeap() |
|
110
|
|
|
{ |
|
111
|
|
|
foreach ($this->notifyHeap as $notifyIndex => $notifyDatas) { |
|
112
|
|
|
$this->action = $notifyDatas->action; |
|
113
|
|
|
$this->context = $notifyDatas->context; |
|
114
|
|
|
|
|
115
|
|
|
$this->notify(); |
|
116
|
|
|
|
|
117
|
|
|
//Remove the current notification from list |
|
118
|
|
|
unset($this->notifyHeap[$notifyIndex]); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
//Some new notifications has been added during the loop |
|
122
|
|
|
if (count($this->notifyHeap) > 0) { |
|
123
|
|
|
$this->readNotifyHeap(); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
return $this; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* Add a new notification to the list of notification to send. |
|
131
|
|
|
* If there is only one notification into the list, it will be send now. |
|
132
|
|
|
* Else, a notification is currently sent, so we wait it finish and the |
|
133
|
|
|
* current notification will be sent. |
|
134
|
|
|
* |
|
135
|
|
|
* @param string $action The action to send |
|
136
|
|
|
* @param notification $context (default null) The context to send |
|
137
|
|
|
* |
|
138
|
|
|
* @return \BFW\Subjects The current instance of this class |
|
139
|
|
|
*/ |
|
140
|
|
|
public function addNotification($action, $context = null) |
|
141
|
|
|
{ |
|
142
|
|
|
$this->notifyHeap[] = (object) [ |
|
143
|
|
|
'action' => $action, |
|
144
|
|
|
'context' => $context |
|
145
|
|
|
]; |
|
146
|
|
|
|
|
147
|
|
|
if (count($this->notifyHeap) === 1) { |
|
148
|
|
|
$this->readNotifyHeap(); |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
return $this; |
|
152
|
|
|
} |
|
153
|
|
|
} |
|
154
|
|
|
|