- * Given the necessary info, build php code that creates a new function to
- * invoke a remote xmlrpc method.
- * Take care that no full checking of input parameters is done to ensure that
- * valid php code is emitted.
- * Note: real spaghetti code follows...
- */
- public function build_remote_method_wrapper_code($client, $methodName, $xmlrpcFuncName,
- $mSig, $mDesc = '', $timeout = 0, $protocol = '', $clientCopyMode = 0, $prefix = 'xmlrpc',
- $decodePhpObjects = false, $encodePhpObjects = false, $decodeFault = false,
- $faultResponse = '', $namespace = '\\PhpXmlRpc\\')
- {
- $code = "function $xmlrpcFuncName (";
- if ($clientCopyMode < 2) {
- // client copy mode 0 or 1 == partial / full client copy in emitted code
- $innerCode = $this->build_client_wrapper_code($client, $clientCopyMode, $prefix, $namespace);
- $innerCode .= "\$client->setDebug(\$debug);\n";
- $this_ = '';
- } else {
- // client copy mode 2 == no client copy in emitted code
- $innerCode = '';
- $this_ = 'this->';
- }
- $innerCode .= "\$req = new {$namespace}Request('$methodName');\n";
-
- if ($mDesc != '') {
- // take care that PHP comment is not terminated unwillingly by method description
- $mDesc = "/**\n* " . str_replace('*/', '* /', $mDesc) . "\n";
- } else {
- $mDesc = "/**\nFunction $xmlrpcFuncName\n";
- }
-
- // param parsing
- $innerCode .= "\$encoder = new {$namespace}Encoder();\n";
- $plist = array();
- $pcount = count($mSig);
- for ($i = 1; $i < $pcount; $i++) {
- $plist[] = "\$p$i";
- $ptype = $mSig[$i];
- if ($ptype == 'i4' || $ptype == 'int' || $ptype == 'boolean' || $ptype == 'double' ||
- $ptype == 'string' || $ptype == 'dateTime.iso8601' || $ptype == 'base64' || $ptype == 'null'
- ) {
- // only build directly xmlrpc values when type is known and scalar
- $innerCode .= "\$p$i = new {$namespace}Value(\$p$i, '$ptype');\n";
- } else {
- if ($encodePhpObjects) {
- $innerCode .= "\$p$i = \$encoder->encode(\$p$i, array('encode_php_objs'));\n";
- } else {
- $innerCode .= "\$p$i = \$encoder->encode(\$p$i);\n";
- }
- }
- $innerCode .= "\$req->addparam(\$p$i);\n";
- $mDesc .= '* @param ' . $this->xmlrpc_2_php_type($ptype) . " \$p$i\n";
- }
- if ($clientCopyMode < 2) {
- $plist[] = '$debug=0';
- $mDesc .= "* @param int \$debug when 1 (or 2) will enable debugging of the underlying {$prefix} call (defaults to 0)\n";
- }
- $plist = implode(', ', $plist);
- $mDesc .= '* @return ' . $this->xmlrpc_2_php_type($mSig[0]) . " (or an {$namespace}Response obj instance if call fails)\n*/\n";
-
- $innerCode .= "\$res = \${$this_}client->send(\$req, $timeout, '$protocol');\n";
- if ($decodeFault) {
- if (is_string($faultResponse) && ((strpos($faultResponse, '%faultCode%') !== false) || (strpos($faultResponse, '%faultString%') !== false))) {
- $respCode = "str_replace(array('%faultCode%', '%faultString%'), array(\$res->faultCode(), \$res->faultString()), '" . str_replace("'", "''", $faultResponse) . "')";
- } else {
- $respCode = var_export($faultResponse, true);
- }
- } else {
- $respCode = '$res';
- }
- if ($decodePhpObjects) {
- $innerCode .= "if (\$res->faultcode()) return $respCode; else return \$encoder->decode(\$res->value(), array('decode_php_objs'));";
- } else {
- $innerCode .= "if (\$res->faultcode()) return $respCode; else return \$encoder->decode(\$res->value());";
- }
-
- $code = $code . $plist . ") {\n" . $innerCode . "\n}\n";
-
- return array('source' => $code, 'docstring' => $mDesc);
- }
-
- /**
- * Given necessary info, generate php code that will rebuild a client object