1414 *
1515 * @category Zend
1616 * @package Zend_Stdlib
17- * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
17+ * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
1818 * @license http://framework.zend.com/license/new-bsd New BSD License
1919 */
2020
2424namespace Zend \Stdlib ;
2525
2626/**
27- * SignalHandler
27+ * CallbackHandler
2828 *
29- * A handler for a signal , event, filterchain, etc. Abstracts PHP callbacks,
29+ * A handler for a event , event, filterchain, etc. Abstracts PHP callbacks,
3030 * primarily to allow for lazy-loading and ensuring availability of default
3131 * arguments (currying).
3232 *
3333 * @category Zend
3434 * @package Zend_Stdlib
35- * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
35+ * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
3636 * @license http://framework.zend.com/license/new-bsd New BSD License
3737 */
38- class SignalHandler
38+ class CallbackHandler
3939{
4040 /**
4141 * @var string|array PHP callback to invoke
4242 */
43- protected $ _callback ;
43+ protected $ callback ;
4444
4545 /**
46- * @var string Signal to which this handle is subscribed
46+ * @var string Event to which this handle is subscribed
4747 */
48- protected $ _signal ;
48+ protected $ event ;
4949
5050 /**
5151 * Until callback has been validated, mark as invalid
5252 * @var bool
5353 */
54- protected $ _isValidCallback = false ;
54+ protected $ isValidCallback = false ;
55+
56+ /**
57+ * Callback options, if any
58+ * @var array
59+ */
60+ protected $ options ;
5561
5662 /**
5763 * Constructor
5864 *
59- * @param string $signal Signal to which slot is subscribed
60- * @param string|object $context Function name, class name, or object instance
61- * @param string|null $handler Method name, if $context is a class or object
65+ * @param string $event Event to which slot is subscribed
66+ * @param string|array| object $callback PHP callback (first element may be )
67+ * @param array $options Options used by the callback handler (e.g., priority)
6268 * @return void
6369 */
64- public function __construct ($ signal , $ context , $ handler = null )
70+ public function __construct ($ event , $ callback , array $ options = array () )
6571 {
66- $ this ->_signal = $ signal ;
67-
68- if (null === $ handler ) {
69- $ this ->_callback = $ context ;
70- } else {
71- $ this ->_callback = array ($ context , $ handler );
72- }
72+ $ this ->event = $ event ;
73+ $ this ->callback = $ callback ;
74+ $ this ->options = $ options ;
7375 }
7476
7577 /**
76- * Get signal to which slot is subscribed
78+ * Get event to which handler is subscribed
7779 *
7880 * @return string
7981 */
80- public function getSignal ()
82+ public function getEvent ()
8183 {
82- return $ this ->_signal ;
84+ return $ this ->event ;
8385 }
8486
8587 /**
8688 * Retrieve registered callback
8789 *
8890 * @return Callback
89- * @throws InvalidCallbackException
91+ * @throws Exception\ InvalidCallbackException
9092 */
9193 public function getCallback ()
9294 {
93- if ($ this ->_isValidCallback ) {
94- return $ this ->_callback ;
95+ if ($ this ->isValidCallback ) {
96+ return $ this ->callback ;
9597 }
9698
97- $ callback = $ this ->_callback ;
99+ $ callback = $ this ->callback ;
98100 if (is_string ($ callback )) {
99- return $ this ->_validateStringCallback ($ callback );
101+ return $ this ->validateStringCallback ($ callback );
100102 }
101103 if (is_array ($ callback )) {
102- return $ this ->_validateArrayCallback ($ callback );
104+ return $ this ->validateArrayCallback ($ callback );
103105 }
104106 if (is_callable ($ callback )) {
105- $ this ->_isValidCallback = true ;
107+ $ this ->isValidCallback = true ;
106108 return $ callback ;
107109 }
108- throw new InvalidCallbackException ('Invalid callback provided; not callable ' );
110+ throw new Exception \ InvalidCallbackException ('Invalid callback provided; not callable ' );
109111 }
110112
111113 /**
@@ -120,6 +122,30 @@ public function call(array $args = array())
120122 return call_user_func_array ($ callback , $ args );
121123 }
122124
125+ /**
126+ * Get all callback options
127+ *
128+ * @return array
129+ */
130+ public function getOptions ()
131+ {
132+ return $ this ->options ;
133+ }
134+
135+ /**
136+ * Retrieve a single option
137+ *
138+ * @param string $name
139+ * @return mixed
140+ */
141+ public function getOption ($ name )
142+ {
143+ if (array_key_exists ($ name , $ this ->options )) {
144+ return $ this ->options [$ name ];
145+ }
146+ return null ;
147+ }
148+
123149 /**
124150 * Validate a string callback
125151 *
@@ -128,27 +154,27 @@ public function call(array $args = array())
128154 *
129155 * @param string $callback
130156 * @return Callback
131- * @throws InvalidCallbackException
157+ * @throws Exception\ InvalidCallbackException
132158 */
133- protected function _validateStringCallback ($ callback )
159+ protected function validateStringCallback ($ callback )
134160 {
135161 if (is_callable ($ callback )) {
136- $ this ->_isValidCallback = true ;
162+ $ this ->isValidCallback = true ;
137163 return $ callback ;
138164 }
139165
140166 if (!class_exists ($ callback )) {
141- throw new InvalidCallbackException ('Provided callback is not a function or a class ' );
167+ throw new Exception \ InvalidCallbackException ('Provided callback is not a function or a class ' );
142168 }
143169
144170 // check __invoke before instantiating
145171 if (!method_exists ($ callback , '__invoke ' )) {
146- throw new InvalidCallbackException ('Class provided as a callback does not implement __invoke ' );
172+ throw new Exception \ InvalidCallbackException ('Class provided as a callback does not implement __invoke ' );
147173 }
148174 $ object = new $ callback ();
149175
150- $ this ->_callback = $ object ;
151- $ this ->_isValidCallback = true ;
176+ $ this ->callback = $ object ;
177+ $ this ->isValidCallback = true ;
152178 return $ object ;
153179 }
154180
@@ -157,9 +183,9 @@ protected function _validateStringCallback($callback)
157183 *
158184 * @param array $callback
159185 * @return callback
160- * @throws InvalidCallbackException
186+ * @throws Exception\ InvalidCallbackException
161187 */
162- protected function _validateArrayCallback (array $ callback )
188+ protected function validateArrayCallback (array $ callback )
163189 {
164190 $ context = $ callback [0 ];
165191 $ method = $ callback [1 ];
@@ -168,51 +194,51 @@ protected function _validateArrayCallback(array $callback)
168194 // Dealing with a class/method callback, and class provided is a string classname
169195
170196 if (!class_exists ($ context )) {
171- throw new InvalidCallbackException ('Class provided in callback does not exist ' );
197+ throw new Exception \ InvalidCallbackException ('Class provided in callback does not exist ' );
172198 }
173199
174200 // We need to determine if we need to instantiate the class first
175201 $ r = new \ReflectionClass ($ context );
176202 if (!$ r ->hasMethod ($ method )) {
177203 // Explicit method does not exist
178204 if (!$ r ->hasMethod ('__callStatic ' ) && !$ r ->hasMethod ('__call ' )) {
179- throw new InvalidCallbackException ('Class provided in callback does not define the method requested ' );
205+ throw new Exception \ InvalidCallbackException ('Class provided in callback does not define the method requested ' );
180206 }
181207
182208 if ($ r ->hasMethod ('__callStatic ' )) {
183209 // We have a __callStatic defined, so the original callback is valid
184- $ this ->_isValidCallback = true ;
210+ $ this ->isValidCallback = true ;
185211 return $ callback ;
186212 }
187213
188214 // We have __call defined, so we need to instantiate the class
189215 // first, and redefine the callback
190216 $ object = new $ context ();
191- $ this ->_callback = array ($ object , $ method );
192- $ this ->_isValidCallback = true ;
193- return $ this ->_callback ;
217+ $ this ->callback = array ($ object , $ method );
218+ $ this ->isValidCallback = true ;
219+ return $ this ->callback ;
194220 }
195221
196222 // Explicit method exists
197223 $ rMethod = $ r ->getMethod ($ method );
198224 if ($ rMethod ->isStatic ()) {
199225 // Method is static, so original callback is fine
200- $ this ->_isValidCallback = true ;
226+ $ this ->isValidCallback = true ;
201227 return $ callback ;
202228 }
203229
204230 // Method is an instance method; instantiate object and redefine callback
205231 $ object = new $ context ();
206- $ this ->_callback = array ($ object , $ method );
207- $ this ->_isValidCallback = true ;
208- return $ this ->_callback ;
232+ $ this ->callback = array ($ object , $ method );
233+ $ this ->isValidCallback = true ;
234+ return $ this ->callback ;
209235 } elseif (is_callable ($ callback )) {
210236 // The
211- $ this ->_isValidCallback = true ;
237+ $ this ->isValidCallback = true ;
212238 return $ callback ;
213239 }
214240
215241
216- throw new InvalidCallbackException ('Method provided in callback does not exist in object ' );
242+ throw new Exception \ InvalidCallbackException ('Method provided in callback does not exist in object ' );
217243 }
218244}
0 commit comments