Skip to content

Commit

Permalink
improve solution to allow custom mapping of class names when wrapping…
Browse files Browse the repository at this point in the history
… them
  • Loading branch information
gggeek committed Dec 9, 2021
1 parent e344025 commit 9d2e97b
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 24 deletions.
5 changes: 4 additions & 1 deletion NEWS
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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


Expand Down
2 changes: 1 addition & 1 deletion demo/server/methodProviders/wrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ public function returnObject()

$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));

Expand Down
36 changes: 23 additions & 13 deletions src/Wrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -623,15 +623,13 @@ protected function buildWrapFunctionSource($callable, $newFuncName, $extraOption
* - 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);
Expand All @@ -643,17 +641,9 @@ public function wrapPhpClass($className, $extraOptions = array())
(!$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;
}
}
}
Expand All @@ -663,6 +653,26 @@ public function wrapPhpClass($className, $extraOptions = array())
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
Expand Down
18 changes: 9 additions & 9 deletions tests/5ServerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -826,6 +826,15 @@ public function testServerWrappedClass()
$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
Expand All @@ -841,15 +850,6 @@ public function testWrapInexistentUrl()
$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
Expand Down

0 comments on commit 9d2e97b

Please sign in to comment.