diff options
Diffstat (limited to 'framework/Util/TRpcClient.php')
-rw-r--r-- | framework/Util/TRpcClient.php | 211 |
1 files changed, 2 insertions, 209 deletions
diff --git a/framework/Util/TRpcClient.php b/framework/Util/TRpcClient.php index 91b27b52..ec0da4e2 100644 --- a/framework/Util/TRpcClient.php +++ b/framework/Util/TRpcClient.php @@ -1,5 +1,4 @@ <?php - /** * @author Robin J. Rogge <rrogge@bigpoint.net> * @link http://www.pradosoft.com/ @@ -9,6 +8,7 @@ * @package System.Util */ + /** * TRpcClient class * @@ -147,211 +147,4 @@ class TRpcClient extends TApplicationComponent { $this->_serverUrl = $value; } -} - -/** - * TRpcClientTypesEnumerable class - * - * @author Robin J. Rogge <rrogge@bigpoint.net> - * @version $Id$ - * @package System.Util - * @since 3.2 - */ - -class TRpcClientTypesEnumerable extends TEnumerable -{ - const JSON = 'TJsonRpcClient'; - const XML = 'TXmlRpcClient'; -} - -/** - * TRpcClientRequestException class - * - * This Exception is fired if the RPC request fails because of transport problems e.g. when - * there is no RPC server responding on the given remote host. - * - * @author Robin J. Rogge <rrogge@bigpoint.net> - * @version $Id$ - * @package System.Util - * @since 3.2 - */ - -class TRpcClientRequestException extends TApplicationException -{ -} - -/** - * TRpcClientResponseException class - * - * This Exception is fired when the - * - * @author Robin J. Rogge <rrogge@bigpoint.net> - * @version $Id$ - * @package System.Util - * @since 3.2 - */ - -class TRpcClientResponseException extends TApplicationException -{ - /** - * @param string error message - * @param integer error code (optional) - */ - public function __construct($errorMessage, $errorCode = null) - { - $this->setErrorCode($errorCode); - - parent::__construct($errorMessage); - } -} - -/** - * TJsonRpcClient class - * - * Note: When using setIsNotification(true), *every* following request is also - * considered to be a notification until you use setIsNotification(false). - * - * Usage: - * <pre> - * $_rpcClient = new TJsonRpcClient('http://host/server'); - * $_result = $_rpcClient->remoteMethod($param, $otherParam); - * // or - * $_result = TJsonRpcClient::create('http://host/server')->remoteMethod($param, $otherParam); - * </pre> - * - * @author Robin J. Rogge <rrogge@bigpoint.net> - * @version $Id$ - * @package System.Util - * @since 3.2 - */ - -class TJsonRpcClient extends TRpcClient -{ - // magics - - /** - * @param string RPC method name - * @param array RPC method parameters - * @return mixed RPC request result - * @throws TRpcClientRequestException if the client fails to connect to the server - * @throws TRpcClientResponseException if the response represents an RPC fault - */ - public function __call($method, $parameters) - { - // send request - $_response = $this->performRequest($this->getServerUrl(), $this->encodeRequest($method, $parameters), 'application/json'); - - // skip response handling if the request was just a notification request - if($this->isNotification) - return true; - - // decode response - if(($_response = json_decode($_response, true)) === null) - throw new TRpcClientResponseException('Empty response received'); - - // handle error response - if(!is_null($_response['error'])) - throw new TRpcClientResponseException($_response['error']); - - return $_response['result']; - } - - // methods - - /** - * @param string method name - * @param array method parameters - */ - public function encodeRequest($method, $parameters) - { - static $_requestId; - $_requestId = ($_requestId === null) ? 1 : $_requestId + 1; - - return json_encode(array( - 'method' => $method, - 'params' => $parameters, - 'id' => $this->isNotification ? null : $_requestId - )); - } - - /** - * Creates an instance of TJsonRpcClient - * @param string url of the rpc server - * @param boolean whether the requests are considered to be notifications (completely ignoring the response) (default: false) - */ - public static function create($type, $serverUrl, $isNotification = false) - { - return new self($serverUrl, $isNotification); - } -} - -/** - * TXmlRpcClient class - * - * Note: When using setIsNotification(true), *every* following request is also - * considered to be a notification until you use setIsNotification(false). - * - * Usage: - * <pre> - * $_rpcClient = new TXmlRpcClient('http://remotehost/rpcserver'); - * $_rpcClient->remoteMethod($param, $otherParam); - * </pre> - * - * @author Robin J. Rogge <rrogge@bigpoint.net> - * @version $Id$ - * @package System.Util - * @since 3.2 - */ - -class TXmlRpcClient extends TRpcClient -{ - // magics - - /** - * @param string RPC method name - * @param array RPC method parameters - * @return mixed RPC request result - * @throws TRpcClientRequestException if the client fails to connect to the server - * @throws TRpcClientResponseException if the response represents an RPC fault - */ - public function __call($method, $parameters) - { - // send request - $_response = $this->performRequest($this->getServerUrl(), $this->encodeRequest($method, $parameters), 'text/xml'); - - // skip response handling if the request was just a notification request - if($this->isNotification) - return true; - - // decode response - if(($_response = xmlrpc_decode($_response)) === null) - throw new TRpcClientResponseException('Empty response received'); - - // handle error response - if(xmlrpc_is_fault($_response)) - throw new TRpcClientResponseException($_response['faultString'], $_response['faultCode']); - - return $_response; - } - - // methods - - /** - * @param string method name - * @param array method parameters - */ - public function encodeRequest($method, $parameters) - { - return xmlrpc_encode_request($method, $parameters); - } - - /** - * Creates an instance of TXmlRpcClient - * @param string url of the rpc server - * @param boolean whether the requests are considered to be notifications (completely ignoring the response) (default: false) - */ - public static function create($type, $serverUrl, $isNotification = false) - { - return new self($serverUrl, $isNotification); - } -} +}
\ No newline at end of file |