From 9d2e97ba11a758c1af241ef467edb8e7d7519798 Mon Sep 17 00:00:00 2001 From: gggeek Date: Thu, 9 Dec 2021 00:18:37 +0000 Subject: [PATCH] improve solution to allow custom mapping of class names when wrapping them --- NEWS | 5 +++- demo/server/methodProviders/wrapper.php | 2 +- src/Wrapper.php | 36 ++++++++++++++++--------- tests/5ServerTest.php | 18 ++++++------- 4 files changed, 37 insertions(+), 24 deletions(-) diff --git a/NEWS b/NEWS index edd3106..34bdcd8 100644 --- a/NEWS +++ b/NEWS @@ -1,4 +1,4 @@ -XML-RPC for PHP version 4.6.0 - unreleased +XML-RPC for PHP version 4.6.0 - 2021/12/9 * fixed: compatibility with php 8.1 @@ -18,6 +18,9 @@ XML-RPC for PHP version 4.6.0 - unreleased * new: method `XMLParser::parse()` acquired a 4th argument +* new: method `Wrapper::wrapPhpClass` allows to customize the names of the phpxmlrpc methods by stripping the original + class name and accompanying namespace and replace it with a user-defined prefix, via option `replace_class_name` + * improved: Continuous Integration is now running on Github Actions instead of Travis diff --git a/demo/server/methodProviders/wrapper.php b/demo/server/methodProviders/wrapper.php index 5f4df88..749670f 100644 --- a/demo/server/methodProviders/wrapper.php +++ b/demo/server/methodProviders/wrapper.php @@ -138,7 +138,7 @@ $c = new xmlrpcServerMethodsContainer(); $moreSignatures = $wrapper->wrapPhpClass($c, array('prefix' => 'tests.', 'method_type' => 'all')); -$namespaceSignatures = $wrapper->wrapPhpClass($c, array('namespace' => 'namespacetest', 'method_type' => 'all')); +$namespaceSignatures = $wrapper->wrapPhpClass($c, array('prefix' => 'namespacetest.', 'replace_class_name' => true, 'method_filter' => '/^findState$/', 'method_type' => 'static')); $returnObj_sig = $wrapper->wrapPhpFunction(array($c, 'returnObject'), '', array('encode_php_objs' => true)); diff --git a/src/Wrapper.php b/src/Wrapper.php index b87af43..e000a06 100644 --- a/src/Wrapper.php +++ b/src/Wrapper.php @@ -623,15 +623,13 @@ class Wrapper * - 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 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! + * - string replace_class_name use to completely replace the class name with the prefix in the generated method names. e.g. instead of \Some\Namespace\Class.method use prefixmethod * @return array|false false on failure */ public function wrapPhpClass($className, $extraOptions = array()) { $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,17 +641,9 @@ class Wrapper (!$func->isStatic() && ($methodType == 'all' || $methodType == 'nonstatic' || ($methodType == 'auto' && is_object($className)))) ) { $methodWrap = $this->wrapPhpFunction(array($className, $mName), '', $extraOptions); + if ($methodWrap) { - if ($namespace) { - $realClassName = $namespace; - } else { - if (is_object($className)) { - $realClassName = get_class($className); - }else { - $realClassName = $className; - } - } - $results[$prefix."$realClassName.$mName"] = $methodWrap; + $results[$this->generateMethodNameForClassMethod($className, $mName, $extraOptions)] = $methodWrap; } } } @@ -663,6 +653,26 @@ class Wrapper return $results; } + /** + * @param string|object $className + * @param string $classMethod + * @param array $extraOptions + * @return string + */ + protected function generateMethodNameForClassMethod($className, $classMethod, $extraOptions = array()) + { + if (isset($extraOptions['replace_class_name']) && $extraOptions['replace_class_name']) { + return (isset($extraOptions['prefix']) ? $extraOptions['prefix'] : '') . $classMethod; + } + + if (is_object($className)) { + $realClassName = get_class($className); + } else { + $realClassName = $className; + } + return (isset($extraOptions['prefix']) ? $extraOptions['prefix'] : '') . "$realClassName.$classMethod"; + } + /** * Given an xmlrpc client and a method name, register a php wrapper function * that will call it and return results using native php types for both diff --git a/tests/5ServerTest.php b/tests/5ServerTest.php index 9374390..0345b79 100644 --- a/tests/5ServerTest.php +++ b/tests/5ServerTest.php @@ -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 testWrapInexistentMethod() { // make a 'deep client copy' as the original one might have many properties set @@ -841,15 +850,6 @@ And turned it into nylon'; $this->assertEquals(false, $func); } - 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 -- 2.43.0