bump to php 5.4, use traits
authorgggeek <giunta.gaetano@gmail.com>
Tue, 24 Jan 2023 13:42:17 +0000 (13:42 +0000)
committergggeek <giunta.gaetano@gmail.com>
Tue, 24 Jan 2023 13:42:17 +0000 (13:42 +0000)
16 files changed:
NEWS.md
composer.json
src/Client.php
src/Encoder.php
src/Helper/Charset.php
src/Helper/Http.php
src/Helper/XMLParser.php
src/Request.php
src/Response.php
src/Server.php
src/Traits/CharsetEncoderAware.php [new file with mode: 0644]
src/Traits/LoggerAware.php [new file with mode: 0644]
src/Traits/ParserAware.php [new file with mode: 0644]
src/Value.php
src/Wrapper.php
tests/04LoggerTest.php

diff --git a/NEWS.md b/NEWS.md
index 44ab8d3..6e0f5a8 100644 (file)
--- 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.
 
     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
 
index 344e963..6762b50 100644 (file)
@@ -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?",
index 2430f74..571b56d 100644 (file)
@@ -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)
index 03fabed..bcb3971 100644 (file)
@@ -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.
index 92bdcec..0bced4f 100644 (file)
@@ -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.
      */
index 829ad50..0e30556 100644 (file)
@@ -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.
index 0a35586..019d003 100644 (file)
@@ -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:
index f5ae68b..a715991 100644 (file)
@@ -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)
index c20f9af..0ccb46a 100644 (file)
@@ -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
index f8137a4..76ccd54 100644 (file)
@@ -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 (file)
index 0000000..b51ba17
--- /dev/null
@@ -0,0 +1,27 @@
+<?php
+
+namespace PhpXmlRpc\Traits;
+
+use PhpXmlRpc\Helper\Charset;
+
+trait CharsetEncoderAware
+{
+    protected static $charsetEncoder;
+
+    public function getCharsetEncoder()
+    {
+        if (self::$charsetEncoder === null) {
+            self::$charsetEncoder = Charset::instance();
+        }
+        return self::$charsetEncoder;
+    }
+
+    /**
+     * @param $charsetEncoder
+     * @return void
+     */
+    public static function setCharsetEncoder($charsetEncoder)
+    {
+        self::$charsetEncoder = $charsetEncoder;
+    }
+}
diff --git a/src/Traits/LoggerAware.php b/src/Traits/LoggerAware.php
new file mode 100644 (file)
index 0000000..5d47e8c
--- /dev/null
@@ -0,0 +1,27 @@
+<?php
+
+namespace PhpXmlRpc\Traits;
+
+use PhpXmlRpc\Helper\Logger;
+
+trait LoggerAware
+{
+    protected static $logger;
+
+    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;
+    }
+}
diff --git a/src/Traits/ParserAware.php b/src/Traits/ParserAware.php
new file mode 100644 (file)
index 0000000..24ab1c2
--- /dev/null
@@ -0,0 +1,27 @@
+<?php
+
+namespace PhpXmlRpc\Traits;
+
+use PhpXmlRpc\Helper\XMLParser;
+
+trait ParserAware
+{
+    protected static $parser;
+
+    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;
+    }
+}
index 708fef7..f0203e0 100644 (file)
@@ -2,14 +2,17 @@
 
 namespace PhpXmlRpc;
 
-use PhpXmlRpc\Helper\Charset;
-use PhpXmlRpc\Helper\Logger;
+use PhpXmlRpc\Traits\CharsetEncoderAware;
+use PhpXmlRpc\Traits\LoggerAware;
 
 /**
  * This class enables the creation of values for XML-RPC, by encapsulating plain php values.
  */
 class Value implements \Countable, \IteratorAggregate, \ArrayAccess
 {
+    use CharsetEncoderAware;
+    use LoggerAware;
+
     public static $xmlrpcI4 = "i4";
     public static $xmlrpcI8 = "i8";
     public static $xmlrpcInt = "int";
@@ -37,9 +40,6 @@ class Value implements \Countable, \IteratorAggregate, \ArrayAccess
         "null" => 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.
      *
index a04972f..8beb5b3 100644 (file)
@@ -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.
index d1c8cbe..b857997 100644 (file)
@@ -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('<?xml version="1.0" ?><methodResponse><params><param><value><boolean>x</boolean></value></param></params></methodResponse>');
+        $o = ob_get_clean();
+        $this->assertEquals('', $o);
         $this->assertStringContainsString("invalid data received in BOOLEAN value", $this->errorBuffer);
 
         XMLParser::setLogger($l);