From e665f3691b5eee236217384b7769ad56f47b1933 Mon Sep 17 00:00:00 2001 From: gggeek Date: Tue, 24 Jan 2023 18:46:00 +0000 Subject: [PATCH] simplify global logger injection --- NEWS.md | 2 ++ demo/client/loggerinjection.php | 11 ++--------- src/PhpXmlRpc.php | 21 +++++++++++++++++++++ 3 files changed, 25 insertions(+), 9 deletions(-) diff --git a/NEWS.md b/NEWS.md index 8b8e3aef..910431a7 100644 --- 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 diff --git a/demo/client/loggerinjection.php b/demo/client/loggerinjection.php index b5f89a28..2791922d 100644 --- a/demo/client/loggerinjection.php +++ b/demo/client/loggerinjection.php @@ -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 diff --git a/src/PhpXmlRpc.php b/src/PhpXmlRpc.php index 1976db16..b2c96dde 100644 --- a/src/PhpXmlRpc.php +++ b/src/PhpXmlRpc.php @@ -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); + } } -- 2.47.0