From: gggeek Date: Mon, 30 Jan 2023 18:11:13 +0000 (+0000) Subject: WIP allow deprecations to be logged X-Git-Tag: 4.10.0~59 X-Git-Url: http://git.onelab.eu/?a=commitdiff_plain;h=721548885829b4f81fd253110d09b33bfc32f0bc;p=plcapi.git WIP allow deprecations to be logged --- diff --git a/NEWS.md b/NEWS.md index c05a6645..2c711fea 100644 --- a/NEWS.md +++ b/NEWS.md @@ -172,8 +172,8 @@ 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`, which should be used to instantiate requests and responses - - if you replaced the Logger class, take care that you will have to implement methods `error` and `debug` (all is ok - if you subclassed id) + - if you replaced the Logger class, take care that you will have to implement methods `error`, `warning` and `debug` + (all is ok if you subclassed it) - traits have been introduced for all classes dealing with Logger, XMLParser and CharsetEncoder; method `setCharsetEncoder` is now static - exception `\PhpXmlRpc\Exception\PhpXmlRpcException` is deprecated. Use `\PhpXmlRpc\Exception` instead diff --git a/src/Client.php b/src/Client.php index e1230fd6..da15e656 100644 --- a/src/Client.php +++ b/src/Client.php @@ -5,14 +5,14 @@ namespace PhpXmlRpc; //use PhpXmlRpc\Helper\Charset; use PhpXmlRpc\Exception\ValueErrorException; use PhpXmlRpc\Helper\XMLParser; -use PhpXmlRpc\Traits\LoggerAware; +use PhpXmlRpc\Traits\DeprecationLogger; /** * Used to represent a client of an XML-RPC server. */ class Client { - use LoggerAware; + use DeprecationLogger; const USE_CURL_NEVER = 0; const USE_CURL_ALWAYS = 1; @@ -946,9 +946,9 @@ class Client */ public function send($req, $timeout = 0, $method = '') { - //if ($method !== '' || $timeout !== 0) { - // trigger_error("Using non-default values for arguments 'method' and 'timeout' when calling method " . __METHOD__ . ' is deprecated', E_USER_DEPRECATED); - //} + if ($method !== '' || $timeout !== 0) { + $this->logDeprecation("Using non-default values for arguments 'method' and 'timeout' when calling method " . __METHOD__ . ' is deprecated'); + } // if user does not specify http protocol, use native method of this client // (i.e. method set during call to constructor) @@ -1054,7 +1054,7 @@ class Client $authType = 1, $proxyHost = '', $proxyPort = 0, $proxyUsername = '', $proxyPassword = '', $proxyAuthType = 1, $method='http') { - //trigger_error('Method ' . __METHOD__ . ' is deprecated', E_USER_DEPRECATED); + $this->logDeprecation('Method ' . __METHOD__ . ' is deprecated'); return $this->sendPayloadSocket($req, $server, $port, $timeout, $username, $password, $authType, null, null, null, null, $proxyHost, $proxyPort, $proxyUsername, $proxyPassword, $proxyAuthType, $method); @@ -1090,7 +1090,7 @@ class Client $proxyUsername = '', $proxyPassword = '', $proxyAuthType = 1, $keepAlive = false, $key = '', $keyPass = '', $sslVersion = 0) { - //trigger_error('Method ' . __METHOD__ . ' is deprecated', E_USER_DEPRECATED); + $this->logDeprecation('Method ' . __METHOD__ . ' is deprecated'); return $this->sendPayloadCURL($req, $server, $port, $timeout, $username, $password, $authType, $cert, $certPass, $caCert, $caCertDir, $proxyHost, $proxyPort, diff --git a/src/Encoder.php b/src/Encoder.php index 0491447d..b897af5a 100644 --- a/src/Encoder.php +++ b/src/Encoder.php @@ -335,7 +335,7 @@ class Encoder if ($xmlRpcParser->_xh['isf'] > 1) { // test that $xmlrpc->_xh['value'] is an obj, too??? - $this->getLogger()->error($xmlRpcParser->_xh['isf_reason']); + $this->getLogger()->error('XML-RPC: ' . $xmlRpcParser->_xh['isf_reason']); return false; } diff --git a/src/Helper/Charset.php b/src/Helper/Charset.php index 54885614..d683140e 100644 --- a/src/Helper/Charset.php +++ b/src/Helper/Charset.php @@ -4,14 +4,14 @@ namespace PhpXmlRpc\Helper; use PhpXmlRpc\Exception\ValueErrorException; use PhpXmlRpc\PhpXmlRpc; -use PhpXmlRpc\Traits\LoggerAware; +use PhpXmlRpc\Traits\DeprecationLogger; /** * @todo implement an interface */ class Charset { - use LoggerAware; + use DeprecationLogger; // tables used for transcoding different charsets into us-ascii xml protected $xml_iso88591_Entities = array("in" => array(), "out" => array()); @@ -347,7 +347,7 @@ class Charset */ public function isValidCharset($encoding, $validList) { - //trigger_error('Method ' . __METHOD__ . ' is deprecated', E_USER_DEPRECATED); + $this->logDeprecation('Method ' . __METHOD__ . ' is deprecated'); if (is_string($validList)) { $validList = explode(',', $validList); @@ -377,7 +377,7 @@ class Charset */ public function getEntities($charset) { - //trigger_error('Method ' . __METHOD__ . ' is deprecated', E_USER_DEPRECATED); + $this->logDeprecation('Method ' . __METHOD__ . ' is deprecated'); switch ($charset) { diff --git a/src/Helper/Logger.php b/src/Helper/Logger.php index a4056e5c..a934611f 100644 --- a/src/Helper/Logger.php +++ b/src/Helper/Logger.php @@ -42,6 +42,18 @@ class Logger } } + /** + * Following the general principle of 'never break stdout', the default behaviour + * + * @param string $message + * @param $context + * @return void + */ + public function warning($message, $context = array()) + { + $this->errorLog(preg_replace('/^XML-RPC :/', 'XML-RPC Warning: ', $message)); + } + /** * Triggers the writing of a message to php's error log * @@ -51,7 +63,7 @@ class Logger */ public function error($message, $context = array()) { - $this->errorLog($message); + $this->errorLog(preg_replace('/^XML-RPC :/', 'XML-RPC Error: ', $message)); } // BC interface diff --git a/src/Helper/XMLParser.php b/src/Helper/XMLParser.php index 839ffd28..4e2d1878 100644 --- a/src/Helper/XMLParser.php +++ b/src/Helper/XMLParser.php @@ -3,7 +3,7 @@ namespace PhpXmlRpc\Helper; use PhpXmlRpc\PhpXmlRpc; -use PhpXmlRpc\Traits\LoggerAware; +use PhpXmlRpc\Traits\DeprecationLogger; use PhpXmlRpc\Value; /** @@ -20,7 +20,7 @@ use PhpXmlRpc\Value; */ class XMLParser { - use LoggerAware; + use DeprecationLogger; const RETURN_XMLRPCVALS = 'xmlrpcvals'; const RETURN_EPIVALS = 'epivals'; @@ -1012,14 +1012,14 @@ class XMLParser public function __set($name, $value) { - //trigger_error('setting property Response::' . $name . ' is deprecated', E_USER_DEPRECATED); - switch ($name) { // this should only ever be called by subclasses which overtook `parse()` case 'accept': + $this->logDeprecation('Setting property XMLParser::' . $name . ' is deprecated'); $this->current_parsing_options['accept'] = $value; break; default: + /// @todo throw instead? There are very few other places where the lib trigger errors which can potentially reach stdout... $trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS); trigger_error('Undefined property via __set(): ' . $name . ' in ' . $trace[0]['file'] . ' on line ' . $trace[0]['line'], E_USER_WARNING); } @@ -1027,10 +1027,9 @@ class XMLParser public function __isset($name) { - //trigger_error('checking property Response::' . $name . ' is deprecated', E_USER_DEPRECATED); - switch ($name) { case 'accept': + $this->logDeprecation('Checking property XMLParser::' . $name . ' is deprecated'); return isset($this->current_parsing_options['accept']); default: return false; @@ -1042,9 +1041,11 @@ class XMLParser switch ($name) { // q: does this make sense at all? case 'accept': + $this->logDeprecation('Unsetting property XMLParser::' . $name . ' is deprecated'); unset($this->current_parsing_options['accept']); break; default: + /// @todo throw instead? There are very few other places where the lib trigger errors which can potentially reach stdout... $trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS); trigger_error('Undefined property via __unset(): ' . $name . ' in ' . $trace[0]['file'] . ' on line ' . $trace[0]['line'], E_USER_WARNING); } diff --git a/src/PhpXmlRpc.php b/src/PhpXmlRpc.php index 66d95134..e8b8035a 100644 --- a/src/PhpXmlRpc.php +++ b/src/PhpXmlRpc.php @@ -93,7 +93,6 @@ class PhpXmlRpc * received charset cannot be determined and mbstring extension is not enabled. */ public static $xmlrpc_defencoding = "UTF-8"; - /** * @var string[] * The list of preferred encodings used by the server for requests and by the client for responses to detect the @@ -102,7 +101,6 @@ class PhpXmlRpc * - mbstring extension is enabled */ public static $xmlrpc_detectencodings = array(); - /** * @var string * The encoding used internally by PHP. @@ -209,10 +207,18 @@ class PhpXmlRpc */ public static $xmlrpc_methodname_format = '|^[ \t]*[a-zA-Z0-9_.:/]+[ \t]*$|'; + /** + * @var bool + * Set this to false to have a warning added to the log whenever user code uses a deprecated method/parameter/property + */ + public static $xmlrpc_silence_deprecations = true; + /** * A function to be used for compatibility with legacy code: it creates all global variables which used to be declared, * such as library version etc... * @return void + * + * @deprecated */ public static function exportGlobals() { @@ -255,6 +261,8 @@ class PhpXmlRpc * 4. run your own code. * * @return void + * + * @deprecated */ public static function importGlobals() { diff --git a/src/Request.php b/src/Request.php index 4a68454c..2dbcf18b 100644 --- a/src/Request.php +++ b/src/Request.php @@ -6,7 +6,7 @@ use PhpXmlRpc\Exception\HttpException; use PhpXmlRpc\Helper\Http; use PhpXmlRpc\Helper\XMLParser; use PhpXmlRpc\Traits\CharsetEncoderAware; -use PhpXmlRpc\Traits\LoggerAware; +use PhpXmlRpc\Traits\DeprecationLogger; use PhpXmlRpc\Traits\ParserAware; /** @@ -18,7 +18,7 @@ use PhpXmlRpc\Traits\ParserAware; class Request { use CharsetEncoderAware; - use LoggerAware; + use DeprecationLogger; use ParserAware; /// @todo: do these need to be public? diff --git a/src/Response.php b/src/Response.php index 210f8b52..d577bea5 100644 --- a/src/Response.php +++ b/src/Response.php @@ -4,7 +4,7 @@ namespace PhpXmlRpc; use PhpXmlRpc\Exception\StateErrorException; use PhpXmlRpc\Traits\CharsetEncoderAware; -use PhpXmlRpc\Traits\LoggerAware; +use PhpXmlRpc\Traits\DeprecationLogger; /** * This class provides the representation of the response of an XML-RPC server. @@ -18,7 +18,7 @@ use PhpXmlRpc\Traits\LoggerAware; class Response { use CharsetEncoderAware; - use LoggerAware; + use DeprecationLogger; /// @todo: do these need to be public? /** @internal */ @@ -194,16 +194,18 @@ class Response // we have to make this return by ref in order to allow calls such as `$resp->_cookies['name'] = ['value' => 'something'];` public function &__get($name) { - //trigger_error('getting property Response::' . $name . ' is deprecated', E_USER_DEPRECATED); - switch ($name) { case 'hdrs': + $this->logDeprecation('Getting property Response::' . $name . ' is deprecated'); return $this->httpResponse['headers']; case '_cookies': + $this->logDeprecation('Getting property Response::' . $name . ' is deprecated'); return $this->httpResponse['cookies']; case 'raw_data': + $this->logDeprecation('Getting property Response::' . $name . ' is deprecated'); return $this->httpResponse['raw_data']; default: + /// @todo throw instead? There are very few other places where the lib trigger errors which can potentially reach stdout... $trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS); trigger_error('Undefined property via __get(): ' . $name . ' in ' . $trace[0]['file'] . ' on line ' . $trace[0]['line'], E_USER_WARNING); return null; @@ -212,19 +214,21 @@ class Response public function __set($name, $value) { - //trigger_error('setting property Response::' . $name . ' is deprecated', E_USER_DEPRECATED); - switch ($name) { case 'hdrs': + $this->logDeprecation('Setting property Response::' . $name . ' is deprecated'); $this->httpResponse['headers'] = $value; break; case '_cookies': + $this->logDeprecation('Setting property Response::' . $name . ' is deprecated'); $this->httpResponse['cookies'] = $value; break; case 'raw_data': + $this->logDeprecation('Setting property Response::' . $name . ' is deprecated'); $this->httpResponse['raw_data'] = $value; break; default: + /// @todo throw instead? There are very few other places where the lib trigger errors which can potentially reach stdout... $trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS); trigger_error('Undefined property via __set(): ' . $name . ' in ' . $trace[0]['file'] . ' on line ' . $trace[0]['line'], E_USER_WARNING); } @@ -232,14 +236,15 @@ class Response public function __isset($name) { - //trigger_error('checking property Response::' . $name . ' is deprecated', E_USER_DEPRECATED); - switch ($name) { case 'hdrs': + $this->logDeprecation('Checking property Response::' . $name . ' is deprecated'); return isset($this->httpResponse['headers']); case '_cookies': + $this->logDeprecation('Checking property Response::' . $name . ' is deprecated'); return isset($this->httpResponse['cookies']); case 'raw_data': + $this->logDeprecation('Checking property Response::' . $name . ' is deprecated'); return isset($this->httpResponse['raw_data']); default: return false; @@ -250,15 +255,19 @@ class Response { switch ($name) { case 'hdrs': + $this->logDeprecation('Unsetting property Response::' . $name . ' is deprecated'); unset($this->httpResponse['headers']); break; case '_cookies': + $this->logDeprecation('Unsetting property Response::' . $name . ' is deprecated'); unset($this->httpResponse['cookies']); break; case 'raw_data': + $this->logDeprecation('Unsetting property Response::' . $name . ' is deprecated'); unset($this->httpResponse['raw_data']); break; default: + /// @todo throw instead? There are very few other places where the lib trigger errors which can potentially reach stdout... $trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS); trigger_error('Undefined property via __unset(): ' . $name . ' in ' . $trace[0]['file'] . ' on line ' . $trace[0]['line'], E_USER_WARNING); } diff --git a/src/Server.php b/src/Server.php index a9f35fa2..85fbaa4c 100644 --- a/src/Server.php +++ b/src/Server.php @@ -9,7 +9,7 @@ use PhpXmlRpc\Helper\Interop; use PhpXmlRpc\Helper\Logger; use PhpXmlRpc\Helper\XMLParser; use PhpXmlRpc\Traits\CharsetEncoderAware; -use PhpXmlRpc\Traits\LoggerAware; +use PhpXmlRpc\Traits\DeprecationLogger; use PhpXmlRpc\Traits\ParserAware; /** @@ -18,7 +18,7 @@ use PhpXmlRpc\Traits\ParserAware; class Server { use CharsetEncoderAware; - use LoggerAware; + use DeprecationLogger; use ParserAware; const OPT_ACCEPTED_COMPRESSION = 'accepted_compression'; diff --git a/src/Traits/DeprecationLogger.php b/src/Traits/DeprecationLogger.php new file mode 100644 index 00000000..a499789a --- /dev/null +++ b/src/Traits/DeprecationLogger.php @@ -0,0 +1,19 @@ +getLogger()->warning('XML-RPC Deprecated: ' . $message); + } +} diff --git a/src/Value.php b/src/Value.php index 11167971..9ec2cfc4 100644 --- a/src/Value.php +++ b/src/Value.php @@ -6,7 +6,7 @@ use PhpXmlRpc\Exception\StateErrorException; use PhpXmlRpc\Exception\TypeErrorException; use PhpXmlRpc\Exception\ValueErrorException; use PhpXmlRpc\Traits\CharsetEncoderAware; -use PhpXmlRpc\Traits\LoggerAware; +use PhpXmlRpc\Traits\DeprecationLogger; /** * This class enables the creation of values for XML-RPC, by encapsulating plain php values. @@ -14,7 +14,7 @@ use PhpXmlRpc\Traits\LoggerAware; class Value implements \Countable, \IteratorAggregate, \ArrayAccess { use CharsetEncoderAware; - use LoggerAware; + use DeprecationLogger; public static $xmlrpcI4 = "i4"; public static $xmlrpcI8 = "i8"; @@ -373,7 +373,7 @@ class Value implements \Countable, \IteratorAggregate, \ArrayAccess */ public function structMemExists($key) { - //trigger_error('Method ' . __METHOD__ . ' is deprecated', E_USER_DEPRECATED); + $this->logDeprecation('Method ' . __METHOD__ . ' is deprecated'); return array_key_exists($key, $this->me['struct']); } @@ -389,7 +389,7 @@ class Value implements \Countable, \IteratorAggregate, \ArrayAccess */ public function structMem($key) { - //trigger_error('Method ' . __METHOD__ . ' is deprecated', E_USER_DEPRECATED); + $this->logDeprecation('Method ' . __METHOD__ . ' is deprecated'); return $this->me['struct'][$key]; } @@ -402,7 +402,7 @@ class Value implements \Countable, \IteratorAggregate, \ArrayAccess */ public function structReset() { - //trigger_error('Method ' . __METHOD__ . ' is deprecated', E_USER_DEPRECATED); + $this->logDeprecation('Method ' . __METHOD__ . ' is deprecated'); reset($this->me['struct']); } @@ -417,7 +417,7 @@ class Value implements \Countable, \IteratorAggregate, \ArrayAccess */ public function structEach() { - //trigger_error('Method ' . __METHOD__ . ' is deprecated', E_USER_DEPRECATED); + $this->logDeprecation('Method ' . __METHOD__ . ' is deprecated'); return @each($this->me['struct']); } @@ -462,7 +462,7 @@ class Value implements \Countable, \IteratorAggregate, \ArrayAccess */ public function arrayMem($key) { - //trigger_error('Method ' . __METHOD__ . ' is deprecated', E_USER_DEPRECATED); + $this->logDeprecation('Method ' . __METHOD__ . ' is deprecated'); return $this->me['array'][$key]; } @@ -476,7 +476,7 @@ class Value implements \Countable, \IteratorAggregate, \ArrayAccess */ public function arraySize() { - //trigger_error('Method ' . __METHOD__ . ' is deprecated', E_USER_DEPRECATED); + $this->logDeprecation('Method ' . __METHOD__ . ' is deprecated'); return count($this->me['array']); } @@ -490,7 +490,7 @@ class Value implements \Countable, \IteratorAggregate, \ArrayAccess */ public function structSize() { - //trigger_error('Method ' . __METHOD__ . ' is deprecated', E_USER_DEPRECATED); + $this->logDeprecation('Method ' . __METHOD__ . ' is deprecated'); return count($this->me['struct']); }