From: gggeek Date: Wed, 18 Jan 2023 18:14:04 +0000 (+0000) Subject: allow better subclassing of Client by JsonRpc X-Git-Tag: 4.10.0~126 X-Git-Url: http://git.onelab.eu/?a=commitdiff_plain;h=c576cf27e76d4168131bfaca34d09bf7ab001ec1;p=plcapi.git allow better subclassing of Client by JsonRpc --- diff --git a/NEWS.md b/NEWS.md index e0a2eacd..f5dc55d6 100644 --- a/NEWS.md +++ b/NEWS.md @@ -39,8 +39,14 @@ * improved: removed usage of `extension_loaded` in favour of `function_exists` when checking for mbstring. This allows for mbstring functions to be polyfilled +* improved: the code generated by `Wrapper::buildWrapMethodSource` is formatter slightly better + +* improved: made the `Wrapper` and `Client` classes easy to subclass for use by the PhpJsonRpc library + * improved: added the library version number to the debugger title line +* improved: the debugger will now sport the "load method synopsis" button when interacting with json-rpc servers + * improved: made sure the test container has at least one locale with comma as decimal separator * BC notes: @@ -64,6 +70,7 @@ - if you had been somehow interacting with private method `Client::_try_multicall`, be warned its returned data has changed: it now returns a Response for the cases in which it previously returned false, and an array of Response objects for the cases in which it previously returned a string + - if you subclassed the `Client` class, take care of new static variables `$requestClass` and `$responseClass` ## XML-RPC for PHP version 4.9.5 - 2023/01/11 diff --git a/src/Client.php b/src/Client.php index cfe82c9c..b0db5ec4 100644 --- a/src/Client.php +++ b/src/Client.php @@ -18,6 +18,10 @@ class Client const USE_CURL_AUTO = 2; protected static $logger; + /** @var string */ + protected static $requestClass = '\\PhpXmlRpc\\Request'; + /** @var string */ + protected static $responseClass = '\\PhpXmlRpc\\Response'; /// @todo: do these need to be public? public $method = 'http'; @@ -579,7 +583,7 @@ class Client return $r; } elseif (is_string($req)) { - $n = new Request(''); + $n = new static::$requestClass(''); $n->payload = $req; $req = $n; } @@ -903,7 +907,7 @@ class Client } $this->errstr = 'Connect error: ' . $this->errstr; - $r = new Response(0, PhpXmlRpc::$xmlrpcerr['http_error'], $this->errstr . ' (' . $this->errno . ')'); + $r = new static::$responseClass(0, PhpXmlRpc::$xmlrpcerr['http_error'], $this->errstr . ' (' . $this->errno . ')'); return $r; } @@ -911,7 +915,7 @@ class Client if (!fputs($fp, $op, strlen($op))) { fclose($fp); $this->errstr = 'Write error'; - $r = new Response(0, PhpXmlRpc::$xmlrpcerr['http_error'], $this->errstr); + $r = new static::$responseClass(0, PhpXmlRpc::$xmlrpcerr['http_error'], $this->errstr); return $r; } @@ -968,7 +972,7 @@ class Client { if (!function_exists('curl_init')) { $this->errstr = 'CURL unavailable on this install'; - return new Response(0, PhpXmlRpc::$xmlrpcerr['no_curl'], PhpXmlRpc::$xmlrpcstr['no_curl']); + return new static::$responseClass(0, PhpXmlRpc::$xmlrpcerr['no_curl'], PhpXmlRpc::$xmlrpcstr['no_curl']); } if ($method == 'https' || $method == 'h2') { // q: what about installs where we get back a string, but curl is linked to other ssl libs than openssl? @@ -976,13 +980,13 @@ class Client ((is_string($info) && strpos($info, 'OpenSSL') === null) || (is_array($info) && !isset($info['ssl_version']))) ) { $this->errstr = 'SSL unavailable on this install'; - return new Response(0, PhpXmlRpc::$xmlrpcerr['no_ssl'], PhpXmlRpc::$xmlrpcstr['no_ssl']); + return new static::$responseClass(0, PhpXmlRpc::$xmlrpcerr['no_ssl'], PhpXmlRpc::$xmlrpcstr['no_ssl']); } } if (($method == 'h2' && !defined('CURL_HTTP_VERSION_2_0')) || ($method == 'h2c' && !defined('CURL_HTTP_VERSION_2_PRIOR_KNOWLEDGE'))) { $this->errstr = 'HTTP/2 unavailable on this install'; - return new Response(0, PhpXmlRpc::$xmlrpcerr['no_http2'], PhpXmlRpc::$xmlrpcstr['no_http2']); + return new static::$responseClass(0, PhpXmlRpc::$xmlrpcerr['no_http2'], PhpXmlRpc::$xmlrpcstr['no_http2']); } $curl = $this->prepareCurlHandle($req, $server, $port, $timeout, $username, $password, @@ -991,7 +995,7 @@ class Client $keyPass, $sslVersion); if (!$curl) { - return new Response(0, PhpXmlRpc::$xmlrpcerr['curl_fail'], PhpXmlRpc::$xmlrpcstr['curl_fail'] . ': error during curl initialization. Check php error log for details'); + return new static::$responseClass(0, PhpXmlRpc::$xmlrpcerr['curl_fail'], PhpXmlRpc::$xmlrpcstr['curl_fail'] . ': error during curl initialization. Check php error log for details'); } $result = curl_exec($curl); @@ -1012,7 +1016,7 @@ class Client /// @todo we should use a better check here - what if we get back '' or '0'? $this->errstr = 'no response'; - $resp = new Response(0, PhpXmlRpc::$xmlrpcerr['curl_fail'], PhpXmlRpc::$xmlrpcstr['curl_fail'] . ': ' . curl_error($curl)); + $resp = new static::$responseClass(0, PhpXmlRpc::$xmlrpcerr['curl_fail'], PhpXmlRpc::$xmlrpcstr['curl_fail'] . ': ' . curl_error($curl)); curl_close($curl); if ($keepAlive) { $this->xmlrpc_curl_handle = null;