X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=demo%2Fserver%2Fserver.php;h=180a1088b8f0a43569c7dd4681a4ccc547f3f832;hb=393f882cb3a8a040bd7a0eb8da3c05cfacb8af99;hp=efc25bc90f7dc53c45f3b86c8d1c5a2c1bf2aa07;hpb=b74db4b586aab9fab355cce855776debea6e9819;p=plcapi.git diff --git a/demo/server/server.php b/demo/server/server.php index efc25bc..180a108 100644 --- a/demo/server/server.php +++ b/demo/server/server.php @@ -53,14 +53,28 @@ class xmlrpcServerMethodsContainer } /** - * A PHP version of the state-number server. Send me an integer and i'll sell you a state - * @param integer $num - * @return string - */ + * A PHP version of the state-number server. Send me an integer and i'll sell you a state. + * Used to test wrapping of PHP methods into xmlrpc methods. + * + * @param integer $num + * @return string + * @throws Exception + */ public static function findState($num) { return inner_findstate($num); } + + /** + * Returns an instance of stdClass. + * Used to test wrapping of PHP objects with class preservation + */ + public function returnObject() + { + $obj = new stdClass(); + $obj->hello = 'world'; + return $obj; + } } // a PHP version of the state-number server @@ -114,11 +128,13 @@ function findState($req) /** * Inner code of the state-number server. - * Used to test auto-registration of PHP functions as xmlrpc methods. + * Used to test wrapping of PHP functions into xmlrpc methods. * * @param integer $stateNo the state number * * @return string the name of the state (or error description) + * + * @throws Exception if state is not found */ function inner_findstate($stateNo) { @@ -128,25 +144,64 @@ function inner_findstate($stateNo) return $stateNames[$stateNo - 1]; } else { // not, there so complain - return "I don't have a state for the index '" . $stateNo . "'"; + throw new Exception("I don't have a state for the index '" . $stateNo . "'", PhpXmlRpc\PhpXmlRpc::$xmlrpcerruser); } } -$findStateClosure = function ($req) -{ - return findState($req); -}; - $wrapper = new PhpXmlRpc\Wrapper(); -$findstate2_sig = $wrapper->wrap_php_function('inner_findstate'); +$findstate2_sig = $wrapper->wrapPhpFunction('inner_findstate'); + +$findstate3_sig = $wrapper->wrapPhpFunction(array('xmlrpcServerMethodsContainer', 'findState')); -$findstate3_sig = $wrapper->wrap_php_function(array('xmlrpcServerMethodsContainer', 'findState')); +$obj = new xmlrpcServerMethodsContainer(); +$findstate4_sig = $wrapper->wrapPhpFunction(array($obj, 'findstate')); + +$findstate5_sig = $wrapper->wrapPhpFunction('xmlrpcServerMethodsContainer::findState', '', array('return_source' => true)); +eval($findstate5_sig['source']); + +$findstate6_sig = $wrapper->wrapPhpFunction('inner_findstate', '', array('return_source' => true)); +eval($findstate6_sig['source']); -$findstate5_sig = $wrapper->wrap_php_function('xmlrpcServerMethodsContainer::findState'); +$findstate7_sig = $wrapper->wrapPhpFunction(array('xmlrpcServerMethodsContainer', 'findState'), '', array('return_source' => true)); +eval($findstate7_sig['source']); $obj = new xmlrpcServerMethodsContainer(); -$findstate4_sig = $wrapper->wrap_php_function(array($obj, 'findstate')); +$findstate8_sig = $wrapper->wrapPhpFunction(array($obj, 'findstate'), '', array('return_source' => true)); +eval($findstate8_sig['source']); + +$findstate9_sig = $wrapper->wrapPhpFunction('xmlrpcServerMethodsContainer::findState', '', array('return_source' => true)); +eval($findstate9_sig['source']); + +$findstate10_sig = array( + "function" => function ($req) { return findState($req); }, + "signature" => $findstate_sig, + "docstring" => $findstate_doc, +); + +$findstate11_sig = $wrapper->wrapPhpFunction(function ($stateNo) { return inner_findstate($stateNo); }); + +$c = new xmlrpcServerMethodsContainer; +$moreSignatures = $wrapper->wrapPhpClass($c, array('prefix' => 'tests.', 'method_type' => 'all')); + +$returnObj_sig = $wrapper->wrapPhpFunction(array($c, 'returnObject'), '', array('encode_php_objs' => true)); + +// used to test signatures with NULL params +$findstate12_sig = array( + array(Value::$xmlrpcString, Value::$xmlrpcInt, Value::$xmlrpcNull), + array(Value::$xmlrpcString, Value::$xmlrpcNull, Value::$xmlrpcInt), +); + +function findStateWithNulls($req) +{ + $a = $req->getParam(0); + $b = $req->getParam(1); + + if ($a->scalartyp() == Value::$xmlrpcNull) + return new PhpXmlRpc\Response(new Value(inner_findstate($b->scalarval()))); + else + return new PhpXmlRpc\Response(new Value(inner_findstate($a->scalarval()))); +} $addtwo_sig = array(array(Value::$xmlrpcInt, Value::$xmlrpcInt, Value::$xmlrpcInt)); $addtwo_doc = 'Add two integers together and return the result'; @@ -302,8 +357,7 @@ function ageSorter($req) } } -// signature and instructions, place these in the dispatch -// map +// signature and instructions, place these in the dispatch map $mailsend_sig = array(array( Value::$xmlrpcBoolean, Value::$xmlrpcString, Value::$xmlrpcString, Value::$xmlrpcString, Value::$xmlrpcString, Value::$xmlrpcString, @@ -860,29 +914,31 @@ $signatures = array( "signature" => $i_whichToolkit_sig, "docstring" => $i_whichToolkit_doc, ), -); - -if ($findstate2_sig) { - $signatures['examples.php.getStateName'] = $findstate2_sig; -} -if ($findstate3_sig) { - $signatures['examples.php2.getStateName'] = $findstate3_sig; -} + 'tests.getStateName.2' => $findstate2_sig, + 'tests.getStateName.3' => $findstate3_sig, + 'tests.getStateName.4' => $findstate4_sig, + 'tests.getStateName.5' => $findstate5_sig, + 'tests.getStateName.6' => $findstate6_sig, + 'tests.getStateName.7' => $findstate7_sig, + 'tests.getStateName.8' => $findstate8_sig, + 'tests.getStateName.9' => $findstate9_sig, + 'tests.getStateName.10' => $findstate10_sig, + 'tests.getStateName.11' => $findstate11_sig, + + 'tests.getStateName.12' => array( + "function" => "findStateWithNulls", + "signature" => $findstate12_sig, + "docstring" => $findstate_doc, + ), -if ($findstate4_sig) { - $signatures['examples.php3.getStateName'] = $findstate4_sig; -} + 'tests.returnPhpObject' => $returnObj_sig, +); -if ($findstate5_sig) { - $signatures['examples.php4.getStateName'] = $findstate5_sig; -} +$signatures = array_merge($signatures, $moreSignatures); -$signatures['examples.php5.getStateName'] = array( - "function" => $findStateClosure, - "signature" => $findstate_sig, - "docstring" => $findstate_doc, -); +// enable support for the NULL extension +PhpXmlRpc\PhpXmlRpc::$xmlrpc_null_extension = true; $s = new PhpXmlRpc\Server($signatures, false); $s->setdebug(3);