1 | <?php |
||
12 | class Subject implements SplSubject |
||
13 | { |
||
14 | /** |
||
15 | * @const ERR_OBSERVER_NOT_FOUND Exception code if the observer to detach |
||
16 | * has not been found. |
||
17 | */ |
||
18 | const ERR_OBSERVER_NOT_FOUND = 1314001; |
||
19 | |||
20 | /** |
||
21 | * @var \SplObserver[] $observers List of all observers |
||
22 | */ |
||
23 | protected $observers = []; |
||
24 | |||
25 | /** |
||
26 | * @var \stdClass[] $notifyHeap List of notify to send |
||
27 | */ |
||
28 | protected $notifyHeap = []; |
||
29 | |||
30 | /** |
||
31 | * @var string $action The current action to send to observers |
||
32 | */ |
||
33 | protected $action = ''; |
||
34 | |||
35 | /** |
||
36 | * @var mixed $context The current context to send to observers |
||
37 | */ |
||
38 | protected $context = null; |
||
39 | |||
40 | /** |
||
41 | * Return list of all observers |
||
42 | * |
||
43 | * @return \SplObserver[] |
||
44 | */ |
||
45 | public function getObservers() |
||
49 | |||
50 | /** |
||
51 | * Return list of all notify to send |
||
52 | * |
||
53 | * @return \stdClass[] |
||
54 | */ |
||
55 | public function getNotifyHeap() |
||
59 | |||
60 | /** |
||
61 | * Return the action |
||
62 | * |
||
63 | * @return string |
||
64 | */ |
||
65 | public function getAction() |
||
69 | |||
70 | /** |
||
71 | * Return the context |
||
72 | * |
||
73 | * @return mixed |
||
74 | */ |
||
75 | public function getContext() |
||
79 | |||
80 | /** |
||
81 | * Attach a new observer to the list |
||
82 | * |
||
83 | * @param \SplObserver $observer The new observer |
||
84 | * |
||
85 | * @return \BFW\Subject The current instance of this class |
||
86 | */ |
||
87 | public function attach(SplObserver $observer) |
||
93 | |||
94 | /** |
||
95 | * Detach a observer to the list |
||
96 | * |
||
97 | * @param \SplObserver $observer The observer instance to detach |
||
98 | * |
||
99 | * @return \BFW\Subject The current instance of this class |
||
100 | */ |
||
101 | public function detach(SplObserver $observer) |
||
116 | |||
117 | /** |
||
118 | * Send a notification to all observers |
||
119 | * |
||
120 | * @return \BFW\Subject The current instance of this class |
||
121 | */ |
||
122 | public function notify() |
||
130 | |||
131 | /** |
||
132 | * Read the notify heap list and send each notify into the list. |
||
133 | * |
||
134 | * @return $this |
||
135 | */ |
||
136 | public function readNotifyHeap() |
||
155 | |||
156 | /** |
||
157 | * Add a new notification to the list of notification to send. |
||
158 | * If there is only one notification into the list, it will be send now. |
||
159 | * Else, a notification is currently sent, so we wait it finish and the |
||
160 | * current notification will be sent. |
||
161 | * |
||
162 | * @param string $action The action to send |
||
163 | * @param notification $context (default null) The context to send |
||
164 | * |
||
165 | * @return \BFW\Subject The current instance of this class |
||
166 | */ |
||
167 | public function addNotification($action, $context = null) |
||
180 | } |
||
181 |