@@ -182,23 +182,28 @@ public function handle($request = false)
182
182
if (isset ($ request ['method ' ])) {
183
183
$ this ->_method = $ request ['method ' ];
184
184
if (isset ($ this ->_functions [$ this ->_method ])) {
185
- if ($ this ->_functions [$ this ->_method ] instanceof Zend_Server_Reflection_Function || $ this ->_functions [$ this ->_method ] instanceof Zend_Server_Reflection_Method && $ this ->_functions [$ this ->_method ]->isPublic ()) {
186
- $ request_keys = array_keys ($ request );
187
- array_walk ($ request_keys , array (__CLASS__ , "lowerCase " ));
188
- $ request = array_combine ($ request_keys , $ request );
189
-
190
- $ func_args = $ this ->_functions [$ this ->_method ]->getParameters ();
185
+ if ($ this ->_functions [$ this ->_method ] instanceof
186
+ Zend_Server_Reflection_Function
187
+ || $ this ->_functions [$ this ->_method ] instanceof
188
+ Zend_Server_Reflection_Method
189
+ && $ this ->_functions [$ this ->_method ]->isPublic ()
190
+ ) {
191
+ $ requestKeys = array_keys ($ request );
192
+ array_walk ($ requestKeys , array (__CLASS__ , "lowerCase " ));
193
+ $ request = array_combine ($ requestKeys , $ request );
194
+
195
+ $ funcArgs = $ this ->_functions [$ this ->_method ]->getParameters ();
191
196
192
197
// calling_args will be a zero-based array of the parameters
193
- $ calling_args = array ();
194
- $ missing_args = array ();
195
- foreach ($ func_args as $ i => $ arg ) {
198
+ $ callingArgs = array ();
199
+ $ missingArgs = array ();
200
+ foreach ($ funcArgs as $ i => $ arg ) {
196
201
if (isset ($ request [strtolower ($ arg ->getName ())])) {
197
- $ calling_args [$ i ] = $ request [strtolower ($ arg ->getName ())];
202
+ $ callingArgs [$ i ] = $ request [strtolower ($ arg ->getName ())];
198
203
} elseif ($ arg ->isOptional ()) {
199
- $ calling_args [$ i ] = $ arg ->getDefaultValue ();
204
+ $ callingArgs [$ i ] = $ arg ->getDefaultValue ();
200
205
} else {
201
- $ missing_args [] = $ arg ->getName ();
206
+ $ missingArgs [] = $ arg ->getName ();
202
207
}
203
208
}
204
209
@@ -207,57 +212,79 @@ public function handle($request = false)
207
212
if (substr ($ key , 0 , 3 ) == 'arg ' ) {
208
213
$ key = str_replace ('arg ' , '' , $ key );
209
214
$ anonymousArgs [$ key ] = $ value ;
210
- if (($ index = array_search ($ key , $ missing_args )) !== false ) {
211
- unset($ missing_args [$ index ]);
215
+ if (($ index = array_search ($ key , $ missingArgs )) !== false ) {
216
+ unset($ missingArgs [$ index ]);
212
217
}
213
218
}
214
219
}
215
220
216
221
// re-key the $anonymousArgs to be zero-based, and add in
217
222
// any values already set in calling_args (optional defaults)
218
223
ksort ($ anonymousArgs );
219
- $ calling_args = array_values ($ anonymousArgs ) + $ calling_args ;
224
+ $ callingArgs = array_values ($ anonymousArgs ) + $ callingArgs ;
220
225
221
226
// Sort arguments by key -- @see ZF-2279
222
- ksort ($ calling_args );
227
+ ksort ($ callingArgs );
223
228
224
229
$ result = false ;
225
- if (count ($ calling_args ) < count ($ func_args )) {
230
+ if (count ($ callingArgs ) < count ($ funcArgs )) {
226
231
require_once 'Zend/Rest/Server/Exception.php ' ;
227
- $ result = $ this ->fault (new Zend_Rest_Server_Exception ('Invalid Method Call to ' . $ this ->_method . '. Missing argument(s): ' . implode (', ' , $ missing_args ) . '. ' ), 400 );
232
+ $ result = $ this ->fault (
233
+ new Zend_Rest_Server_Exception (
234
+ 'Invalid Method Call to ' . $ this ->_method
235
+ . '. Missing argument(s): ' . implode (
236
+ ', ' , $ missingArgs
237
+ ) . '. '
238
+ ), 400
239
+ );
228
240
}
229
241
230
- if (!$ result && $ this ->_functions [$ this ->_method ] instanceof Zend_Server_Reflection_Method) {
242
+ if (!$ result && $ this ->_functions [$ this ->_method ] instanceof
243
+ Zend_Server_Reflection_Method
244
+ ) {
231
245
// Get class
232
246
$ class = $ this ->_functions [$ this ->_method ]->getDeclaringClass ()->getName ();
233
247
234
248
if ($ this ->_functions [$ this ->_method ]->isStatic ()) {
235
249
// for some reason, invokeArgs() does not work the same as
236
250
// invoke(), and expects the first argument to be an object.
237
251
// So, using a callback if the method is static.
238
- $ result = $ this ->_callStaticMethod ($ class , $ calling_args );
252
+ $ result = $ this ->_callStaticMethod (
253
+ $ class ,
254
+ $ callingArgs
255
+ );
239
256
} else {
240
257
// Object method
241
- $ result = $ this ->_callObjectMethod ($ class , $ calling_args );
258
+ $ result = $ this ->_callObjectMethod (
259
+ $ class ,
260
+ $ callingArgs
261
+ );
242
262
}
243
263
} elseif (!$ result ) {
244
264
try {
245
- $ result = call_user_func_array ($ this ->_functions [$ this ->_method ]->getName (), $ calling_args ); //$this->_functions[$this->_method]->invokeArgs($calling_args);
265
+ $ result = call_user_func_array (
266
+ $ this ->_functions [$ this ->_method ]->getName (),
267
+ $ callingArgs
268
+ );
246
269
} catch (Exception $ e ) {
247
270
$ result = $ this ->fault ($ e );
248
271
}
249
272
}
250
273
} else {
251
274
require_once "Zend/Rest/Server/Exception.php " ;
252
275
$ result = $ this ->fault (
253
- new Zend_Rest_Server_Exception ("Unknown Method ' $ this ->_method '. " ),
276
+ new Zend_Rest_Server_Exception (
277
+ "Unknown Method ' $ this ->_method '. "
278
+ ),
254
279
404
255
280
);
256
281
}
257
282
} else {
258
283
require_once "Zend/Rest/Server/Exception.php " ;
259
284
$ result = $ this ->fault (
260
- new Zend_Rest_Server_Exception ("Unknown Method ' $ this ->_method '. " ),
285
+ new Zend_Rest_Server_Exception (
286
+ "Unknown Method ' $ this ->_method '. "
287
+ ),
261
288
404
262
289
);
263
290
}
@@ -362,9 +389,11 @@ protected function _handleStruct($struct)
362
389
* @param DOMElement $parent
363
390
* @return void
364
391
*/
365
- protected function _structValue ($ struct , DOMDocument $ dom , DOMElement $ parent )
392
+ protected function _structValue (
393
+ $ struct , DOMDocument $ dom , DOMElement $ parent
394
+ )
366
395
{
367
- $ struct = (array ) $ struct ;
396
+ $ struct = (array )$ struct ;
368
397
369
398
foreach ($ struct as $ key => $ value ) {
370
399
if ($ value === false ) {
@@ -373,7 +402,7 @@ protected function _structValue($struct, DOMDocument $dom, DOMElement $parent)
373
402
$ value = 1 ;
374
403
}
375
404
376
- if (ctype_digit ((string ) $ key )) {
405
+ if (ctype_digit ((string )$ key )) {
377
406
$ key = 'key_ ' . $ key ;
378
407
}
379
408
@@ -487,13 +516,23 @@ public function fault($exception = null, $code = null)
487
516
488
517
if ($ exception instanceof Exception) {
489
518
$ element = $ dom ->createElement ('message ' );
490
- $ element ->appendChild ($ dom ->createTextNode ($ exception ->getMessage ()));
519
+ $ element ->appendChild (
520
+ $ dom ->createTextNode ($ exception ->getMessage ())
521
+ );
491
522
$ xmlResponse ->appendChild ($ element );
492
523
$ code = $ exception ->getCode ();
493
524
} elseif (($ exception !== null ) || 'rest ' == $ function ) {
494
- $ xmlResponse ->appendChild ($ dom ->createElement ('message ' , 'An unknown error occured. Please try again. ' ));
525
+ $ xmlResponse ->appendChild (
526
+ $ dom ->createElement (
527
+ 'message ' , 'An unknown error occured. Please try again. '
528
+ )
529
+ );
495
530
} else {
496
- $ xmlResponse ->appendChild ($ dom ->createElement ('message ' , 'Call to ' . $ method . ' failed. ' ));
531
+ $ xmlResponse ->appendChild (
532
+ $ dom ->createElement (
533
+ 'message ' , 'Call to ' . $ method . ' failed. '
534
+ )
535
+ );
497
536
}
498
537
499
538
$ xmlMethod ->appendChild ($ xmlResponse );
@@ -536,7 +575,9 @@ public function addFunction($function, $namespace = '')
536
575
$ this ->_functions [$ func ] = $ this ->_reflection ->reflectFunction ($ func );
537
576
} else {
538
577
require_once 'Zend/Rest/Server/Exception.php ' ;
539
- throw new Zend_Rest_Server_Exception ("Invalid Method Added to Service. " );
578
+ throw new Zend_Rest_Server_Exception (
579
+ "Invalid Method Added to Service. "
580
+ );
540
581
}
541
582
}
542
583
}
@@ -581,7 +622,13 @@ public function setPersistence($mode)
581
622
protected function _callStaticMethod ($ class , array $ args )
582
623
{
583
624
try {
584
- $ result = call_user_func_array (array ($ class , $ this ->_functions [$ this ->_method ]->getName ()), $ args );
625
+ $ result = call_user_func_array (
626
+ array (
627
+ $ class ,
628
+ $ this ->_functions [$ this ->_method ]->getName ()
629
+ ),
630
+ $ args
631
+ );
585
632
} catch (Exception $ e ) {
586
633
$ result = $ this ->fault ($ e );
587
634
}
@@ -606,14 +653,21 @@ protected function _callObjectMethod($class, array $args)
606
653
}
607
654
} catch (Exception $ e ) {
608
655
require_once 'Zend/Rest/Server/Exception.php ' ;
609
- throw new Zend_Rest_Server_Exception ('Error instantiating class ' . $ class .
610
- ' to invoke method ' . $ this ->_functions [$ this ->_method ]->getName () .
611
- ' ( ' . $ e ->getMessage () . ') ' ,
612
- 500 , $ e );
656
+ throw new Zend_Rest_Server_Exception (
657
+ 'Error instantiating class ' . $ class .
658
+ ' to invoke method '
659
+ . $ this ->_functions [$ this ->_method ]->getName () .
660
+ ' ( ' . $ e ->getMessage () . ') ' ,
661
+ 500 ,
662
+ $ e
663
+ );
613
664
}
614
665
615
666
try {
616
- $ result = $ this ->_functions [$ this ->_method ]->invokeArgs ($ object , $ args );
667
+ $ result = $ this ->_functions [$ this ->_method ]->invokeArgs (
668
+ $ object ,
669
+ $ args
670
+ );
617
671
} catch (Exception $ e ) {
618
672
$ result = $ this ->fault ($ e );
619
673
}
0 commit comments