Add a test for registering anonymous functions in the server dispatch map
authorgggeek <giunta.gaetano@gmail.com>
Sat, 2 May 2015 16:45:12 +0000 (17:45 +0100)
committergggeek <giunta.gaetano@gmail.com>
Sat, 2 May 2015 16:45:12 +0000 (17:45 +0100)
demo/server/server.php
src/Server.php
tests/3LocalhostTest.php

index aa604b8..efc25bc 100644 (file)
@@ -38,7 +38,7 @@ class xmlrpcServerMethodsContainer
     /**
      * Method used to test logging of php warnings generated by user functions.
      */
-    public function phpWarningGenerator($m)
+    public function phpWarningGenerator($req)
     {
         $a = $undefinedVariable; // this triggers a warning in E_ALL mode, since $undefinedVariable is undefined
         return new PhpXmlRpc\Response(new Value(1, 'boolean'));
@@ -47,19 +47,19 @@ class xmlrpcServerMethodsContainer
     /**
      * Method used to test catching of exceptions in the server.
      */
-    public function exceptionGenerator($m)
+    public function exceptionGenerator($req)
     {
         throw new Exception("it's just a test", 1);
     }
 
     /**
     * A PHP version of the state-number server. Send me an integer and i'll sell you a state
-    * @param integer $s
+    * @param integer $num
     * @return string
     */
-    public static function findState($s)
+    public static function findState($num)
     {
-        return inner_findstate($s);
+        return inner_findstate($num);
     }
 }
 
@@ -83,13 +83,13 @@ $findstate_doc = 'When passed an integer between 1 and 51 returns the
 name of a US state, where the integer is the index of that state name
 in an alphabetic order.';
 
-function findState($m)
+function findState($req)
 {
     global $stateNames;
 
     $err = "";
     // get the first param
-    $sno = $m->getParam(0);
+    $sno = $req->getParam(0);
 
     // param must be there and of the correct type: server object does the validation for us
 
@@ -132,6 +132,11 @@ function inner_findstate($stateNo)
     }
 }
 
+$findStateClosure = function ($req)
+{
+    return findState($req);
+};
+
 $wrapper = new PhpXmlRpc\Wrapper();
 
 $findstate2_sig = $wrapper->wrap_php_function('inner_findstate');
@@ -145,58 +150,58 @@ $findstate4_sig = $wrapper->wrap_php_function(array($obj, 'findstate'));
 
 $addtwo_sig = array(array(Value::$xmlrpcInt, Value::$xmlrpcInt, Value::$xmlrpcInt));
 $addtwo_doc = 'Add two integers together and return the result';
-function addTwo($m)
+function addTwo($req)
 {
-    $s = $m->getParam(0);
-    $t = $m->getParam(1);
+    $s = $req->getParam(0);
+    $t = $req->getParam(1);
 
     return new PhpXmlRpc\Response(new Value($s->scalarval() + $t->scalarval(), "int"));
 }
 
 $addtwodouble_sig = array(array(Value::$xmlrpcDouble, Value::$xmlrpcDouble, Value::$xmlrpcDouble));
 $addtwodouble_doc = 'Add two doubles together and return the result';
-function addTwoDouble($m)
+function addTwoDouble($req)
 {
-    $s = $m->getParam(0);
-    $t = $m->getParam(1);
+    $s = $req->getParam(0);
+    $t = $req->getParam(1);
 
     return new PhpXmlRpc\Response(new Value($s->scalarval() + $t->scalarval(), "double"));
 }
 
 $stringecho_sig = array(array(Value::$xmlrpcString, Value::$xmlrpcString));
 $stringecho_doc = 'Accepts a string parameter, returns the string.';
-function stringEcho($m)
+function stringEcho($req)
 {
     // just sends back a string
-    return new PhpXmlRpc\Response(new Value($m->getParam(0)->scalarval()));
+    return new PhpXmlRpc\Response(new Value($req->getParam(0)->scalarval()));
 }
 
 $echoback_sig = array(array(Value::$xmlrpcString, Value::$xmlrpcString));
 $echoback_doc = 'Accepts a string parameter, returns the entire incoming payload';
-function echoBack($m)
+function echoBack($req)
 {
     // just sends back a string with what i got sent to me, just escaped, that's all
-    $s = "I got the following message:\n" . $m->serialize();
+    $s = "I got the following message:\n" . $req->serialize();
 
     return new PhpXmlRpc\Response(new Value($s));
 }
 
 $echosixtyfour_sig = array(array(Value::$xmlrpcString, Value::$xmlrpcBase64));
 $echosixtyfour_doc = 'Accepts a base64 parameter and returns it decoded as a string';
-function echoSixtyFour($m)
+function echoSixtyFour($req)
 {
     // Accepts an encoded value, but sends it back as a normal string.
     // This is to test that base64 encoding is working as expected
-    $incoming = $m->getParam(0);
+    $incoming = $req->getParam(0);
 
     return new PhpXmlRpc\Response(new Value($incoming->scalarval(), "string"));
 }
 
 $bitflipper_sig = array(array(Value::$xmlrpcArray, Value::$xmlrpcArray));
 $bitflipper_doc = 'Accepts an array of booleans, and returns them inverted';
-function bitFlipper($m)
+function bitFlipper($req)
 {
-    $v = $m->getParam(0);
+    $v = $req->getParam(0);
     $sz = $v->arraysize();
     $rv = new Value(array(), Value::$xmlrpcArray);
 
@@ -248,13 +253,13 @@ $agesorter_doc = 'Send this method an array of [string, int] structs, eg:
 </pre>
 And the array will be returned with the entries sorted by their numbers.
 ';
-function ageSorter($m)
+function ageSorter($req)
 {
     global $agesorter_arr, $s;
 
     PhpXmlRpc\Server::xmlrpc_debugmsg("Entering 'agesorter'");
     // get the parameter
-    $sno = $m->getParam(0);
+    $sno = $req->getParam(0);
     // error string for [if|when] things go wrong
     $err = "";
     // create the output value
@@ -315,17 +320,17 @@ mimetype, a string, is a standard MIME type, for example, text/plain.
 // WARNING; this functionality depends on the sendmail -t option
 // it may not work with Windows machines properly; particularly
 // the Bcc option. Sneak on your friends at your own risk!
-function mailSend($m)
+function mailSend($req)
 {
     $err = "";
 
-    $mTo = $m->getParam(0);
-    $mSub = $m->getParam(1);
-    $mBody = $m->getParam(2);
-    $mFrom = $m->getParam(3);
-    $mCc = $m->getParam(4);
-    $mBcc = $m->getParam(5);
-    $mMime = $m->getParam(6);
+    $mTo = $req->getParam(0);
+    $mSub = $req->getParam(1);
+    $mBody = $req->getParam(2);
+    $mFrom = $req->getParam(3);
+    $mCc = $req->getParam(4);
+    $mBcc = $req->getParam(5);
+    $mMime = $req->getParam(6);
 
     if ($mTo->scalarval() == "") {
         $err = "Error, no 'To' field specified";
@@ -335,25 +340,25 @@ function mailSend($m)
         $err = "Error, no 'From' field specified";
     }
 
-    $msghdr = "From: " . $mFrom->scalarval() . "\n";
-    $msghdr .= "To: " . $mTo->scalarval() . "\n";
+    $msgHdr = "From: " . $mFrom->scalarval() . "\n";
+    $msgHdr .= "To: " . $mTo->scalarval() . "\n";
 
     if ($mCc->scalarval() != "") {
-        $msghdr .= "Cc: " . $mCc->scalarval() . "\n";
+        $msgHdr .= "Cc: " . $mCc->scalarval() . "\n";
     }
     if ($mBcc->scalarval() != "") {
-        $msghdr .= "Bcc: " . $mBcc->scalarval() . "\n";
+        $msgHdr .= "Bcc: " . $mBcc->scalarval() . "\n";
     }
     if ($mMime->scalarval() != "") {
-        $msghdr .= "Content-type: " . $mMime->scalarval() . "\n";
+        $msgHdr .= "Content-type: " . $mMime->scalarval() . "\n";
     }
-    $msghdr .= "X-Mailer: XML-RPC for PHP mailer 1.0";
+    $msgHdr .= "X-Mailer: XML-RPC for PHP mailer 1.0";
 
     if ($err == "") {
         if (!mail("",
             $mSub->scalarval(),
             $mBody->scalarval(),
-            $msghdr)
+            $msgHdr)
         ) {
             $err = "Error, could not send the mail.";
         }
@@ -368,7 +373,7 @@ function mailSend($m)
 
 $getallheaders_sig = array(array(Value::$xmlrpcStruct));
 $getallheaders_doc = 'Returns a struct containing all the HTTP headers received with the request. Provides limited functionality with IIS';
-function getallheaders_xmlrpc($m)
+function getAllHeaders_xmlrpc($req)
 {
     $encoder = new PhpXmlRpc\Encoder();
 
@@ -390,10 +395,10 @@ function getallheaders_xmlrpc($m)
 
 $setcookies_sig = array(array(Value::$xmlrpcInt, Value::$xmlrpcStruct));
 $setcookies_doc = 'Sends to client a response containing a single \'1\' digit, and sets to it http cookies as received in the request (array of structs describing a cookie)';
-function setCookies($m)
+function setCookies($req)
 {
     $encoder = new PhpXmlRpc\Encoder();
-    $m = $m->getParam(0);
+    $m = $req->getParam(0);
     while (list($name, $value) = $m->structeach()) {
         $cookieDesc = $encoder->decode($value);
         setcookie($name, @$cookieDesc['value'], @$cookieDesc['expires'], @$cookieDesc['path'], @$cookieDesc['domain'], @$cookieDesc['secure']);
@@ -404,7 +409,7 @@ function setCookies($m)
 
 $getcookies_sig = array(array(Value::$xmlrpcStruct));
 $getcookies_doc = 'Sends to client a response containing all http cookies as received in the request (as struct)';
-function getCookies($m)
+function getCookies($req)
 {
     $encoder = new PhpXmlRpc\Encoder();
     return new PhpXmlRpc\Response($encoder->encode($_COOKIE));
@@ -412,9 +417,9 @@ function getCookies($m)
 
 $v1_arrayOfStructs_sig = array(array(Value::$xmlrpcInt, Value::$xmlrpcArray));
 $v1_arrayOfStructs_doc = 'This handler takes a single parameter, an array of structs, each of which contains at least three elements named moe, larry and curly, all <i4>s. Your handler must add all the struct elements named curly and return the result.';
-function v1_arrayOfStructs($m)
+function v1_arrayOfStructs($req)
 {
-    $sno = $m->getParam(0);
+    $sno = $req->getParam(0);
     $numCurly = 0;
     for ($i = 0; $i < $sno->arraysize(); $i++) {
         $str = $sno->arraymem($i);
@@ -431,9 +436,9 @@ function v1_arrayOfStructs($m)
 
 $v1_easyStruct_sig = array(array(Value::$xmlrpcInt, Value::$xmlrpcStruct));
 $v1_easyStruct_doc = 'This handler takes a single parameter, a struct, containing at least three elements named moe, larry and curly, all &lt;i4&gt;s. Your handler must add the three numbers and return the result.';
-function v1_easyStruct($m)
+function v1_easyStruct($req)
 {
-    $sno = $m->getParam(0);
+    $sno = $req->getParam(0);
     $moe = $sno->structmem("moe");
     $larry = $sno->structmem("larry");
     $curly = $sno->structmem("curly");
@@ -444,9 +449,9 @@ function v1_easyStruct($m)
 
 $v1_echoStruct_sig = array(array(Value::$xmlrpcStruct, Value::$xmlrpcStruct));
 $v1_echoStruct_doc = 'This handler takes a single parameter, a struct. Your handler must return the struct.';
-function v1_echoStruct($m)
+function v1_echoStruct($req)
 {
-    $sno = $m->getParam(0);
+    $sno = $req->getParam(0);
 
     return new PhpXmlRpc\Response($sno);
 }
@@ -457,24 +462,24 @@ $v1_manyTypes_sig = array(array(
     Value::$xmlrpcBase64,
 ));
 $v1_manyTypes_doc = 'This handler takes six parameters, and returns an array containing all the parameters.';
-function v1_manyTypes($m)
+function v1_manyTypes($req)
 {
     return new PhpXmlRpc\Response(new Value(array(
-        $m->getParam(0),
-        $m->getParam(1),
-        $m->getParam(2),
-        $m->getParam(3),
-        $m->getParam(4),
-        $m->getParam(5),),
+        $req->getParam(0),
+        $req->getParam(1),
+        $req->getParam(2),
+        $req->getParam(3),
+        $req->getParam(4),
+        $req->getParam(5),),
         "array"
     ));
 }
 
 $v1_moderateSizeArrayCheck_sig = array(array(Value::$xmlrpcString, Value::$xmlrpcArray));
 $v1_moderateSizeArrayCheck_doc = 'This handler takes a single parameter, which is an array containing between 100 and 200 elements. Each of the items is a string, your handler must return a string containing the concatenated text of the first and last elements.';
-function v1_moderateSizeArrayCheck($m)
+function v1_moderateSizeArrayCheck($req)
 {
-    $ar = $m->getParam(0);
+    $ar = $req->getParam(0);
     $sz = $ar->arraysize();
     $first = $ar->arraymem(0);
     $last = $ar->arraymem($sz - 1);
@@ -485,9 +490,9 @@ function v1_moderateSizeArrayCheck($m)
 
 $v1_simpleStructReturn_sig = array(array(Value::$xmlrpcStruct, Value::$xmlrpcInt));
 $v1_simpleStructReturn_doc = 'This handler takes one parameter, and returns a struct containing three elements, times10, times100 and times1000, the result of multiplying the number by 10, 100 and 1000.';
-function v1_simpleStructReturn($m)
+function v1_simpleStructReturn($req)
 {
-    $sno = $m->getParam(0);
+    $sno = $req->getParam(0);
     $v = $sno->scalarval();
 
     return new PhpXmlRpc\Response(new Value(array(
@@ -500,9 +505,9 @@ function v1_simpleStructReturn($m)
 
 $v1_nestedStruct_sig = array(array(Value::$xmlrpcInt, Value::$xmlrpcStruct));
 $v1_nestedStruct_doc = 'This handler takes a single parameter, a struct, that models a daily calendar. At the top level, there is one struct for each year. Each year is broken down into months, and months into days. Most of the days are empty in the struct you receive, but the entry for April 1, 2000 contains a least three elements named moe, larry and curly, all &lt;i4&gt;s. Your handler must add the three numbers and return the result.';
-function v1_nestedStruct($m)
+function v1_nestedStruct($req)
 {
-    $sno = $m->getParam(0);
+    $sno = $req->getParam(0);
 
     $twoK = $sno->structmem("2000");
     $april = $twoK->structmem("04");
@@ -516,9 +521,9 @@ function v1_nestedStruct($m)
 
 $v1_countTheEntities_sig = array(array(Value::$xmlrpcStruct, Value::$xmlrpcString));
 $v1_countTheEntities_doc = 'This handler takes a single parameter, a string, that contains any number of predefined entities, namely &lt;, &gt;, &amp; \' and ".<BR>Your handler must return a struct that contains five fields, all numbers: ctLeftAngleBrackets, ctRightAngleBrackets, ctAmpersands, ctApostrophes, ctQuotes.';
-function v1_countTheEntities($m)
+function v1_countTheEntities($req)
 {
-    $sno = $m->getParam(0);
+    $sno = $req->getParam(0);
     $str = $sno->scalarval();
     $gt = 0;
     $lt = 0;
@@ -594,72 +599,72 @@ $i_echoBase64_doc = "Echoes base64.";
 $i_echoDate_sig = array(array(Value::$xmlrpcDateTime, Value::$xmlrpcDateTime));
 $i_echoDate_doc = "Echoes dateTime.";
 
-function i_echoParam($m)
+function i_echoParam($req)
 {
-    $s = $m->getParam(0);
+    $s = $req->getParam(0);
 
     return new PhpXmlRpc\Response($s);
 }
 
-function i_echoString($m)
+function i_echoString($req)
 {
-    return i_echoParam($m);
+    return i_echoParam($req);
 }
 
-function i_echoInteger($m)
+function i_echoInteger($req)
 {
-    return i_echoParam($m);
+    return i_echoParam($req);
 }
 
-function i_echoFloat($m)
+function i_echoFloat($req)
 {
-    return i_echoParam($m);
+    return i_echoParam($req);
 }
 
-function i_echoStruct($m)
+function i_echoStruct($req)
 {
-    return i_echoParam($m);
+    return i_echoParam($req);
 }
 
-function i_echoStringArray($m)
+function i_echoStringArray($req)
 {
-    return i_echoParam($m);
+    return i_echoParam($req);
 }
 
-function i_echoIntegerArray($m)
+function i_echoIntegerArray($req)
 {
-    return i_echoParam($m);
+    return i_echoParam($req);
 }
 
-function i_echoFloatArray($m)
+function i_echoFloatArray($req)
 {
-    return i_echoParam($m);
+    return i_echoParam($req);
 }
 
-function i_echoStructArray($m)
+function i_echoStructArray($req)
 {
-    return i_echoParam($m);
+    return i_echoParam($req);
 }
 
-function i_echoValue($m)
+function i_echoValue($req)
 {
-    return i_echoParam($m);
+    return i_echoParam($req);
 }
 
-function i_echoBase64($m)
+function i_echoBase64($req)
 {
-    return i_echoParam($m);
+    return i_echoParam($req);
 }
 
-function i_echoDate($m)
+function i_echoDate($req)
 {
-    return i_echoParam($m);
+    return i_echoParam($req);
 }
 
 $i_whichToolkit_sig = array(array(Value::$xmlrpcStruct));
 $i_whichToolkit_doc = "Returns a struct containing the following strings: toolkitDocsUrl, toolkitName, toolkitVersion, toolkitOperatingSystem.";
 
-function i_whichToolkit($m)
+function i_whichToolkit($req)
 {
     global $SERVER_SOFTWARE;
     $ret = array(
@@ -736,7 +741,7 @@ $signatures = array(
         "docstring" => $stringecho_doc,
     ),*/
     "examples.getallheaders" => array(
-        "function" => 'getallheaders_xmlrpc',
+        "function" => 'getAllHeaders_xmlrpc',
         "signature" => $getallheaders_sig,
         "docstring" => $getallheaders_doc,
     ),
@@ -873,6 +878,12 @@ if ($findstate5_sig) {
     $signatures['examples.php4.getStateName'] = $findstate5_sig;
 }
 
+$signatures['examples.php5.getStateName'] = array(
+    "function" => $findStateClosure,
+    "signature" => $findstate_sig,
+    "docstring" => $findstate_doc,
+);
+
 $s = new PhpXmlRpc\Server($signatures, false);
 $s->setdebug(3);
 $s->compress_response = true;
index 2280847..2eca096 100644 (file)
@@ -11,59 +11,62 @@ class Server
      * Array defining php functions exposed as xmlrpc methods by this server.
      */
     protected $dmap = array();
-    /*
-    * Defines how functions in dmap will be invoked: either using an xmlrpc msg object
-    * or plain php values.
-    * valid strings are 'xmlrpcvals', 'phpvals' or 'epivals'
-    */
+    /**
+     * Defines how functions in dmap will be invoked: either using an xmlrpc msg object
+     * or plain php values.
+     * valid strings are 'xmlrpcvals', 'phpvals' or 'epivals'
+     */
     public $functions_parameters_type = 'xmlrpcvals';
-    /*
-    * Option used for fine-tuning the encoding the php values returned from
-    * functions registered in the dispatch map when the functions_parameters_types
-    * member is set to 'phpvals'
-    * @see php_xmlrpc_encode for a list of values
-    */
+    /**
+     * Option used for fine-tuning the encoding the php values returned from
+     * functions registered in the dispatch map when the functions_parameters_types
+     * member is set to 'phpvals'
+     * @see php_xmlrpc_encode for a list of values
+     */
     public $phpvals_encoding_options = array('auto_dates');
-    /// controls whether the server is going to echo debugging messages back to the client as comments in response body. valid values: 0,1,2,3
+    /**
+     * Controls whether the server is going to echo debugging messages back to the client as comments in response body.
+     * Valid values: 0,1,2,3
+     */
     public $debug = 1;
-    /*
-    * Controls behaviour of server when invoked user function throws an exception:
-    * 0 = catch it and return an 'internal error' xmlrpc response (default)
-    * 1 = catch it and return an xmlrpc response with the error corresponding to the exception
-    * 2 = allow the exception to float to the upper layers
-    */
+    /**
+     * Controls behaviour of server when invoked user function throws an exception:
+     * 0 = catch it and return an 'internal error' xmlrpc response (default)
+     * 1 = catch it and return an xmlrpc response with the error corresponding to the exception
+     * 2 = allow the exception to float to the upper layers
+     */
     public $exception_handling = 0;
-    /*
-    * When set to true, it will enable HTTP compression of the response, in case
-    * the client has declared its support for compression in the request.
-    */
+    /**
+     * When set to true, it will enable HTTP compression of the response, in case
+     * the client has declared its support for compression in the request.
+     */
     public $compress_response = false;
-    /*
-    * List of http compression methods accepted by the server for requests.
-    * NB: PHP supports deflate, gzip compressions out of the box if compiled w. zlib
-    */
+    /**
+     * List of http compression methods accepted by the server for requests.
+     * NB: PHP supports deflate, gzip compressions out of the box if compiled w. zlib
+     */
     public $accepted_compression = array();
     /// shall we serve calls to system.* methods?
     public $allow_system_funcs = true;
     /// list of charset encodings natively accepted for requests
     public $accepted_charset_encodings = array();
-    /*
-    * charset encoding to be used for response.
-    * NB: if we can, we will convert the generated response from internal_encoding to the intended one.
-    * can be: a supported xml encoding (only UTF-8 and ISO-8859-1 at present, unless mbstring is enabled),
-    * null (leave unspecified in response, convert output stream to US_ASCII),
-    * 'default' (use xmlrpc library default as specified in xmlrpc.inc, convert output stream if needed),
-    * or 'auto' (use client-specified charset encoding or same as request if request headers do not specify it (unless request is US-ASCII: then use library default anyway).
-    * NB: pretty dangerous if you accept every charset and do not have mbstring enabled)
-    */
+    /**
+     * charset encoding to be used for response.
+     * NB: if we can, we will convert the generated response from internal_encoding to the intended one.
+     * Can be: a supported xml encoding (only UTF-8 and ISO-8859-1 at present, unless mbstring is enabled),
+     * null (leave unspecified in response, convert output stream to US_ASCII),
+     * 'default' (use xmlrpc library default as specified in xmlrpc.inc, convert output stream if needed),
+     * or 'auto' (use client-specified charset encoding or same as request if request headers do not specify it (unless request is US-ASCII: then use library default anyway).
+     * NB: pretty dangerous if you accept every charset and do not have mbstring enabled)
+     */
     public $response_charset_encoding = '';
     /**
      * Storage for internal debug info.
      */
     protected $debug_info = '';
-    /*
-    * Extra data passed at runtime to method handling functions. Used only by EPI layer
-    */
+    /**
+     * Extra data passed at runtime to method handling functions. Used only by EPI layer
+     */
     public $user_data = null;
 
     protected static $_xmlrpc_debuginfo = '';
@@ -541,6 +544,8 @@ class Server
      * @param array $paramTypes array with xmlrpc types of method parameters (if m is method name only)
      *
      * @return Response
+     *
+     * @throws \Exception in case the executed method does throw an exception (and depending on )
      */
     protected function execute($m, $params = null, $paramTypes = null)
     {
index b782319..629fc7c 100644 (file)
@@ -601,6 +601,15 @@ And turned it into nylon';
         }
     }
 
+    public function testClosure()
+    {
+        $f = new xmlrpcmsg('examples.php5.getStateName', array(
+            new xmlrpcval(23, 'int'),
+        ));
+        $v = $this->send($f);
+        $this->assertEquals('Michigan', $v->scalarval());
+    }
+
     public function testGetCookies()
     {
         // let server set to us some cookies we tell it