Package home | Report new bug | New search | Development Roadmap Status: Open | Feedback | All | Closed Since Version 1.5.5

Request #1161 [patch] add introspection methods
Submitted: 2004-04-08 08:13 UTC
From: sebastien dot person at easter-eggs dot fr Assigned: danielc
Status: Wont fix Package: XML_RPC
PHP Version: Irrelevant OS: linux
Roadmaps: (Not assigned)    
Subscription  


 [2004-04-08 08:13 UTC] sebastien dot person at easter-eggs dot fr
Description: ------------ last time, I proposed an extension to XML/RPC.php to make developper life easier but my modifications wasn't following pear policy. So I tried to follow pear policy and pear-dev list advices. The changes are the following : - function are now documented - I tried to respect alignement in variable assignement - my function name follow the studly caps style - I have made a discover method wich return an assoc array of discovered infos - another method called discoverHTML for people who want to output to browser (output is conform to the HTML reocmmendation) - methods for each of the three rpc methods (listMethods, methodsHelp, methodSignature) wich return an array of the result. You could find attached my patch. Let me know if there is any problem with it ... : --- RPC.php Wed Oct 2 23:10:19 2002 +++ /usr/share/pear/XML/RPC.php Wed Apr 7 17:15:39 2004 @@ -393,8 +394,119 @@ $this->debug=0; } } + + /** + * return associative array with all the available methods from the server + * and add the signature and doc string informations ('docstring') + * + * @access public + * @return array associative array of the discovered methods + */ + function discover() + { + $methods = $this->listMethods(); + $number_of_methods = sizeof($methods); + + for ( $i= 0; $i < $number_of_methods; $i++){ + $method = $methods[$i]; + $return[$method]['docstring'] = $this->methodHelp($method); + $return[$method]['signature'] = $this->methodSignature($method); + } + return $return; + } + + + /** + * the purpose of this function is for helping developper to let them + * browse through a brwoser available method of the current XML-RPC + * server the client is attached to. + * + * @access public + * @return string HTML output representation of the available method + */ + function discoverHTML(){ + + $discover = $this->discover(); + + foreach ($discover as $method_name => $method_array){ + $output .= '<pre><b>' . $method_name . '</b>'; + + $number_of_params = sizeof($method_array['signature']); + for ($i = 0; $i < $number_of_params; $i++){ + $output .= $i != 0 ? ', input:' : ' (output:'; + $output .= $method_array['signature'][$i]; + } + $output .= ')<br>'. $method_array['docstring'] . '<br>'; + $output .= '</pre>'; + } + return $output; + } + + + /** + * list all available remote RPC methods + * + * @access public + * @return array an array of available method + */ + function listMethods(){ + $response = $this->send(new XML_RPC_Message('system.listMethods')); + + if(!$response->faultcode()){ + $method_value = $response->value(); + return XML_RPC_Decode($method_value); + } + } + + /** + * get doc string describing the method purpose + * + * @access public + * @param string method name + * @return string contains the description or an error message is something went wrong + */ + function methodHelp($method){ + // request method doc + $doc_response = $this->send(new XML_RPC_Message("system.methodHelp", + array(XML_RPC_Encode($method)))); + if ($doc_response == 0) { + return "Error occured : " . $this->errno() . $this->errstring(); + } + + if ($doc_response->faultcode()) { + return $doc_response->faultString(); + } + return XML_RPC_Decode($doc_response->value()); + } + + /** + * return the signature for a given method + * + * @access public + * @param string method name + * @return array all availables signatures or a string describing the error + */ + function methodSignature($method) + { + // request method signature + $sig_response = $this->send(new XML_RPC_Message("system.methodSignature", + array(XML_RPC_Encode($method)))); + + // be sure there isn't any I/O trouble + if ($sig_response == 0) { + return "Error occured : " . $this->errno() . $this->errstring(); + } + // signature stuff + if ($sig_response->faultcode()){ + return $sig_response->faultString(); + } + $sig_value = $sig_response->value(); + $sig_array = XML_RPC_Decode($sig_response->value()); + return $sig_array[0]; + } + function setCredentials($u, $p) { $this->username=$u; $this->password=$p; @@ -514,9 +626,9 @@ function value() { return $this->xv; } function serialize() { $rs="<methodResponse>\n"; - if ($this->fn) { + if (isset($this->fn)) { $rs.="<fault> <value> <struct> <member>

Comments

 [2005-04-06 21:35 UTC] danielc
While this is an interesting proposal, I don't think it's wise for providers to list all of their methods. Server providers that wish to have such functionality can easily make methods for doing so.