Moodle PHP Documentation 4.1
Moodle 4.1.11 (Build: 20240610) (c8c84b4af18)
PhpXmlRpc\Wrapper Class Reference

PHP-XMLRPC "wrapper" class - generate stubs to transparently access xmlrpc methods as php functions and vice-versa. More...

Public Member Functions

 buildWrapMethodSource ($client, $methodName, array $extraOptions, $newFuncName, $mSig, $mDesc='')
 
 getLogger ()
 
 php2XmlrpcType ($phpType)
 Given a string defining a php type or phpxmlrpc type (loosely defined: strings accepted come from javadoc blocks), return corresponding phpxmlrpc type.
 
 wrapPhpClass ($className, $extraOptions=array())
 Given a user-defined PHP class or php object, map its methods onto a list of PHP 'wrapper' functions that can be exposed as xmlrpc methods from an xmlrpc server object and called from remote clients (as well as their corresponding signature info).
 
 wrapPhpFunction ($callable, $newFuncName='', $extraOptions=array())
 Given a user-defined PHP function, create a PHP 'wrapper' function that can be exposed as xmlrpc method from an xmlrpc server object and called from remote clients (as well as its corresponding signature info).
 
 wrapXmlrpcMethod ($client, $methodName, $extraOptions=array())
 Given an xmlrpc client and a method name, register a php wrapper function that will call it and return results using native php types for both params and results.
 
 wrapXmlrpcServer ($client, $extraOptions=array())
 Similar to wrapXmlrpcMethod, but will generate a php class that wraps all xmlrpc methods exposed by the remote server as own methods.
 
 xmlrpc2PhpType ($xmlrpcType)
 Given a string defining a phpxmlrpc type return the corresponding php type.
 

Static Public Member Functions

static setLogger ($logger)
 

Static Public Attributes

static $objHolder = array()
 used to hold a reference to object instances whose methods get wrapped by wrapPhpFunction(), in 'create source' mode
 

Protected Member Functions

 buildClientWrapperCode ($client, $verbatimClientCopy, $prefix='xmlrpc', $namespace='\\PhpXmlRpc\\')
 Given necessary info, generate php code that will build a client object just like the given one.
 
 buildMethodSignatures ($funcDesc)
 Given the method description given by introspection, create method signature data.
 
 buildWrapFunctionClosure ($callable, $extraOptions, $plainFuncName, $funcDesc)
 Creates a closure that will execute $callable.
 
 buildWrapFunctionSource ($callable, $newFuncName, $extraOptions, $plainFuncName, $funcDesc)
 
 buildWrapMethodClosure ($client, $methodName, array $extraOptions, $mSig)
 
 generateMethodNameForClassMethod ($className, $classMethod, $extraOptions=array())
 
 introspectFunction ($callable, $plainFuncName)
 Introspect a php callable and its phpdoc block and extract information about its signature.
 
 newFunctionName ($callable, $newFuncName, $extraOptions)
 Return a name for a new function, based on $callable, insuring its uniqueness.
 
 retrieveMethodHelp ($client, $methodName, array $extraOptions=array())
 
 retrieveMethodSignature ($client, $methodName, array $extraOptions=array())
 Retrieves an xmlrpc method signature from a server which supports system.methodSignature.
 

Static Protected Attributes

static $logger
 

Detailed Description

PHP-XMLRPC "wrapper" class - generate stubs to transparently access xmlrpc methods as php functions and vice-versa.

Note: this class implements the PROXY pattern, but it is not named so to avoid confusion with http proxies.

Todo

use some better templating system for code generation?

implement method wrapping with preservation of php objs in calls

when wrapping methods without obj rebuilding, use return_type = 'phpvals' (faster)

add support for 'epivals' mode

allow setting custom namespace for generated wrapping code

Member Function Documentation

◆ buildClientWrapperCode()

PhpXmlRpc\Wrapper::buildClientWrapperCode ( $client,
$verbatimClientCopy,
$prefix = 'xmlrpc',
$namespace = '\\PhpXmlRpc\\' )
protected

Given necessary info, generate php code that will build a client object just like the given one.

Take care that no full checking of input parameters is done to ensure that valid php code is emitted.

Parameters
Client$client
bool$verbatimClientCopywhen true, copy all of the state of the client, except for 'debug' and 'return_type'
string$prefixused for the return_type of the created client
string$namespace
Return values
string

◆ buildMethodSignatures()

PhpXmlRpc\Wrapper::buildMethodSignatures ( $funcDesc)
protected

Given the method description given by introspection, create method signature data.

Todo
support better docs with multiple types separated by pipes by creating multiple signatures (this is questionable, as it might produce a big matrix of possible signatures with many such occurrences)
Parameters
array$funcDescas generated by self\introspectFunction()
Return values
array

◆ buildWrapFunctionClosure()

PhpXmlRpc\Wrapper::buildWrapFunctionClosure ( $callable,
$extraOptions,
$plainFuncName,
$funcDesc )
protected

Creates a closure that will execute $callable.

Todo

validate params? In theory all validation is left to the dispatch map...

add support for $catchWarnings

Parameters
$callable
array$extraOptions
string$plainFuncName
array$funcDesc
Return values
Closure

◆ buildWrapFunctionSource()

PhpXmlRpc\Wrapper::buildWrapFunctionSource ( $callable,
$newFuncName,
$extraOptions,
$plainFuncName,
$funcDesc )
protected
Parameters
$callable
string$newFuncName
array$extraOptions
string$plainFuncName
array$funcDesc
Return values
string
Todo
add a nice phpdoc block in the generated source

◆ buildWrapMethodClosure()

PhpXmlRpc\Wrapper::buildWrapMethodClosure ( $client,
$methodName,
array $extraOptions,
$mSig )
protected
Parameters
Client$client
string$methodName
array$extraOptions
array$mSig
Return values
Closure
Todo
should we allow usage of parameter simple_client_copy to mean 'do not clone' in this case?

◆ buildWrapMethodSource()

PhpXmlRpc\Wrapper::buildWrapMethodSource ( $client,
$methodName,
array $extraOptions,
$newFuncName,
$mSig,
$mDesc = '' )
Parameters
Client$client
string$methodName
array$extraOptions
string$newFuncName
array$mSig
string$mDesc
Return values
string[]keys: source, docstring

◆ generateMethodNameForClassMethod()

PhpXmlRpc\Wrapper::generateMethodNameForClassMethod ( $className,
$classMethod,
$extraOptions = array() )
protected
Parameters
string | object$className
string$classMethod
array$extraOptions
Return values
string

◆ introspectFunction()

PhpXmlRpc\Wrapper::introspectFunction ( $callable,
$plainFuncName )
protected

Introspect a php callable and its phpdoc block and extract information about its signature.

Parameters
callable$callable
string$plainFuncName
Return values
array|false

◆ newFunctionName()

PhpXmlRpc\Wrapper::newFunctionName ( $callable,
$newFuncName,
$extraOptions )
protected

Return a name for a new function, based on $callable, insuring its uniqueness.

Parameters
mixed$callablea php callable, or the name of an xmlrpc method
string$newFuncNamewhen not empty, it is used instead of the calculated version
Return values
string

◆ php2XmlrpcType()

PhpXmlRpc\Wrapper::php2XmlrpcType ( $phpType)

Given a string defining a php type or phpxmlrpc type (loosely defined: strings accepted come from javadoc blocks), return corresponding phpxmlrpc type.

Notes:

  • for php 'resource' types returns empty string, since resources cannot be serialized;
  • for php class names returns 'struct', since php objects can be serialized as xmlrpc structs
  • for php arrays always return array, even though arrays sometimes serialize as structs...
  • for 'void' and 'null' returns 'undefined'
Parameters
string$phpType
Return values
string
Todo
support notation something[] as 'array'

◆ retrieveMethodHelp()

PhpXmlRpc\Wrapper::retrieveMethodHelp ( $client,
$methodName,
array $extraOptions = array() )
protected
Parameters
Client$client
string$methodName
array$extraOptions
Return values
stringin case of any error, an empty string is returned, no warnings generated

◆ retrieveMethodSignature()

PhpXmlRpc\Wrapper::retrieveMethodSignature ( $client,
$methodName,
array $extraOptions = array() )
protected

Retrieves an xmlrpc method signature from a server which supports system.methodSignature.

Parameters
Client$client
string$methodName
array$extraOptions
Return values
false|array

◆ wrapPhpClass()

PhpXmlRpc\Wrapper::wrapPhpClass ( $className,
$extraOptions = array() )

Given a user-defined PHP class or php object, map its methods onto a list of PHP 'wrapper' functions that can be exposed as xmlrpc methods from an xmlrpc server object and called from remote clients (as well as their corresponding signature info).

Parameters
string | object$classNamethe name of the class whose methods are to be exposed as xmlrpc methods, or an object instance of that class
array$extraOptionssee the docs for wrapPhpMethod for basic options, plus
  • string method_type 'static', 'nonstatic', 'all' and 'auto' (default); the latter will switch between static and non-static depending on whether $className is a class name or object instance
  • string method_filter a regexp used to filter methods to wrap based on their names
  • string prefix used for the names of the xmlrpc methods created.
  • string replace_class_name use to completely replace the class name with the prefix in the generated method names. e.g. instead of Some\Namespace\Class.method use prefixmethod
Return values
array|falsefalse on failure

◆ wrapPhpFunction()

PhpXmlRpc\Wrapper::wrapPhpFunction ( $callable,
$newFuncName = '',
$extraOptions = array() )

Given a user-defined PHP function, create a PHP 'wrapper' function that can be exposed as xmlrpc method from an xmlrpc server object and called from remote clients (as well as its corresponding signature info).

Since php is a typeless language, to infer types of input and output parameters, it relies on parsing the javadoc-style comment block associated with the given function. Usage of xmlrpc native types (such as datetime.dateTime.iso8601 and base64) in the '

Parameters
'tag is also allowed, if you need the php function to receive/send data in that particular format (note that base64 encoding/decoding is transparently carried out by the lib, while datetime vals are passed around as strings)

Known limitations:

  • only works for user-defined functions, not for PHP internal functions (reflection does not support retrieving number/type of params for those)
  • functions returning php objects will generate special structs in xmlrpc responses: when the xmlrpc decoding of those responses is carried out by this same lib, using the appropriate param in php_xmlrpc_decode, the php objects will be rebuilt. In short: php objects can be serialized, too (except for their resource members), using this function. Other libs might choke on the very same xml that will be generated in this case (i.e. it has a nonstandard attribute on struct element tags)

Note that since rel. 2.0RC3 the preferred method to have the server call 'standard' php functions (ie. functions not expecting a single Request obj as parameter) is by making use of the functions_parameters_type class member.

Parameters
callable$callablethe PHP user function to be exposed as xmlrpc method/ a closure, function name, array($obj, 'methodname') or array('class', 'methodname') are ok
string$newFuncName(optional) name for function to be created. Used only when return_source in $extraOptions is true
array$extraOptions(optional) array of options for conversion. valid values include:
  • bool return_source when true, php code w. function definition will be returned, instead of a closure
  • bool encode_php_objs let php objects be sent to server using the 'improved' xmlrpc notation, so server can deserialize them as php objects
  • bool decode_php_objs — WARNING !!! possible security hazard. only use it with trusted servers —
  • bool suppress_warnings remove from produced xml any warnings generated at runtime by the php function being invoked
Return values
array|falsefalse on error, or an array containing the name of the new php function, its signature and docs, to be used in the server dispatch map
Todo

decide how to deal with params passed by ref in function definition: bomb out or allow?

finish using phpdoc info to build method sig if all params are named but out of order

add a check for params of 'resource' type

add some trigger_errors / error_log when returning false?

what to do when the PHP function returns NULL? We are currently returning an empty string value...

add an option to suppress php warnings in invocation of user function, similar to server debug level 3?

add a verbatim_object_copy parameter to allow avoiding usage the same obj instance?

add an option to allow generated function to skip validation of number of parameters, as that is done by the server anyway

◆ wrapXmlrpcMethod()

PhpXmlRpc\Wrapper::wrapXmlrpcMethod ( $client,
$methodName,
$extraOptions = array() )

Given an xmlrpc client and a method name, register a php wrapper function that will call it and return results using native php types for both params and results.

The generated php function will return a Response object for failed xmlrpc calls.

Known limitations:

  • server must support system.methodsignature for the wanted xmlrpc method
  • for methods that expose many signatures, only one can be picked (we could in principle check if signatures differ only by number of params and not by type, but it would be more complication than we can spare time)
  • nested xmlrpc params: the caller of the generated php function has to encode on its own the params passed to the php function if these are structs or arrays whose (sub)members include values of type datetime or base64

Notes: the connection properties of the given client will be copied and reused for the connection used during the call to the generated php function. Calling the generated php function 'might' be slow: a new xmlrpc client is created on every invocation and an xmlrpc-connection opened+closed. An extra 'debug' param is appended to param list of xmlrpc method, useful for debugging purposes.

Todo

allow caller to give us the method signature instead of querying for it, or just say 'skip it'

if we can not retrieve method signature, create a php function with varargs

allow the created function to throw exceptions on method calls failures

if caller did not specify a specific sig, shall we support all of them? It might be hard (hence slow) to match based on type and number of arguments...

Parameters
Client$clientan xmlrpc client set up correctly to communicate with target server
string$methodNamethe xmlrpc method to be mapped to a php function
array$extraOptionsarray of options that specify conversion details. Valid options include
  • integer signum the index of the method signature to use in mapping (if method exposes many sigs)
  • integer timeout timeout (in secs) to be used when executing function/calling remote method
  • string protocol 'http' (default), 'http11' or 'https'
  • string new_function_name the name of php function to create, when return_source is used. If unspecified, lib will pick an appropriate name
  • string return_source if true return php code w. function definition instead of function itself (closure)
  • bool encode_php_objs let php objects be sent to server using the 'improved' xmlrpc notation, so server can deserialize them as php objects
  • bool decode_php_objs — WARNING !!! possible security hazard. only use it with trusted servers —
  • mixed return_on_fault a php value to be returned when the xmlrpc call fails/returns a fault response (by default the Response object is returned in this case). If a string is used, 'faultCode' and 'faultString' tokens will be substituted with actual error values
  • bool debug set it to 1 or 2 to see debug results of querying server for method synopsis
  • int simple_client_copy set it to 1 to have a lightweight copy of the $client object made in the generated code (only used when return_source = true)
Return values
closure|string[]|falsefalse on failure, closure by default and array for return_source = true

◆ wrapXmlrpcServer()

PhpXmlRpc\Wrapper::wrapXmlrpcServer ( $client,
$extraOptions = array() )

Similar to wrapXmlrpcMethod, but will generate a php class that wraps all xmlrpc methods exposed by the remote server as own methods.

For more details see wrapXmlrpcMethod.

For a slimmer alternative, see the code in demo/client/proxy.php

Note that unlike wrapXmlrpcMethod, we always have to generate php code here. It seems that php 7 will have anon classes...

Parameters
Client$clientthe client obj all set to query the desired server
array$extraOptionslist of options for wrapped code. See the ones from wrapXmlrpcMethod plus
  • string method_filter regular expression
  • string new_class_name
  • string prefix
  • bool simple_client_copy set it to true to avoid copying all properties of $client into the copy made in the new class
Return values
mixedfalse on error, the name of the created class if all ok or an array with code, class name and comments (if the appropriate option is set in extra_options)

◆ xmlrpc2PhpType()

PhpXmlRpc\Wrapper::xmlrpc2PhpType ( $xmlrpcType)

Given a string defining a phpxmlrpc type return the corresponding php type.

Parameters
string$xmlrpcType
Return values
string

The documentation for this class was generated from the following file: