Made it possible to wrap namespaced classes with a different namespace
authorrene.patzer@gameforge.com <Rene Patzer>
Fri, 5 Nov 2021 12:44:41 +0000 (13:44 +0100)
committerrene.patzer@gameforge.com <Rene Patzer>
Fri, 5 Nov 2021 12:45:01 +0000 (13:45 +0100)
demo/server/methodProviders/wrapper.php
src/Wrapper.php
tests/5ServerTest.php

index b90425a..5f4df88 100644 (file)
@@ -138,6 +138,8 @@ $c = new xmlrpcServerMethodsContainer();
 
 $moreSignatures = $wrapper->wrapPhpClass($c, array('prefix' => 'tests.', 'method_type' => 'all'));
 
+$namespaceSignatures = $wrapper->wrapPhpClass($c, array('namespace' => 'namespacetest', 'method_type' => 'all'));
+
 $returnObj_sig =  $wrapper->wrapPhpFunction(array($c, 'returnObject'), '', array('encode_php_objs' => true));
 
 return array_merge(
@@ -154,5 +156,6 @@ return array_merge(
         'tests.getStateName.11' => $findstate11_sig,
         'tests.returnPhpObject' => $returnObj_sig,
     ),
+    $namespaceSignatures,
     $moreSignatures
 );
index 15f84ce..8b5ddb4 100644 (file)
@@ -622,8 +622,8 @@ class Wrapper
      * @param array $extraOptions see the docs for wrapPhpMethod for basic options, plus
      *                            - string method_type    'static', 'nonstatic', 'all' and 'auto' (default); the latter will switch between static and non-static depending on whether $className is a class name or object instance
      *                            - string method_filter  a regexp used to filter methods to wrap based on their names
-     *                            - string prefix         used for the names of the xmlrpc methods created
-     *
+     *                            - string prefix         used for the names of the xmlrpc methods created.
+     *                            - string namespace      use when classes with actual namespaces should only have one namespace. e.g. \Some\Namespace\Api is needed as my.Api set this to "my". Works in conjunction with prefix!
      * @return array|false false on failure
      */
     public function wrapPhpClass($className, $extraOptions = array())
@@ -631,6 +631,7 @@ class Wrapper
         $methodFilter = isset($extraOptions['method_filter']) ? $extraOptions['method_filter'] : '';
         $methodType = isset($extraOptions['method_type']) ? $extraOptions['method_type'] : 'auto';
         $prefix = isset($extraOptions['prefix']) ? $extraOptions['prefix'] : '';
+        $namespace = isset($extraOptions['namespace']) ? $extraOptions['namespace'] : '';
 
         $results = array();
         $mList = get_class_methods($className);
@@ -643,10 +644,14 @@ class Wrapper
                     ) {
                         $methodWrap = $this->wrapPhpFunction(array($className, $mName), '', $extraOptions);
                         if ($methodWrap) {
-                            if (is_object($className)) {
-                                $realClassName = get_class($className);
-                            }else {
-                                $realClassName = $className;
+                            if ($namespace) {
+                                $realClassName = $namespace;
+                            } else {
+                                if (is_object($className)) {
+                                    $realClassName = get_class($className);
+                                }else {
+                                    $realClassName = $className;
+                                }
                             }
                             $results[$prefix."$realClassName.$mName"] = $methodWrap;
                         }
index 92d8ba9..483bebc 100644 (file)
@@ -826,6 +826,15 @@ And turned it into nylon';
         $this->assertEquals('Michigan', $v->scalarval());
     }
 
+    public function testServerWrappedClassWithNamespace()
+    {
+        $m = new xmlrpcmsg('namespacetest.findState', array(
+            new xmlrpcval(23, 'int'),
+        ));
+        $v = $this->send($m);
+        $this->assertEquals('Michigan', $v->scalarval());
+    }
+
     public function testWrappedMethod()
     {
         // make a 'deep client copy' as the original one might have many properties set