Move from static logging calls to a singleton
authorgggeek <giunta.gaetano@gmail.com>
Sun, 12 Apr 2015 11:43:38 +0000 (12:43 +0100)
committergggeek <giunta.gaetano@gmail.com>
Sun, 12 Apr 2015 11:43:38 +0000 (12:43 +0100)
src/Client.php
src/Helper/Http.php
src/Helper/Logger.php
src/Request.php

index d71ef27..79948e8 100644 (file)
@@ -553,7 +553,7 @@ class Client
             $payload;
 
         if ($this->debug > 1) {
-            Logger::debugMessage("---SENDING---\n$op\n---END---");
+            Logger::instance()->debugMessage("---SENDING---\n$op\n---END---");
         }
 
         if ($timeout > 0) {
@@ -709,7 +709,7 @@ class Client
         }
 
         if ($this->debug > 1) {
-            Logger::debugMessage("---SENDING---\n$payload\n---END---");
+            Logger::instance()->debugMessage("---SENDING---\n$payload\n---END---");
         }
 
         if (!$keepAlive || !$this->xmlrpc_curl_handle) {
@@ -848,7 +848,7 @@ class Client
                 $message .= $name . ': ' . $val . "\n";
             }
             $message .= "---END---";
-            Logger::debugMessage($message);
+            Logger::instance()->debugMessage($message);
         }
 
         if (!$result) {
index 0fa3f51..2faf583 100644 (file)
@@ -3,14 +3,12 @@
 namespace PhpXmlRpc\Helper;
 
 use PhpXmlRpc\PhpXmlRpc;
-use PhpXmlRpc\Helper\Logger;
 
 class Http
 {
     /**
-     * Decode a string that is encoded w/ "chunked" transfer encoding
-     * as defined in rfc2068 par. 19.4.6
-     * code shamelessly stolen from nusoap library by Dietrich Ayala.
+     * Decode a string that is encoded with "chunked" transfer encoding as defined in rfc2068 par. 19.4.6
+     * Code shamelessly stolen from nusoap library by Dietrich Ayala.
      *
      * @param string $buffer the string to be decoded
      *
@@ -199,7 +197,7 @@ class Http
             foreach ($httpResponse['cookies'] as $header => $value) {
                 $msg .= "COOKIE: $header={$value['value']}\n";
             }
-            Logger::debugMessage($msg);
+            Logger::instance()->debugMessage($msg);
         }
 
         // if CURL was used for the call, http headers have been processed,
@@ -223,12 +221,12 @@ class Http
                         if ($httpResponse['headers']['content-encoding'] == 'deflate' && $degzdata = @gzuncompress($data)) {
                             $data = $degzdata;
                             if ($debug) {
-                                Logger::debugMessage("---INFLATED RESPONSE---[" . strlen($data) . " chars]---\n$data\n---END---");
+                                Logger::instance()->debugMessage("---INFLATED RESPONSE---[" . strlen($data) . " chars]---\n$data\n---END---");
                             }
                         } elseif ($httpResponse['headers']['content-encoding'] == 'gzip' && $degzdata = @gzinflate(substr($data, 10))) {
                             $data = $degzdata;
                             if ($debug) {
-                                Logger::debugMessage("---INFLATED RESPONSE---[" . strlen($data) . " chars]---\n$data\n---END---");
+                                Logger::instance()->debugMessage("---INFLATED RESPONSE---[" . strlen($data) . " chars]---\n$data\n---END---");
                             }
                         } else {
                             error_log('XML-RPC: ' . __METHOD__ . ': errors occurred when trying to decode the deflated data received from server');
index cb0ab10..0194765 100644 (file)
@@ -11,6 +11,22 @@ namespace PhpXmlRpc\Helper;
 
 class Logger
 {
+    protected static $instance = null;
+
+    /**
+     * This class is singleton, so that later we can move to DI patterns.
+     *
+     * @return Charset
+     */
+    public static function instance()
+    {
+        if (self::$instance === null) {
+            self::$instance = new self();
+        }
+
+        return self::$instance;
+    }
+
     /**
      * Echoes a debug message, taking care of escaping it when not in console mode.
      * NB: if the encoding of the message is not known or wrong, and we are working in web mode, there is no guarantee
@@ -19,7 +35,7 @@ class Logger
      * @param string $message
      * @param string $encoding
      */
-    public static function debugMessage($message, $encoding=null)
+    public function debugMessage($message, $encoding=null)
     {
         if (PHP_SAPI != 'cli') {
             $flags = ENT_COMPAT | ENT_HTML401 | ENT_SUBSTITUTE;
index 6eb7671..8b367f1 100644 (file)
@@ -169,7 +169,7 @@ class Request
     public function parseResponse($data = '', $headersProcessed = false, $returnType = 'xmlrpcvals')
     {
         if ($this->debug) {
-            Logger::debugMessage("---GOT---\n$data\n---END---");
+            Logger::instance()->debugMessage("---GOT---\n$data\n---END---");
         }
 
         $this->httpResponse = array('raw_data' => $data, 'headers' => array(), 'cookies' => array());
@@ -215,7 +215,7 @@ class Request
                 $start += strlen('<!-- SERVER DEBUG INFO (BASE64 ENCODED):');
                 $end = strpos($data, '-->', $start);
                 $comments = substr($data, $start, $end - $start);
-                Logger::debugMessage("---SERVER DEBUG INFO (DECODED) ---\n\t" .
+                Logger::instance()->debugMessage("---SERVER DEBUG INFO (DECODED) ---\n\t" .
                     str_replace("\n", "\n\t", base64_decode($comments)) . "\n---END---", $respEncoding);
             }
         }
@@ -318,7 +318,7 @@ class Request
                 PhpXmlRpc::$xmlrpcstr['invalid_return']);
         } else {
             if ($this->debug) {
-                Logger::debugMessage(
+                Logger::instance()->debugMessage(
                     "---PARSED---\n".var_export($xmlRpcParser->_xh['value'], true)."\n---END---"
                 );
             }