Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

Differences Between: [Versions 401 and 402] [Versions 401 and 403]

(no description)

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

Defines 2 classes


Class: Wrapper  - X-Ref

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.

getLogger()   X-Ref
No description

setLogger($logger)   X-Ref
No description

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 xmlrpc 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 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 '@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 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.

param: callable $callable the PHP user function to be exposed as xmlrpc 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 xmlrpc 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 xmlrpc methods from an xmlrpc 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 xmlrpc methods, or an object instance of that class
param: array $extraOptions see the docs for wrapPhpMethod for basic options, plus
return: array|false false on failure

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 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.

param: Client $client an xmlrpc client set up correctly to communicate with target server
param: string $methodName the xmlrpc 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 xmlrpc 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
param: array $mSig
return: \Closure

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

param: Client $client
param: string $methodName
param: array $extraOptions
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 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...

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: mixed 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 all of the state 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