simplify global logger injection
authorgggeek <giunta.gaetano@gmail.com>
Tue, 24 Jan 2023 18:46:00 +0000 (18:46 +0000)
committergggeek <giunta.gaetano@gmail.com>
Tue, 24 Jan 2023 18:46:00 +0000 (18:46 +0000)
NEWS.md
demo/client/loggerinjection.php
src/PhpXmlRpc.php

diff --git a/NEWS.md b/NEWS.md
index 8b8e3ae..910431a 100644 (file)
--- a/NEWS.md
+++ b/NEWS.md
@@ -66,6 +66,8 @@
 * new: it is now possible to inject a custom logger into helper classes `Charset`, `Http`, `XMLParser`, inching a step
   closer to supporting DIC patterns
 
+* new: method `PhpXmlRpc::setLogger()`, to simplify injecting the logger into all classes of the library in one step
+
 * new: passing value -1 to `$client->setDebug` will avoid storing the full http response data in the returned Response
   object when executing `call`. This could be useful in reducing memory usage for big responses
 
index b5f89a2..2791922 100644 (file)
@@ -7,9 +7,7 @@ require_once __DIR__ . "/_prepend.php";
 
 use PhpXmlRpc\Client;
 use PhpXmlRpc\Encoder;
-use PhpXmlRpc\Helper\Charset;
-use PhpXmlRpc\Helper\Http;
-use PhpXmlRpc\Helper\XMLParser;
+use PhpXmlRpc\PhpXmlRpc;
 use PhpXmlRpc\Request;
 
 // Definition of a custom logger implementing the same API as the default one
@@ -48,12 +46,7 @@ $logger = new MyLogger();
 
 // inject it into all the classes (possibly) involved
 
-Charset::setLogger($logger);
-Client::setLogger($logger);
-Encoder::setLogger($logger);
-Http::setLogger($logger);
-Request::setLogger($logger);
-XMLParser::setLogger($logger);
+PhpXmlRpc::setLogger($logger);
 
 // then send a request
 
index 1976db1..b2c96dd 100644 (file)
@@ -3,6 +3,8 @@
 namespace PhpXmlRpc;
 
 use PhpXmlRpc\Helper\Charset;
+use PhpXmlRpc\Helper\Http;
+use PhpXmlRpc\Helper\XMLParser;
 
 /**
  * Manages global configuration for operation of the library.
@@ -258,4 +260,23 @@ class PhpXmlRpc
             }
         }
     }
+
+    /**
+     * Inject a logger into all classes of the PhpXmlRpc library which use one
+     *
+     * @param $logger
+     * @return void
+     */
+    public static function setLogger($logger)
+    {
+        Charset::setLogger($logger);
+        Client::setLogger($logger);
+        Encoder::setLogger($logger);
+        Http::setLogger($logger);
+        Request::setLogger($logger);
+        Server::setLogger($logger);
+        Value::setLogger($logger);
+        Wrapper::setLogger($logger);
+        XMLParser::setLogger($logger);
+    }
 }