From 9f5262c36d619b17391a8e0ccf910fe14d533488 Mon Sep 17 00:00:00 2001 From: gggeek Date: Tue, 24 Jan 2023 13:42:17 +0000 Subject: [PATCH] bump to php 5.4, use traits --- NEWS.md | 5 ++- composer.json | 2 +- src/Client.php | 22 ++-------- src/Encoder.php | 41 ++----------------- src/Helper/Charset.php | 20 +--------- src/Helper/Http.php | 23 +---------- src/Helper/XMLParser.php | 22 ++-------- src/Request.php | 64 +++--------------------------- src/Response.php | 23 +---------- src/Server.php | 63 +++-------------------------- src/Traits/CharsetEncoderAware.php | 27 +++++++++++++ src/Traits/LoggerAware.php | 27 +++++++++++++ src/Traits/ParserAware.php | 27 +++++++++++++ src/Value.php | 46 +++------------------ src/Wrapper.php | 23 ++--------- tests/04LoggerTest.php | 13 +++++- 16 files changed, 133 insertions(+), 315 deletions(-) create mode 100644 src/Traits/CharsetEncoderAware.php create mode 100644 src/Traits/LoggerAware.php create mode 100644 src/Traits/ParserAware.php diff --git a/NEWS.md b/NEWS.md index 44ab8d3d..6e0f5a87 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,7 @@ ## XML-RPC for PHP version 4.xx - unreleased +* changed: the minimum php version required has increased to 5.4 + * new: allow to specify other charsets than the canonical three (UTF-8, ISO-8859-1, ASCII), when mbstring is available, both for outgoing and incoming data. @@ -137,7 +139,8 @@ 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` - + - traits have been introduced for all classes dealing with Logger, XMLParser and CharsetEncoder; method `setCharsetEncoder` + is now static ## XML-RPC for PHP version 4.9.5 - 2023/01/11 diff --git a/composer.json b/composer.json index 344e9633..6762b50c 100644 --- a/composer.json +++ b/composer.json @@ -5,7 +5,7 @@ "homepage": "https://gggeek.github.io/phpxmlrpc/", "keywords": [ "xmlrpc", "xml-rpc","webservices" ], "require": { - "php": "^5.3.0 || ^7.0 || ^8.0", + "php": "^5.4.0 || ^7.0 || ^8.0", "ext-xml": "*" }, "_comment::tests": "The dev packages below require a minimum of php 5.4, even though we support php 5.3. Can we manage to do better?", diff --git a/src/Client.php b/src/Client.php index 2430f743..571b56d7 100644 --- a/src/Client.php +++ b/src/Client.php @@ -3,8 +3,8 @@ namespace PhpXmlRpc; //use PhpXmlRpc\Helper\Charset; -use PhpXmlRpc\Helper\Logger; use PhpXmlRpc\Helper\XMLParser; +use PhpXmlRpc\Traits\LoggerAware; /** * Used to represent a client of an XML-RPC server. @@ -13,11 +13,12 @@ use PhpXmlRpc\Helper\XMLParser; */ class Client { + use LoggerAware; + const USE_CURL_NEVER = 0; const USE_CURL_ALWAYS = 1; const USE_CURL_AUTO = 2; - protected static $logger; /** @var string */ protected static $requestClass = '\\PhpXmlRpc\\Request'; /** @var string */ @@ -251,23 +252,6 @@ class Client */ public $xmlrpc_curl_handle = null; - public function getLogger() - { - if (self::$logger === null) { - self::$logger = Logger::instance(); - } - return self::$logger; - } - - /** - * @param $logger - * @return void - */ - public static function setLogger($logger) - { - self::$logger = $logger; - } - /** * @param string $path either the PATH part of the xml-rpc server URL, or complete server URL (in which case you * should use and empty string for all other parameters) diff --git a/src/Encoder.php b/src/Encoder.php index 03fabed9..bcb3971f 100644 --- a/src/Encoder.php +++ b/src/Encoder.php @@ -2,8 +2,9 @@ namespace PhpXmlRpc; -use PhpXmlRpc\Helper\Logger; use PhpXmlRpc\Helper\XMLParser; +use PhpXmlRpc\Traits\LoggerAware; +use PhpXmlRpc\Traits\ParserAware; /** * A helper class to easily convert between Value objects and php native values. @@ -13,42 +14,8 @@ use PhpXmlRpc\Helper\XMLParser; */ class Encoder { - protected static $logger; - protected static $parser; - - public function getLogger() - { - if (self::$logger === null) { - self::$logger = Logger::instance(); - } - return self::$logger; - } - - /** - * @param $logger - * @return void - */ - public static function setLogger($logger) - { - self::$logger = $logger; - } - - public function getParser() - { - if (self::$parser === null) { - self::$parser = new XMLParser(); - } - return self::$parser; - } - - /** - * @param $parser - * @return void - */ - public static function setParser($parser) - { - self::$parser = $parser; - } + use LoggerAware; + use ParserAware; /** * Takes an xml-rpc Value in object instance and translates it into native PHP types, recursively. diff --git a/src/Helper/Charset.php b/src/Helper/Charset.php index 92bdcec9..0bced4f6 100644 --- a/src/Helper/Charset.php +++ b/src/Helper/Charset.php @@ -3,13 +3,14 @@ namespace PhpXmlRpc\Helper; use PhpXmlRpc\PhpXmlRpc; +use PhpXmlRpc\Traits\LoggerAware; /** * @todo implement an interface */ class Charset { - protected static $logger; + use LoggerAware; // tables used for transcoding different charsets into us-ascii xml protected $xml_iso88591_Entities = array("in" => array(), "out" => array()); @@ -43,23 +44,6 @@ class Charset return self::$instance; } - public function getLogger() - { - if (self::$logger === null) { - self::$logger = Logger::instance(); - } - return self::$logger; - } - - /** - * @param $logger - * @return void - */ - public static function setLogger($logger) - { - self::$logger = $logger; - } - /** * Force usage as singleton. */ diff --git a/src/Helper/Http.php b/src/Helper/Http.php index 829ad50e..0e30556c 100644 --- a/src/Helper/Http.php +++ b/src/Helper/Http.php @@ -4,30 +4,11 @@ namespace PhpXmlRpc\Helper; use PhpXmlRpc\Exception\HttpException; use PhpXmlRpc\PhpXmlRpc; +use PhpXmlRpc\Traits\LoggerAware; -/** - * - */ class Http { - protected static $logger; - - public static function getLogger() - { - if (self::$logger === null) { - self::$logger = Logger::instance(); - } - return self::$logger; - } - - /** - * @param $logger - * @return void - */ - public static function setLogger($logger) - { - self::$logger = $logger; - } + use LoggerAware; /** * Decode a string that is encoded with "chunked" transfer encoding as defined in rfc2068 par. 19.4.6. diff --git a/src/Helper/XMLParser.php b/src/Helper/XMLParser.php index 0a355863..019d003f 100644 --- a/src/Helper/XMLParser.php +++ b/src/Helper/XMLParser.php @@ -3,6 +3,7 @@ namespace PhpXmlRpc\Helper; use PhpXmlRpc\PhpXmlRpc; +use PhpXmlRpc\Traits\LoggerAware; use PhpXmlRpc\Value; /** @@ -19,6 +20,8 @@ use PhpXmlRpc\Value; */ class XMLParser { + use LoggerAware; + const RETURN_XMLRPCVALS = 'xmlrpcvals'; const RETURN_EPIVALS = 'epivals'; const RETURN_PHP = 'phpvals'; @@ -28,8 +31,6 @@ class XMLParser const ACCEPT_VALUE = 4; const ACCEPT_FAULT = 8; - protected static $logger; - /** * @var int * The max length beyond which data will get truncated in error messages @@ -107,23 +108,6 @@ class XMLParser /** @var array supported keys: accept, target_charset, methodname_callback, xmlrpc_null_extension, xmlrpc_return_datetimes */ protected $current_parsing_options = array(); - public function getLogger() - { - if (self::$logger === null) { - self::$logger = Logger::instance(); - } - return self::$logger; - } - - /** - * @param $logger - * @return void - */ - public static function setLogger($logger) - { - self::$logger = $logger; - } - /** * @param array $options integer keys: options passed to the xml parser * string keys: diff --git a/src/Request.php b/src/Request.php index f5ae68b5..a7159916 100644 --- a/src/Request.php +++ b/src/Request.php @@ -3,10 +3,11 @@ namespace PhpXmlRpc; use PhpXmlRpc\Exception\HttpException; -use PhpXmlRpc\Helper\Charset; use PhpXmlRpc\Helper\Http; -use PhpXmlRpc\Helper\Logger; use PhpXmlRpc\Helper\XMLParser; +use PhpXmlRpc\Traits\CharsetEncoderAware; +use PhpXmlRpc\Traits\LoggerAware; +use PhpXmlRpc\Traits\ParserAware; /** * This class provides the representation of a request to an XML-RPC server. @@ -16,9 +17,9 @@ use PhpXmlRpc\Helper\XMLParser; */ class Request { - protected static $logger; - protected static $parser; - protected static $charsetEncoder; + use CharsetEncoderAware; + use LoggerAware; + use ParserAware; /// @todo: do these need to be public? public $payload; @@ -35,59 +36,6 @@ class Request /** @deprecated will be removed in a future release */ protected $httpResponse = array(); - public function getLogger() - { - if (self::$logger === null) { - self::$logger = Logger::instance(); - } - return self::$logger; - } - - /** - * @param $logger - * @return void - */ - public static function setLogger($logger) - { - self::$logger = $logger; - } - - public function getParser() - { - if (self::$parser === null) { - self::$parser = new XMLParser(); - } - return self::$parser; - } - - /** - * @param $parser - * @return void - */ - public static function setParser($parser) - { - self::$parser = $parser; - } - - public function getCharsetEncoder() - { - if (self::$charsetEncoder === null) { - self::$charsetEncoder = Charset::instance(); - } - return self::$charsetEncoder; - } - - /** - * @param $charsetEncoder - * @return void - * - * @todo this should be a static method - */ - public function setCharsetEncoder($charsetEncoder) - { - self::$charsetEncoder = $charsetEncoder; - } - /** * @param string $methodName the name of the method to invoke * @param Value[] $params array of parameters to be passed to the method (NB: Value objects, not plain php values) diff --git a/src/Response.php b/src/Response.php index c20f9af4..0ccb46af 100644 --- a/src/Response.php +++ b/src/Response.php @@ -2,7 +2,7 @@ namespace PhpXmlRpc; -use PhpXmlRpc\Helper\Charset; +use PhpXmlRpc\Traits\CharsetEncoderAware; /** * This class provides the representation of the response of an XML-RPC server. @@ -15,7 +15,7 @@ use PhpXmlRpc\Helper\Charset; */ class Response { - protected static $charsetEncoder; + use CharsetEncoderAware; /// @todo: do these need to be public? /** @internal */ @@ -31,25 +31,6 @@ class Response public $content_type = 'text/xml'; protected $httpResponse = array('headers' => array(), 'cookies' => array(), 'raw_data' => '', 'status_code' => null); - public function getCharsetEncoder() - { - if (self::$charsetEncoder === null) { - self::$charsetEncoder = Charset::instance(); - } - return self::$charsetEncoder; - } - - /** - * @param $charsetEncoder - * @return void - * - * @todo this should be a static method - */ - public function setCharsetEncoder($charsetEncoder) - { - self::$charsetEncoder = $charsetEncoder; - } - /** * @param Value|string|mixed $val either a Value object, a php value or the xml serialization of an xml-rpc value (a string) * @param integer $fCode set it to anything but 0 to create an error response. In that case, $val is discarded diff --git a/src/Server.php b/src/Server.php index f8137a4e..76ccd545 100644 --- a/src/Server.php +++ b/src/Server.php @@ -3,19 +3,21 @@ namespace PhpXmlRpc; use PhpXmlRpc\Exception\PhpXmlrpcException; -use PhpXmlRpc\Helper\Charset; use PhpXmlRpc\Helper\Http; use PhpXmlRpc\Helper\Logger; use PhpXmlRpc\Helper\XMLParser; +use PhpXmlRpc\Traits\CharsetEncoderAware; +use PhpXmlRpc\Traits\LoggerAware; +use PhpXmlRpc\Traits\ParserAware; /** * Allows effortless implementation of XML-RPC servers */ class Server { - protected static $logger; - protected static $parser; - protected static $charsetEncoder; + use CharsetEncoderAware; + use LoggerAware; + use ParserAware; /** * @var string @@ -118,59 +120,6 @@ class Server protected static $_xmlrpcs_occurred_errors = ''; protected static $_xmlrpcs_prev_ehandler = ''; - public function getLogger() - { - if (self::$logger === null) { - self::$logger = Logger::instance(); - } - return self::$logger; - } - - /** - * @param $logger - * @return void - */ - public static function setLogger($logger) - { - self::$logger = $logger; - } - - public function getParser() - { - if (self::$parser === null) { - self::$parser = new XMLParser(); - } - return self::$parser; - } - - /** - * @param $parser - * @return void - */ - public static function setParser($parser) - { - self::$parser = $parser; - } - - public function getCharsetEncoder() - { - if (self::$charsetEncoder === null) { - self::$charsetEncoder = Charset::instance(); - } - return self::$charsetEncoder; - } - - /** - * @param $charsetEncoder - * @return void - * - * @todo this should be a static method - */ - public function setCharsetEncoder($charsetEncoder) - { - self::$charsetEncoder = $charsetEncoder; - } - /** * @param array[] $dispatchMap the dispatch map with definition of exposed services * Array keys are the names of the method names. diff --git a/src/Traits/CharsetEncoderAware.php b/src/Traits/CharsetEncoderAware.php new file mode 100644 index 00000000..b51ba17a --- /dev/null +++ b/src/Traits/CharsetEncoderAware.php @@ -0,0 +1,27 @@ + 1, ); - protected static $logger; - protected static $charsetEncoder; - /// @todo: do these need to be public? /** @var Value[]|mixed */ public $me = array(); @@ -51,42 +51,6 @@ class Value implements \Countable, \IteratorAggregate, \ArrayAccess /** @var string|null */ public $_php_class = null; - public function getLogger() - { - if (self::$logger === null) { - self::$logger = Logger::instance(); - } - return self::$logger; - } - - /** - * @param $logger - * @return void - */ - public static function setLogger($logger) - { - self::$logger = $logger; - } - - public function getCharsetEncoder() - { - if (self::$charsetEncoder === null) { - self::$charsetEncoder = Charset::instance(); - } - return self::$charsetEncoder; - } - - /** - * @param $charsetEncoder - * @return void - * - * @todo this should be a static method - */ - public function setCharsetEncoder($charsetEncoder) - { - self::$charsetEncoder = $charsetEncoder; - } - /** * Build an xml-rpc value. * diff --git a/src/Wrapper.php b/src/Wrapper.php index a04972fa..8beb5b3b 100644 --- a/src/Wrapper.php +++ b/src/Wrapper.php @@ -7,7 +7,7 @@ namespace PhpXmlRpc; -use PhpXmlRpc\Helper\Logger; +use PhpXmlRpc\Traits\LoggerAware; /** * PHPXMLRPC "wrapper" class - generate stubs to transparently access xml-rpc methods as php functions and vice-versa. @@ -21,6 +21,8 @@ use PhpXmlRpc\Helper\Logger; */ class Wrapper { + use LoggerAware; + /** * @var object[] * Used to hold a reference to object instances whose methods get wrapped by wrapPhpFunction(), in 'create source' mode @@ -28,28 +30,9 @@ class Wrapper */ public static $objHolder = array(); - protected static $logger; - /** @var string */ protected static $namespace = '\\PhpXmlRpc\\'; - public function getLogger() - { - if (self::$logger === null) { - self::$logger = Logger::instance(); - } - return self::$logger; - } - - /** - * @param $logger - * @return void - */ - public static function setLogger($logger) - { - self::$logger = $logger; - } - /** * Given a string defining a php type or phpxmlrpc type (loosely defined: strings * accepted come from javadoc blocks), return corresponding phpxmlrpc type. diff --git a/tests/04LoggerTest.php b/tests/04LoggerTest.php index d1c8cbe8..b857997e 100644 --- a/tests/04LoggerTest.php +++ b/tests/04LoggerTest.php @@ -23,7 +23,10 @@ class LoggerTest extends PhpXmlRpc_PolyfillTestCase $l = $ch->getLogger(); Charset::setLogger($this); + ob_start(); $ch->encodeEntities('hello world', 'UTF-8', 'NOT-A-CHARSET'); + $o = ob_get_clean(); + $this->assertEquals('', $o); $this->assertStringContainsString("via mbstring: failed", $this->errorBuffer); Charset::setLogger($l); @@ -31,15 +34,18 @@ class LoggerTest extends PhpXmlRpc_PolyfillTestCase public function testHttpAltLogger() { - $l = Http::getLogger(); + $h = new Http(); + $l = $h->getLogger(); Http::setLogger($this); - $h = new Http(); $s = "HTTP/1.0 200 OK\r\n" . "Content-Type: unknown\r\n" . "\r\n" . "body"; + ob_start(); $h->parseResponseHeaders($s, false, 1); + $o = ob_get_clean(); + $this->assertEquals('', $o); $this->assertStringContainsString("HEADER: content-type: unknown", $this->debugBuffer); Http::setLogger($l); } @@ -50,7 +56,10 @@ class LoggerTest extends PhpXmlRpc_PolyfillTestCase $l = $xp->getLogger(); XMLParser::setLogger($this); + ob_start(); $xp->parse('x'); + $o = ob_get_clean(); + $this->assertEquals('', $o); $this->assertStringContainsString("invalid data received in BOOLEAN value", $this->errorBuffer); XMLParser::setLogger($l); -- 2.47.0