Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.x is supported too.

Differences Between: [Versions 401 and 403]

(no description)

Author: Gaetano Giunta
Copyright: (C) 2006-2023 G. Giunta
License: code licensed under the BSD License: see file license.txt
File Size: 1258 lines (60 kb)
Included or required:0 times
Referenced: 0 times
Includes or requires: 0 files

Defines 2 classes


Class: Wrapper  - X-Ref

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.

php2XmlrpcType($phpType)   X-Ref
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 xml-rpc structs
- for php arrays always return array, even though arrays sometimes serialize as structs...
- for 'void' and 'null' returns 'undefined'

param: string $phpType
return: string

xmlrpc2PhpType($xmlrpcType)   X-Ref
Given a string defining a phpxmlrpc type return the corresponding php type.

param: string $xmlrpcType
return: string

wrapPhpFunction($callable, $newFuncName = '', $extraOptions = array()   X-Ref
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).

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.

param: \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
param: string $newFuncName (optional) name for function to be created. Used only when return_source in $extraOptions is true
param: array $extraOptions (optional) array of options for conversion. valid values include:
return: array|false false on error, or an array containing the name of the new php function,

introspectFunction($callable, $plainFuncName)   X-Ref
Introspect a php callable and its phpdoc block and extract information about its signature

param: callable $callable
param: string $plainFuncName
return: array|false

buildMethodSignatures($funcDesc)   X-Ref
Given the method description given by introspection, create method signature data

param: array $funcDesc as generated by self::introspectFunction()
return: array

buildWrapFunctionClosure($callable, $extraOptions, $plainFuncName, $funcDesc)   X-Ref
Creates a closure that will execute $callable

param: $callable
param: array $extraOptions
param: string $plainFuncName
param: array $funcDesc
return: \Closure

newFunctionName($callable, $newFuncName, $extraOptions)   X-Ref
Return a name for a new function, based on $callable, insuring its uniqueness

param: mixed $callable a php callable, or the name of an xml-rpc method
param: string $newFuncName when not empty, it is used instead of the calculated version
return: string

buildWrapFunctionSource($callable, $newFuncName, $extraOptions, $plainFuncName, $funcDesc)   X-Ref

param: $callable
param: string $newFuncName
param: array $extraOptions
param: string $plainFuncName
param: array $funcDesc
return: string

wrapPhpClass($className, $extraOptions = array()   X-Ref
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).

param: string|object $className the name of the class whose methods are to be exposed as xml-rpc methods, or an object instance of that class
param: array $extraOptions see the docs for wrapPhpFunction for basic options, plus
return: array|false false on failure, or on array useable for the dispatch map

generateMethodNameForClassMethod($className, $classMethod, $extraOptions = array()   X-Ref

param: string|object $className
param: string $classMethod
param: array $extraOptions
return: string

wrapXmlrpcMethod($client, $methodName, $extraOptions = array()   X-Ref
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.

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.

param: Client $client an xml-rpc client set up correctly to communicate with target server
param: string $methodName the xml-rpc method to be mapped to a php function
param: array $extraOptions array of options that specify conversion details. Valid options include
return: \Closure|string[]|false false on failure, closure by default and array for return_source = true

retrieveMethodSignature($client, $methodName, array $extraOptions = array()   X-Ref
Retrieves an xml-rpc method signature from a server which supports system.methodSignature

param: Client $client
param: string $methodName
param: array $extraOptions
return: false|array

retrieveMethodHelp($client, $methodName, array $extraOptions = array()   X-Ref

param: Client $client
param: string $methodName
param: array $extraOptions
return: string in case of any error, an empty string is returned, no warnings generated

buildWrapMethodClosure($client, $methodName, array $extraOptions, $mSig)   X-Ref

param: Client $client
param: string $methodName
param: array $extraOptions @see wrapXmlrpcMethod
param: array $mSig
return: \Closure

buildWrapMethodSource($client, $methodName, array $extraOptions, $newFuncName, $mSig, $mDesc='')   X-Ref

param: Client $client
param: string $methodName
param: array $extraOptions @see wrapXmlrpcMethod
param: string $newFuncName
param: array $mSig
param: string $mDesc
return: string[] keys: source, docstring

wrapXmlrpcServer($client, $extraOptions = array()   X-Ref
Similar to wrapXmlrpcMethod, but will generate a php class that wraps all xml-rpc methods exposed by the remote
server as own methods.
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...

param: Client $client the client obj all set to query the desired server
param: array $extraOptions list of options for wrapped code. See the ones from wrapXmlrpcMethod, plus
return: string|array|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)

Class: method  - X-Ref

buildClientWrapperCode($client, $verbatimClientCopy, $prefix = 'xmlrpc', $namespace = '\\PhpXmlRpc\\')   X-Ref
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.

param: Client $client
param: bool $verbatimClientCopy when true, copy the whole options of the client, except for 'debug' and 'return_type'
param: string $prefix used for the return_type of the created client
param: string $namespace
return: string

holdObject($index, $object)   X-Ref

param: string $index
param: object $object
return: void

getHeldObject($index)   X-Ref

param: string $index
return: object