add a demo for logger injection
authorgggeek <giunta.gaetano@gmail.com>
Tue, 24 Jan 2023 10:30:15 +0000 (10:30 +0000)
committergggeek <giunta.gaetano@gmail.com>
Tue, 24 Jan 2023 10:30:15 +0000 (10:30 +0000)
NEWS.md
demo/client/_prepend.php
demo/client/loggerinjection.php [new file with mode: 0644]
tests/04LoggerTest.php
tests/10DemofilesTest.php

diff --git a/NEWS.md b/NEWS.md
index ba4c129..44ab8d3 100644 (file)
--- a/NEWS.md
+++ b/NEWS.md
 * improved: limit the size of incoming data which will be used in error responses and logged error messages, making
   it slightly harder to carry out DOS attacks against the library
 
-* fixed: when calling `Client::multicall()` with `$client->return_type = 'xml'`, we would be always falling back to
-  non-multicall requests
-
 * fixed: when a server is configured with its default value of 'xmlrpcvals' for `$functions_parameters_type`, and
   a method handler in the dispatch was defined with `'parameters_type' = 'phpvals'`, the handler would be passed a
   Request object instead of plain php values.
 
+* fixed: when calling `Client::multicall()` with `$client->return_type = 'xml'`, we would be always falling back to
+  non-multicall requests
+
 * fixed: receiving integers which use the '<EX:I8>' xml tag
 
 * new: added the `Client::setTimeout` method, meant to replace usage of the `$timeout` argument in calls to `send`
@@ -76,6 +76,8 @@
 * new: methods `Wrapper::holdObject()` and `Wrapper::getheldObject()`, allowing flexibility in storing object instances
   for code-generation scenarios involving `Wrapper::wrapPhpClass` and `Wrapper::wrapPhpFunction`
 
+* improved: made sure all debug output goes through the logger at response parsing time (there was one printf call left)
+
 * improved: all the Client's `setSomething()` methods now return the client object, allowing for usage of fluent style
   calling. The same applies to `Request::setDebug`
 
 
 * improved: the debugger will now sport the "load method synopsis" button when interacting with json-rpc servers
 
-* improved: made sure the test container has at least one locale with comma as decimal separator
+* improved: made sure the test container and gha test runners have at least one locale with comma as decimal separator
 
 * BC notes:
 
index 269b0dd..5f50d81 100644 (file)
@@ -21,5 +21,6 @@ if (isset($_SERVER['HTTPSERVER'])) {
 // A helper for cli vs web output:
 function output($text)
 {
-    echo PHP_SAPI == 'cli' ? strip_tags(str_replace('<br/>', "\n", $text)) : $text;
+    /// @todo we should only strip html tags, and let through all xml tags
+    echo PHP_SAPI == 'cli' ? strip_tags(str_replace(array('<br/>','<br>'), "\n", $text)) : $text;
 }
diff --git a/demo/client/loggerinjection.php b/demo/client/loggerinjection.php
new file mode 100644 (file)
index 0000000..9d2133c
--- /dev/null
@@ -0,0 +1,83 @@
+<?php
+require_once __DIR__ . "/_prepend.php";
+
+/**
+ * Demoing how to inject a custom logger for use by the library
+ */
+
+use PhpXmlRpc\Client;
+use PhpXmlRpc\Encoder;
+use PhpXmlRpc\Helper\Charset;
+use PhpXmlRpc\Helper\Http;
+use PhpXmlRpc\Helper\XMLParser;
+use PhpXmlRpc\Request;
+
+// Definition of a custom logger implementing the same API as the default one
+
+class MyLogger
+{
+    protected $debugBuffer = '';
+    protected $errorBuffer = '';
+
+    // logger API
+    public function debugMessage($message, $encoding = null)
+    {
+        $this->debugBuffer .= $message . "\n";
+    }
+
+    // logger API
+    public function errorLog($message)
+    {
+        $this->errorBuffer .= $message . "\n";
+    }
+
+    public function getDebug()
+    {
+        return $this->debugBuffer;
+    }
+
+    public function getError()
+    {
+        return $this->errorBuffer;
+    }
+}
+
+// create the custom logger instance
+
+$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);
+
+// then send a request
+
+$input = array(
+    array('name' => 'Dave', 'age' => 24),
+    array('name' => 'Edd',  'age' => 45),
+    array('name' => 'Joe',  'age' => 37),
+    array('name' => 'Fred', 'age' => 27),
+);
+
+$encoder = new Encoder();
+$client = new Client(XMLRPCSERVER);
+
+// set maximum debug level, to have all the communication details logged
+$client->setDebug(2);
+
+// avid compressed responses, as they mess up the output if echoed on the command-line
+$client->setAcceptedCompression('');
+
+// send request
+output("Sending the request. No output debug should appear below...<br>");
+$request = new Request('examples.sortByAge', array($encoder->encode($input)));
+$response = $client->send($request);
+output("Response received.<br>");
+
+output("The client error info is:<pre>\n" . $logger->getError() . "\n</pre>");
+output("The client debug info is:<pre>\n" . $logger->getDebug() . "\n</pre>");
index 479948f..d1c8cbe 100644 (file)
@@ -23,7 +23,6 @@ class LoggerTest extends PhpXmlRpc_PolyfillTestCase
         $l = $ch->getLogger();
         Charset::setLogger($this);
 
-        // silence the mbstring warning
         $ch->encodeEntities('hello world', 'UTF-8', 'NOT-A-CHARSET');
         $this->assertStringContainsString("via mbstring: failed", $this->errorBuffer);
 
index 1dfef48..eb4474b 100644 (file)
@@ -41,6 +41,11 @@ class DemoFilesTest extends PhpXmlRpc_WebTestCase
         $page = $this->request('?demo=client/getstatename.php', 'POST', array('stateno' => '1'));
     }
 
+    public function testLoggerInjection()
+    {
+        $page = $this->request('?demo=client/loggerinjection.php');
+    }
+
     public function testIntrospect()
     {
         $page = $this->request('?demo=client/introspect.php');