1*7f8f2456SAndreas Gohr<?php 2*7f8f2456SAndreas Gohrnamespace IXR\Server; 3*7f8f2456SAndreas Gohr 4*7f8f2456SAndreas Gohruse IXR\Message\Error; 5*7f8f2456SAndreas Gohr 6*7f8f2456SAndreas Gohr/** 7*7f8f2456SAndreas Gohr * Extension of the {@link Server} class to easily wrap objects. 8*7f8f2456SAndreas Gohr * 9*7f8f2456SAndreas Gohr * Class is designed to extend the existing XML-RPC server to allow the 10*7f8f2456SAndreas Gohr * presentation of methods from a variety of different objects via an 11*7f8f2456SAndreas Gohr * XML-RPC server. 12*7f8f2456SAndreas Gohr * It is intended to assist in organization of your XML-RPC methods by allowing 13*7f8f2456SAndreas Gohr * you to "write once" in your existing model classes and present them. 14*7f8f2456SAndreas Gohr * 15*7f8f2456SAndreas Gohr * @author Jason Stirk <jstirk@gmm.com.au> 16*7f8f2456SAndreas Gohr * @version 1.0.1 19Apr2005 17:40 +0800 17*7f8f2456SAndreas Gohr * @copyright Copyright (c) 2005 Jason Stirk 18*7f8f2456SAndreas Gohr * @package IXR 19*7f8f2456SAndreas Gohr */ 20*7f8f2456SAndreas Gohrclass ClassServer extends Server 21*7f8f2456SAndreas Gohr{ 22*7f8f2456SAndreas Gohr 23*7f8f2456SAndreas Gohr private $_objects; 24*7f8f2456SAndreas Gohr private $_delim; 25*7f8f2456SAndreas Gohr 26*7f8f2456SAndreas Gohr public function __construct($delim = '.', $wait = false) 27*7f8f2456SAndreas Gohr { 28*7f8f2456SAndreas Gohr parent::__construct([], false, $wait); 29*7f8f2456SAndreas Gohr $this->_delim = $delim; 30*7f8f2456SAndreas Gohr $this->_objects = []; 31*7f8f2456SAndreas Gohr } 32*7f8f2456SAndreas Gohr 33*7f8f2456SAndreas Gohr public function addMethod($rpcName, $functionName) 34*7f8f2456SAndreas Gohr { 35*7f8f2456SAndreas Gohr $this->callbacks[$rpcName] = $functionName; 36*7f8f2456SAndreas Gohr } 37*7f8f2456SAndreas Gohr 38*7f8f2456SAndreas Gohr public function registerObject($object, $methods, $prefix = null) 39*7f8f2456SAndreas Gohr { 40*7f8f2456SAndreas Gohr if (is_null($prefix)) { 41*7f8f2456SAndreas Gohr $prefix = get_class($object); 42*7f8f2456SAndreas Gohr } 43*7f8f2456SAndreas Gohr $this->_objects[$prefix] = $object; 44*7f8f2456SAndreas Gohr 45*7f8f2456SAndreas Gohr // Add to our callbacks array 46*7f8f2456SAndreas Gohr foreach ($methods as $method) { 47*7f8f2456SAndreas Gohr if (is_array($method)) { 48*7f8f2456SAndreas Gohr $targetMethod = $method[0]; 49*7f8f2456SAndreas Gohr $method = $method[1]; 50*7f8f2456SAndreas Gohr } else { 51*7f8f2456SAndreas Gohr $targetMethod = $method; 52*7f8f2456SAndreas Gohr } 53*7f8f2456SAndreas Gohr $this->callbacks[$prefix . $this->_delim . $method] = [$prefix, $targetMethod]; 54*7f8f2456SAndreas Gohr } 55*7f8f2456SAndreas Gohr } 56*7f8f2456SAndreas Gohr 57*7f8f2456SAndreas Gohr public function call($methodname, $args) 58*7f8f2456SAndreas Gohr { 59*7f8f2456SAndreas Gohr if (!$this->hasMethod($methodname)) { 60*7f8f2456SAndreas Gohr return new Error(-32601, 'server error. requested method ' . $methodname . ' does not exist.'); 61*7f8f2456SAndreas Gohr } 62*7f8f2456SAndreas Gohr $method = $this->callbacks[$methodname]; 63*7f8f2456SAndreas Gohr 64*7f8f2456SAndreas Gohr // Perform the callback and send the response 65*7f8f2456SAndreas Gohr if (count($args) == 1) { 66*7f8f2456SAndreas Gohr // If only one paramater just send that instead of the whole array 67*7f8f2456SAndreas Gohr $args = $args[0]; 68*7f8f2456SAndreas Gohr } 69*7f8f2456SAndreas Gohr 70*7f8f2456SAndreas Gohr // See if this method comes from one of our objects or maybe self 71*7f8f2456SAndreas Gohr if (is_array($method) || (substr($method, 0, 5) == 'this:')) { 72*7f8f2456SAndreas Gohr if (is_array($method)) { 73*7f8f2456SAndreas Gohr $object = $this->_objects[$method[0]]; 74*7f8f2456SAndreas Gohr $method = $method[1]; 75*7f8f2456SAndreas Gohr } else { 76*7f8f2456SAndreas Gohr $object = $this; 77*7f8f2456SAndreas Gohr $method = substr($method, 5); 78*7f8f2456SAndreas Gohr } 79*7f8f2456SAndreas Gohr 80*7f8f2456SAndreas Gohr // It's a class method - check it exists 81*7f8f2456SAndreas Gohr if (!method_exists($object, $method)) { 82*7f8f2456SAndreas Gohr return new Error(-32601, 'server error. requested class method "' . $method . '" does not exist.'); 83*7f8f2456SAndreas Gohr } 84*7f8f2456SAndreas Gohr 85*7f8f2456SAndreas Gohr // Call the method 86*7f8f2456SAndreas Gohr $result = $object->$method($args); 87*7f8f2456SAndreas Gohr } else { 88*7f8f2456SAndreas Gohr // It's a function - does it exist? 89*7f8f2456SAndreas Gohr if (!function_exists($method)) { 90*7f8f2456SAndreas Gohr return new Error(-32601, 'server error. requested function "' . $method . '" does not exist.'); 91*7f8f2456SAndreas Gohr } 92*7f8f2456SAndreas Gohr 93*7f8f2456SAndreas Gohr // Call the function 94*7f8f2456SAndreas Gohr $result = $method($args); 95*7f8f2456SAndreas Gohr } 96*7f8f2456SAndreas Gohr return $result; 97*7f8f2456SAndreas Gohr } 98*7f8f2456SAndreas Gohr} 99