- * 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...
- */
- protected function build_remote_method_wrapper_code($client, $methodname, $xmlrpcfuncname,
- $msig, $mdesc = '', $timeout = 0, $protocol = '', $client_copy_mode = 0, $prefix = 'xmlrpc',
- $decode_php_objects = false, $encode_php_objects = false, $decode_fault = false,
- $fault_response = '', $namespace = '\\PhpXmlRpc\\')
- {
- $code = "function $xmlrpcfuncname (";
- if ($client_copy_mode < 2) {
- // client copy mode 0 or 1 == partial / full client copy in emitted code
- $innercode = $this->build_client_wrapper_code($client, $client_copy_mode, $prefix, $namespace);
- $innercode .= "\$client->setDebug(\$debug);\n";
- $this_ = '';
- } else {
- // client copy mode 2 == no client copy in emitted code
- $innercode = '';
- $this_ = 'this->';
- }
- $innercode .= "\$msg = 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 xmlrpcvals when type is known and scalar
- $innercode .= "\$p$i = new {$namespace}Value(\$p$i, '$ptype');\n";
- } else {
- if ($encode_php_objects) {
- $innercode .= "\$p$i = \$encoder->encode(\$p$i, array('encode_php_objs'));\n";
- } else {
- $innercode .= "\$p$i = \$encoder->encode(\$p$i);\n";
- }
- }
- $innercode .= "\$msg->addparam(\$p$i);\n";
- $mdesc .= '* @param ' . $this->xmlrpc_2_php_type($ptype) . " \$p$i\n";
- }
- if ($client_copy_mode < 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(\$msg, $timeout, '$protocol');\n";
- if ($decode_fault) {
- if (is_string($fault_response) && ((strpos($fault_response, '%faultCode%') !== false) || (strpos($fault_response, '%faultString%') !== false))) {
- $respcode = "str_replace(array('%faultCode%', '%faultString%'), array(\$res->faultCode(), \$res->faultString()), '" . str_replace("'", "''", $fault_response) . "')";
- } else {
- $respcode = var_export($fault_response, true);
- }
- } else {
- $respcode = '$res';
- }
- if ($decode_php_objects) {
- $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