17f8f2456SAndreas Gohr<?php 27f8f2456SAndreas Gohrnamespace IXR\Server; 37f8f2456SAndreas Gohr 47f8f2456SAndreas Gohruse IXR\Message\Error; 57f8f2456SAndreas Gohr 67f8f2456SAndreas Gohr/** 77f8f2456SAndreas Gohr * Extension of the {@link Server} class to easily wrap objects. 87f8f2456SAndreas Gohr * 97f8f2456SAndreas Gohr * Class is designed to extend the existing XML-RPC server to allow the 107f8f2456SAndreas Gohr * presentation of methods from a variety of different objects via an 117f8f2456SAndreas Gohr * XML-RPC server. 127f8f2456SAndreas Gohr * It is intended to assist in organization of your XML-RPC methods by allowing 137f8f2456SAndreas Gohr * you to "write once" in your existing model classes and present them. 147f8f2456SAndreas Gohr * 157f8f2456SAndreas Gohr * @author Jason Stirk <jstirk@gmm.com.au> 167f8f2456SAndreas Gohr * @version 1.0.1 19Apr2005 17:40 +0800 177f8f2456SAndreas Gohr * @copyright Copyright (c) 2005 Jason Stirk 187f8f2456SAndreas Gohr * @package IXR 197f8f2456SAndreas Gohr */ 207f8f2456SAndreas Gohrclass ClassServer extends Server 217f8f2456SAndreas Gohr{ 227f8f2456SAndreas Gohr 237f8f2456SAndreas Gohr private $_objects; 247f8f2456SAndreas Gohr private $_delim; 257f8f2456SAndreas Gohr 267f8f2456SAndreas Gohr public function __construct($delim = '.', $wait = false) 277f8f2456SAndreas Gohr { 287f8f2456SAndreas Gohr parent::__construct([], false, $wait); 297f8f2456SAndreas Gohr $this->_delim = $delim; 307f8f2456SAndreas Gohr $this->_objects = []; 317f8f2456SAndreas Gohr } 327f8f2456SAndreas Gohr 337f8f2456SAndreas Gohr public function addMethod($rpcName, $functionName) 347f8f2456SAndreas Gohr { 357f8f2456SAndreas Gohr $this->callbacks[$rpcName] = $functionName; 367f8f2456SAndreas Gohr } 377f8f2456SAndreas Gohr 387f8f2456SAndreas Gohr public function registerObject($object, $methods, $prefix = null) 397f8f2456SAndreas Gohr { 407f8f2456SAndreas Gohr if (is_null($prefix)) { 417f8f2456SAndreas Gohr $prefix = get_class($object); 427f8f2456SAndreas Gohr } 437f8f2456SAndreas Gohr $this->_objects[$prefix] = $object; 447f8f2456SAndreas Gohr 457f8f2456SAndreas Gohr // Add to our callbacks array 467f8f2456SAndreas Gohr foreach ($methods as $method) { 477f8f2456SAndreas Gohr if (is_array($method)) { 487f8f2456SAndreas Gohr $targetMethod = $method[0]; 497f8f2456SAndreas Gohr $method = $method[1]; 507f8f2456SAndreas Gohr } else { 517f8f2456SAndreas Gohr $targetMethod = $method; 527f8f2456SAndreas Gohr } 537f8f2456SAndreas Gohr $this->callbacks[$prefix . $this->_delim . $method] = [$prefix, $targetMethod]; 547f8f2456SAndreas Gohr } 557f8f2456SAndreas Gohr } 567f8f2456SAndreas Gohr 577f8f2456SAndreas Gohr public function call($methodname, $args) 587f8f2456SAndreas Gohr { 597f8f2456SAndreas Gohr if (!$this->hasMethod($methodname)) { 607f8f2456SAndreas Gohr return new Error(-32601, 'server error. requested method ' . $methodname . ' does not exist.'); 617f8f2456SAndreas Gohr } 627f8f2456SAndreas Gohr $method = $this->callbacks[$methodname]; 637f8f2456SAndreas Gohr 647f8f2456SAndreas Gohr // Perform the callback and send the response 657f8f2456SAndreas Gohr if (count($args) == 1) { 66*934f970aSAndreas Gohr // If only one parameter just send that instead of the whole array 677f8f2456SAndreas Gohr $args = $args[0]; 687f8f2456SAndreas Gohr } 697f8f2456SAndreas Gohr 707f8f2456SAndreas Gohr // See if this method comes from one of our objects or maybe self 717f8f2456SAndreas Gohr if (is_array($method) || (substr($method, 0, 5) == 'this:')) { 727f8f2456SAndreas Gohr if (is_array($method)) { 737f8f2456SAndreas Gohr $object = $this->_objects[$method[0]]; 747f8f2456SAndreas Gohr $method = $method[1]; 757f8f2456SAndreas Gohr } else { 767f8f2456SAndreas Gohr $object = $this; 777f8f2456SAndreas Gohr $method = substr($method, 5); 787f8f2456SAndreas Gohr } 797f8f2456SAndreas Gohr 807f8f2456SAndreas Gohr // It's a class method - check it exists 817f8f2456SAndreas Gohr if (!method_exists($object, $method)) { 827f8f2456SAndreas Gohr return new Error(-32601, 'server error. requested class method "' . $method . '" does not exist.'); 837f8f2456SAndreas Gohr } 847f8f2456SAndreas Gohr 857f8f2456SAndreas Gohr // Call the method 867f8f2456SAndreas Gohr $result = $object->$method($args); 877f8f2456SAndreas Gohr } else { 887f8f2456SAndreas Gohr // It's a function - does it exist? 897f8f2456SAndreas Gohr if (!function_exists($method)) { 907f8f2456SAndreas Gohr return new Error(-32601, 'server error. requested function "' . $method . '" does not exist.'); 917f8f2456SAndreas Gohr } 927f8f2456SAndreas Gohr 937f8f2456SAndreas Gohr // Call the function 947f8f2456SAndreas Gohr $result = $method($args); 957f8f2456SAndreas Gohr } 967f8f2456SAndreas Gohr return $result; 977f8f2456SAndreas Gohr } 987f8f2456SAndreas Gohr} 99