Wrapper
in package
uses
LoggerAware
PHPXMLRPC "wrapper" class - generate stubs to transparently access xml-rpc 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.
Tags
Table of Contents
Properties
- $logger : mixed
- $namespace : string
Methods
- getHeldObject() : object
- getLogger() : mixed
- holdObject() : void
- php2XmlrpcType() : string
- Given a string defining a php type or phpxmlrpc type (loosely defined: strings accepted come from javadoc blocks), return corresponding phpxmlrpc type.
- setLogger() : void
- wrapPhpClass() : array<string|int, mixed>|false
- Given a user-defined PHP class or php object, map its methods onto a list of PHP 'wrapper' functions that can be exposed as xml-rpc methods from an xml-rpc server object and called from remote clients (as well as their corresponding signature info).
- wrapPhpFunction() : array<string|int, mixed>|false
- Given a user-defined PHP function, create a PHP 'wrapper' function that can be exposed as xml-rpc method from an xml-rpc server object and called from remote clients (as well as its corresponding signature info).
- wrapXmlrpcMethod() : Closure|array<string|int, string>|false
- Given an xml-rpc client and a method name, register a php wrapper function that will call it and return results using native php types for both arguments and results. The generated php function will return a Response object for failed xml-rpc calls.
- wrapXmlrpcServer() : string|array<string|int, mixed>|false
- Similar to wrapXmlrpcMethod, but will generate a php class that wraps all xml-rpc methods exposed by the remote server as own methods.
- xmlrpc2PhpType() : string
- Given a string defining a phpxmlrpc type return the corresponding php type.
- buildClientWrapperCode() : string
- Given necessary info, generate php code that will build a client object just like the given one.
- buildMethodSignatures() : array<string|int, mixed>
- Given the method description given by introspection, create method signature data
- buildWrapFunctionClosure() : Closure
- Creates a closure that will execute $callable
- buildWrapFunctionSource() : string
- buildWrapMethodClosure() : Closure
- generateMethodNameForClassMethod() : string
- introspectFunction() : array<string|int, mixed>|false
- Introspect a php callable and its phpdoc block and extract information about its signature
- newFunctionName() : string
- Return a name for a new function, based on $callable, insuring its uniqueness
- retrieveMethodHelp() : string
- retrieveMethodSignature() : false|array<string|int, mixed>
- Retrieves an xml-rpc method signature from a server which supports system.methodSignature
Properties
$logger
protected
static mixed
$logger
$namespace
protected
static string
$namespace
= '\PhpXmlRpc\\'
Methods
getHeldObject()
public
static getHeldObject(string $index) : object
Parameters
- $index : string
Tags
Return values
objectgetLogger()
public
getLogger() : mixed
holdObject()
public
static holdObject(string $index, object $object) : void
Parameters
- $index : string
- $object : object
php2XmlrpcType()
Given a string defining a php type or phpxmlrpc type (loosely defined: strings accepted come from javadoc blocks), return corresponding phpxmlrpc type.
public
php2XmlrpcType(string $phpType) : string
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 xml-rpc structs
- for php arrays always return array, even though arrays sometimes serialize as structs...
- for 'void' and 'null' returns 'undefined'
Parameters
- $phpType : string
Tags
Return values
stringsetLogger()
public
static setLogger(mixed $logger) : void
Parameters
- $logger : mixed
wrapPhpClass()
Given a user-defined PHP class or php object, map its methods onto a list of PHP 'wrapper' functions that can be exposed as xml-rpc methods from an xml-rpc server object and called from remote clients (as well as their corresponding signature info).
public
wrapPhpClass(string|object $className[, array<string|int, mixed> $extraOptions = array() ]) : array<string|int, mixed>|false
Parameters
- $className : string|object
-
the name of the class whose methods are to be exposed as xml-rpc methods, or an object instance of that class
- $extraOptions : array<string|int, mixed> = array()
-
see the docs for wrapPhpFunction 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 xml-rpc 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
Tags
Return values
array<string|int, mixed>|false —false on failure, or on array useable for the dispatch map
wrapPhpFunction()
Given a user-defined PHP function, create a PHP 'wrapper' function that can be exposed as xml-rpc method from an xml-rpc server object and called from remote clients (as well as its corresponding signature info).
public
wrapPhpFunction(Callable $callable[, string $newFuncName = '' ][, array<string|int, mixed> $extraOptions = array() ]) : array<string|int, mixed>|false
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 xml-rpc native types (such as datetime.dateTime.iso8601 and base64) in the '@param' 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 values 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 xml-rpc responses: when the xml-rpc 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 (i.e. functions not expecting a single Request obj as parameter) is by making use of the $functions_parameters_type and $exception_handling properties.
Parameters
- $callable : Callable
-
the PHP user function to be exposed as xml-rpc method: a closure, function name, array($obj, 'methodname') or array('class', 'methodname') are ok
- $newFuncName : string = ''
-
(optional) name for function to be created. Used only when return_source in $extraOptions is true
- $extraOptions : array<string|int, mixed> = array()
-
(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_nulls let php objects be sent to server using
elements instead of empty strings - bool encode_php_objs let php objects be sent to server using the 'improved' xml-rpc 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
Tags
Return values
array<string|int, mixed>|false —false 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
wrapXmlrpcMethod()
Given an xml-rpc client and a method name, register a php wrapper function that will call it and return results using native php types for both arguments and results. The generated php function will return a Response object for failed xml-rpc calls.
public
wrapXmlrpcMethod(Client $client, string $methodName[, array<string|int, mixed> $extraOptions = array() ]) : Closure|array<string|int, string>|false
Known limitations:
- server must support system.methodSignature for the target xml-rpc 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 for)
- nested xml-rpc 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 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 slightly slow: a new xml-rpc client is created on every invocation and an xmlrpc-connection opened+closed. An extra 'debug' argument, defaulting to 0, is appended to the argument list of the generated function, useful for debugging purposes.
Parameters
- $client : Client
-
an xml-rpc client set up correctly to communicate with target server
- $methodName : string
-
the xml-rpc method to be mapped to a php function
- $extraOptions : array<string|int, mixed> = array()
-
array 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', 'https', 'h2' or 'h2c'
- 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 the function itself (closure)
- bool encode_nulls if true, use
<nil/>
elements instead of empty string xml-rpc values for php null values - bool encode_php_objs let php objects be sent to server using the 'improved' xml-rpc 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 xml-rpc 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 throw_on_fault if true, throw an exception instead of returning a Response in case of errors/faults; if a string, do the same and assume it is the exception class to throw
- 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)
Tags
Return values
Closure|array<string|int, string>|false —false on failure, closure by default and array for return_source = true
wrapXmlrpcServer()
Similar to wrapXmlrpcMethod, but will generate a php class that wraps all xml-rpc methods exposed by the remote server as own methods.
public
wrapXmlrpcServer(Client $client[, array<string|int, mixed> $extraOptions = array() ]) : string|array<string|int, mixed>|false
For a slimmer alternative, see the code in demo/client/proxy.php. Note that unlike wrapXmlrpcMethod, we always have to generate php code here. Since php 7 anon classes exist, but we do not support them yet...
Parameters
- $client : Client
-
the client obj all set to query the desired server
- $extraOptions : array<string|int, mixed> = array()
-
list 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
Tags
Return values
string|array<string|int, mixed>|false —false 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()
Given a string defining a phpxmlrpc type return the corresponding php type.
public
xmlrpc2PhpType(string $xmlrpcType) : string
Parameters
- $xmlrpcType : string
Return values
stringbuildClientWrapperCode()
Given necessary info, generate php code that will build a client object just like the given one.
protected
buildClientWrapperCode(Client $client, bool $verbatimClientCopy[, string $prefix = 'xmlrpc' ][, string $namespace = 'PhpXmlRpc\' ]) : string
Take care that no full checking of input parameters is done to ensure that valid php code is emitted.
Parameters
- $client : Client
- $verbatimClientCopy : bool
-
when true, copy the whole options of the client, except for 'debug' and 'return_type'
- $prefix : string = 'xmlrpc'
-
used for the return_type of the created client
- $namespace : string = 'PhpXmlRpc\'
Return values
stringbuildMethodSignatures()
Given the method description given by introspection, create method signature data
protected
buildMethodSignatures(array<string|int, mixed> $funcDesc) : array<string|int, mixed>
Parameters
- $funcDesc : array<string|int, mixed>
-
as generated by self::introspectFunction()
Tags
Return values
array<string|int, mixed>buildWrapFunctionClosure()
Creates a closure that will execute $callable
protected
buildWrapFunctionClosure(mixed $callable, array<string|int, mixed> $extraOptions, string $plainFuncName, array<string|int, mixed> $funcDesc) : Closure
Parameters
- $callable : mixed
- $extraOptions : array<string|int, mixed>
- $plainFuncName : string
- $funcDesc : array<string|int, mixed>
Tags
Return values
ClosurebuildWrapFunctionSource()
protected
buildWrapFunctionSource(mixed $callable, string $newFuncName, array<string|int, mixed> $extraOptions, string $plainFuncName, array<string|int, mixed> $funcDesc) : string
Parameters
- $callable : mixed
- $newFuncName : string
- $extraOptions : array<string|int, mixed>
- $plainFuncName : string
- $funcDesc : array<string|int, mixed>
Return values
stringbuildWrapMethodClosure()
protected
buildWrapMethodClosure(Client $client, string $methodName, array<string|int, mixed> $extraOptions, array<string|int, mixed> $mSig) : Closure
Parameters
- $client : Client
- $methodName : string
- $extraOptions : array<string|int, mixed>
-
@see wrapXmlrpcMethod
- $mSig : array<string|int, mixed>
Tags
Return values
ClosuregenerateMethodNameForClassMethod()
protected
generateMethodNameForClassMethod(string|object $className, string $classMethod[, array<string|int, mixed> $extraOptions = array() ]) : string
Parameters
- $className : string|object
- $classMethod : string
- $extraOptions : array<string|int, mixed> = array()
Tags
Return values
stringintrospectFunction()
Introspect a php callable and its phpdoc block and extract information about its signature
protected
introspectFunction(callable $callable, string $plainFuncName) : array<string|int, mixed>|false
Parameters
- $callable : callable
- $plainFuncName : string
Return values
array<string|int, mixed>|falsenewFunctionName()
Return a name for a new function, based on $callable, insuring its uniqueness
protected
newFunctionName(mixed $callable, string $newFuncName, mixed $extraOptions) : string
Parameters
- $callable : mixed
-
a php callable, or the name of an xml-rpc method
- $newFuncName : string
-
when not empty, it is used instead of the calculated version
- $extraOptions : mixed
Return values
stringretrieveMethodHelp()
protected
retrieveMethodHelp(Client $client, string $methodName[, array<string|int, mixed> $extraOptions = array() ]) : string
Parameters
- $client : Client
- $methodName : string
- $extraOptions : array<string|int, mixed> = array()
Return values
string —in case of any error, an empty string is returned, no warnings generated
retrieveMethodSignature()
Retrieves an xml-rpc method signature from a server which supports system.methodSignature
protected
retrieveMethodSignature(Client $client, string $methodName[, array<string|int, mixed> $extraOptions = array() ]) : false|array<string|int, mixed>
Parameters
- $client : Client
- $methodName : string
- $extraOptions : array<string|int, mixed> = array()