refactor access to req/resp payload
authorgggeek <giunta.gaetano@gmail.com>
Thu, 2 Feb 2023 12:38:55 +0000 (12:38 +0000)
committergggeek <giunta.gaetano@gmail.com>
Thu, 2 Feb 2023 12:38:55 +0000 (12:38 +0000)
debugger/action.php
src/Client.php
src/Request.php
src/Response.php
src/Server.php
src/Traits/PayloadBearer.php [new file with mode: 0644]

index 83205c4..7b6534e 100644 (file)
@@ -214,25 +214,28 @@ if ($action) {
                 $msg[0] = new $requestClass($method, array(), $id);
                 // hack! build payload by hand
                 if ($wstype == 1) {
-                    $msg[0]->payload = "{\n" .
+                    $payload = "{\n" .
                         '"method": "' . $method . "\",\n\"params\": [" .
                         $payload .
                         "\n],\n\"id\": ";
                     // fix: if user gave an empty string, use NULL, or we'll break json syntax
                     if ($id == "") {
-                        $msg[0]->payload .= "null\n}";
+                        $payload .= "null\n}";
                     } else {
                         if (is_numeric($id) || $id == 'false' || $id == 'true' || $id == 'null') {
-                            $msg[0]->payload .= "$id\n}";
+                            $payload .= "$id\n}";
                         } else {
-                            $msg[0]->payload .= "\"$id\"\n}";
+                            $payload .= "\"$id\"\n}";
                         }
                     }
+                    $msg[0]->setPayload($payload);
                 } else {
-                    $msg[0]->payload = $msg[0]->xml_header($inputcharset) .
+                    $msg[0]->setPayload(
+                        $msg[0]->xml_header($inputcharset) .
                         '<methodName>' . $method . "</methodName>\n<params>" .
                         $payload .
-                        "</params>\n" . $msg[0]->xml_footer();
+                        "</params>\n" . $msg[0]->xml_footer()
+                    );
                 }
                 $actionname = 'Execution of method ' . $method;
                 break;
index d10938b..38a913c 100644 (file)
@@ -998,7 +998,7 @@ class Client
             return $this->multicall($req, $timeout, $method);
         } elseif (is_string($req)) {
             $n = new static::$requestClass('');
-            $n->payload = $req;
+            $n->setPayload($req);
             $req = $n;
         }
 
@@ -1080,10 +1080,10 @@ class Client
         // Only create the payload if it was not created previously
         /// @todo what if the request's payload was created with a different encoding?
         ///       Also, if we do not call serialize(), the request will not set its content-type to have the charset declared
-        if (empty($req->payload)) {
-            $req->serialize($opts['request_charset_encoding']);
+        $payload = $req->getPayload();
+        if (empty($payload)) {
+            $payload = $req->serialize($opts['request_charset_encoding']);
         }
-        $payload = $req->payload;
 
         // Deflate request body and set appropriate request headers
         $encodingHdr = '';
@@ -1360,12 +1360,12 @@ class Client
         }
 
         // Only create the payload if it was not created previously
-        if (empty($req->payload)) {
-            $req->serialize($opts['request_charset_encoding']);
+        $payload = $req->getPayload();
+        if (empty($payload)) {
+            $payload = $req->serialize($opts['request_charset_encoding']);
         }
 
         // Deflate request body and set appropriate request headers
-        $payload = $req->payload;
         $encodingHdr = '';
         /// @todo test for existence of proper function, in case of polyfills
         if (function_exists('gzdeflate') && ($opts['request_compression'] == 'gzip' || $opts['request_compression'] == 'deflate')) {
index 6a99d92..2d9fd1f 100644 (file)
@@ -8,6 +8,7 @@ use PhpXmlRpc\Helper\XMLParser;
 use PhpXmlRpc\Traits\CharsetEncoderAware;
 use PhpXmlRpc\Traits\DeprecationLogger;
 use PhpXmlRpc\Traits\ParserAware;
+use PhpXmlRpc\Traits\PayloadBearer;
 
 /**
  * This class provides the representation of a request to an XML-RPC server.
@@ -20,17 +21,15 @@ class Request
     use CharsetEncoderAware;
     use DeprecationLogger;
     use ParserAware;
+    use PayloadBearer;
 
     /// @todo: do these need to be public?
-    public $payload;
     /** @internal */
     public $methodname;
     /** @internal */
     public $params = array();
     /** @var int */
     public $debug = 0;
-    /** @var string */
-    public $content_type = 'text/xml';
 
     // holds data while parsing the response. NB: Not a full Response object
     /** @deprecated will be removed in a future release */
index ad7e2f1..f891d55 100644 (file)
@@ -5,6 +5,7 @@ namespace PhpXmlRpc;
 use PhpXmlRpc\Exception\StateErrorException;
 use PhpXmlRpc\Traits\CharsetEncoderAware;
 use PhpXmlRpc\Traits\DeprecationLogger;
+use PhpXmlRpc\Traits\PayloadBearer;
 
 /**
  * This class provides the representation of the response of an XML-RPC server.
@@ -19,6 +20,7 @@ class Response
 {
     use CharsetEncoderAware;
     use DeprecationLogger;
+    use PayloadBearer;
 
     /// @todo: do these need to be public?
     /** @internal */
@@ -29,9 +31,6 @@ class Response
     public $errno = 0;
     /** @internal */
     public $errstr = '';
-    public $payload;
-    /** @var string */
-    public $content_type = 'text/xml';
 
     protected $httpResponse = array('headers' => array(), 'cookies' => array(), 'raw_data' => '', 'status_code' => null);
 
@@ -224,7 +223,8 @@ class Response
                 /// @todo throw instead? There are very few other places where the lib trigger errors which can potentially reach stdout...
                 $trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 1);
                 trigger_error('Undefined property via __get(): ' . $name . ' in ' . $trace[0]['file'] . ' on line ' . $trace[0]['line'], E_USER_WARNING);
-                return null;
+                $result = null;
+                return $result;
         }
     }
 
index 33428f6..2da4258 100644 (file)
@@ -399,18 +399,19 @@ class Server
                 static::$_xmlrpcs_occurred_errors . "+++END+++");
         }
 
-        $payload = $resp->xml_header($respCharset);
+        $header = $resp->xml_header($respCharset);
         if ($this->debug > 0) {
-            $payload = $payload . $this->serializeDebug($respCharset);
+            $header .= $this->serializeDebug($respCharset);
         }
 
         // Do not create response serialization if it has already happened. Helps to build json magic
         /// @todo what if the payload was created targeting a different charset than $respCharset?
         ///       Also, if we do not call serialize(), the request will not set its content-type to have the charset declared
-        if (empty($resp->payload)) {
-            $resp->serialize($respCharset);
+        $payload = $resp->getPayload();
+        if (empty($payload)) {
+            $payload = $resp->serialize($respCharset);
         }
-        $payload = $payload . $resp->payload;
+        $payload = $header . $payload;
 
         if ($returnPayload) {
             return $payload;
diff --git a/src/Traits/PayloadBearer.php b/src/Traits/PayloadBearer.php
new file mode 100644 (file)
index 0000000..555baf8
--- /dev/null
@@ -0,0 +1,37 @@
+<?php
+
+namespace PhpXmlRpc\Traits;
+
+trait PayloadBearer
+{
+    /** @var string */
+    public $payload;
+    /** @var string */
+    public $content_type = 'text/xml';
+
+    /**
+     * @internal
+     *
+     * @param string $payload
+     * @param string $contentType
+     * @return $this
+     */
+    public function setPayload($payload, $contentType = '')
+    {
+        $this->payload = $payload;
+
+        if ($contentType != '') {
+            $this->content_type = $contentType;
+        }
+
+        return $this;
+    }
+
+    /**
+     * @return string
+     */
+    public function getPayload()
+    {
+        return $this->payload;
+    }
+}