PluginProbe
CSS & JavaScript Toolbox / 11.9
CSS & JavaScript Toolbox v11.9
trunk 0.3 0.8 10 10.1 11 11.2 11.3 11.4 11.5 11.6 11.7 11.8 11.9 11.9.1 12 12.0 12.0.1 12.0.3 12.0.4 12.0.5 12.0.6 12.0.7 6.0 6.0.11 All 60 releases
css-javascript-toolbox / framework / events / subjects / subject.subject.php

subject.subject.php in CSS & JavaScript Toolbox 11.9, at framework/events/subjects/subject.subject.php

237 lines 4.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 *
4 */
5
6 // Import dependencies.
7 require_once 'subject.interface.php';
8
9 /**
10 *
11 */
12 abstract class CJTEESubject implements CJTEEISubject, Countable, ArrayAccess {
13
14 /**
15 * put your comment there...
16 *
17 * @var mixed
18 */
19 protected $definition;
20
21 /**
22 * put your comment there...
23 *
24 * @var CJTIncludes
25 */
26 protected $includes;
27
28 /**
29 * put your comment there...
30 *
31 * @var mixed
32 */
33 protected $name;
34
35 /**
36 * put your comment there...
37 *
38 * @var mixed
39 */
40 protected $observers = array();
41
42 /**
43 * put your comment there...
44 *
45 * @var mixed
46 */
47 protected $result;
48
49 /**
50 * put your comment there...
51 *
52 * @var mixed
53 */
54 protected $target;
55
56 /**
57 * put your comment there...
58 *
59 */
60 public function count() {
61 return count($this->observers);
62 }
63
64 /**
65 * put your comment there...
66 *
67 * @param mixed $name
68 */
69 public function getDefinition($name) {
70 return isset($this->definition[$name]) ? $this->definition[$name] : [];
71 }
72
73 /**
74 * put your comment there...
75 *
76 */
77 public static function getInstance($name, $target, $definition, $includes) {
78 // Instantiate!
79 $subjectClass = $definition['subjectClass'];
80 $subject = new $subjectClass();
81 // Initialize subject!
82 $subject->init($name, $target, $definition, $includes);
83 return $subject;
84 }
85
86 /**
87 * put your comment there...
88 *
89 */
90 public function getName() {
91 return $this->name;
92 }
93
94 /**
95 * put your comment there...
96 *
97 * @param mixed $callback
98 */
99 public function getObserver($callback) {
100 // Make sure observer class file is included!
101 $this->includes->import($this->getDefinition('observerFile'));
102 // Instantiate observer!
103 $observer = call_user_func(array($this->getDefinition('observerClass'), 'getInstance'), $this, $callback);
104 return $observer;
105 }
106
107 /**
108 * put your comment there...
109 *
110 */
111 public function getResult() {
112 return $this->result;
113 }
114
115 /**
116 * put your comment there...
117 *
118 */
119 public function getTarget() {
120 return $this->target;
121 }
122
123 /**
124 * put your comment there...
125 *
126 */
127 protected function init($name, $target, $definition, $includes) {
128 $this->name = $name;
129 $this->target = $target;
130 $this->definition = $definition;
131 $this->includes = $includes;
132 return $this;
133 }
134
135 /**
136 * put your comment there...
137 *
138 * @param mixed $params
139 */
140 protected function initResultArray($params) {
141 // Add observer as the first parameter!
142 $this->result['params'] = array('observer' => null) + $params;
143 //--- Add target + subject!
144 //--- cancel for now !! array_unshift($this->result['params'], $this, $this->target);
145 $this->result['return'] = false;
146 }
147
148 /**
149 * put your comment there...
150 *
151 * @param mixed $callback
152 */
153 public function offsetExists($callback) {
154 $key = null;
155
156 // Check existance!
157 return isset($this->observers[$key]);
158 }
159
160 /**
161 * put your comment there...
162 *
163 * @param mixed $callback
164 */
165 public function offsetGet($callback) {
166 $key = null;
167
168 // Return observer!
169 return $this->observers[$key];
170 }
171
172 /**
173 * put your comment there...
174 *
175 * @param mixed $key
176 * @param mixed $callback
177 */
178 public function offsetSet($key, $callback) {
179 $observer = $this->getObserver($callback);
180 $key = $observer->getKey();
181 // Add to observers list!
182 $this->observers[$key] = $observer;
183 }
184
185 /**
186 * put your comment there...
187 *
188 * @param mixed $callback
189 */
190 public function offsetUnset($callback) {
191 $observer = $this->getObserver($callback);
192 $key = $observer->getKey();
193 // Just remove from observers list!
194 unset($this->observers[$key]);
195 }
196
197 /**
198 * put your comment there...
199 *
200 */
201 protected function prepareResultParameters() {
202 // No changes should happen for the general observer parameters!
203 return;
204 }
205
206 /**
207 * put your comment there...
208 *
209 * @param mixed $observer
210 */
211 protected function processFilter($observer) {
212 return true; // Always call observer!
213 }
214
215 /**
216 * put your comment there...
217 *
218 */
219 public function trigger() {
220 // Read parameters.
221 $this->initResultArray(func_get_args());
222 reset($this->observers);
223 while ($observer = current($this->observers)) {
224 if ($this->processFilter($observer)) {
225 // Pass observer referecne along with user params!!
226 $this->result['params']['observer'] = $observer;
227 $this->result['return'] = call_user_func_array( [ $observer, 'trigger' ], array_values( $this->result['params'] ) );
228 // Prepare parameters based on the previous call result!
229 $this->prepareResultParameters();
230 }
231 next($this->observers);
232 }
233 // Return last result!
234 return $this->result['return'];
235 }
236
237 } // End class.