📄 server.php
字号:
<?php/** * Zend Framework * * LICENSE * * This source file is subject to version 1.0 of the Zend Framework * license, that is bundled with this package in the file LICENSE.txt, and * is available through the world-wide-web at the following URL: * http://framework.zend.com/license/new-bsd. If you did not receive * a copy of the Zend Framework license and are unable to obtain it * through the world-wide-web, please send a note to license@zend.com * so we can mail you a copy immediately. * * @package Zend_XmlRpc * @subpackage Server * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License *//** * Implement Zend_Server_Interface */require_once 'Zend/Server/Interface.php';/** * Exception this class throws */require_once 'Zend/XmlRpc/Server/Exception.php';/** * XMLRPC Request */require_once 'Zend/XmlRpc/Request.php';/** * XMLRPC Response */require_once 'Zend/XmlRpc/Response.php';/** * XMLRPC HTTP Response */require_once 'Zend/XmlRpc/Response/Http.php';/** * XMLRPC server fault class */require_once 'Zend/XmlRpc/Server/Fault.php';/** * Convert PHP to and from xmlrpc native types */require_once 'Zend/XmlRpc/Value.php';/** * Reflection API for function/method introspection */require_once 'Zend/Server/Reflection.php';/** * Zend_Server_Reflection_Function_Abstract */require_once 'Zend/Server/Reflection/Function/Abstract.php';/** * Specifically grab the Zend_Server_Reflection_Method for manually setting up * system.* methods and handling callbacks in {@link loadFunctions()}. */require_once 'Zend/Server/Reflection/Method.php';/** * An XML-RPC server implementation * * Example: * <code> * require_once 'Zend/XmlRpc/Server.php'; * require_once 'Zend/XmlRpc/Server/Cache.php'; * require_once 'Zend/XmlRpc/Server/Fault.php'; * require_once 'My/Exception.php'; * require_once 'My/Fault/Observer.php'; * * // Instantiate server * $server = new Zend_XmlRpc_Server(); * * // Allow some exceptions to report as fault responses: * Zend_XmlRpc_Server_Fault::attachFaultException('My_Exception'); * Zend_XmlRpc_Server_Fault::attachObserver('My_Fault_Observer'); * * // Get or build dispatch table: * if (!Zend_XmlRpc_Server_Cache::get($filename, $server)) { * require_once 'Some/Service/Class.php'; * require_once 'Another/Service/Class.php'; * * // Attach Some_Service_Class in 'some' namespace * $server->setClass('Some_Service_Class', 'some'); * * // Attach Another_Service_Class in 'another' namespace * $server->setClass('Another_Service_Class', 'another'); * * // Create dispatch table cache file * Zend_XmlRpc_Server_Cache::save($filename, $server); * } * * $response = $server->handle(); * echo $response; * </code> * * @category Zend * @package Zend_XmlRpc * @subpackage Server * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License */class Zend_XmlRpc_Server{ /** * Character encoding * @var string */ protected $_encoding = 'UTF-8'; /** * Array of dispatchables * @var array */ protected $_methods = array(); /** * Request processed * @var null|Zend_XmlRpc_Request */ protected $_request = null; /** * Class to use for responses; defaults to {@link Zend_XmlRpc_Response_Http} * @var string */ protected $_responseClass = 'Zend_XmlRpc_Response_Http'; /** * Dispatch table of name => method pairs * @var array */ protected $_table = array(); /** * PHP types => XML-RPC types * @var array */ protected $_typeMap = array( 'i4' => 'i4', 'int' => 'int', 'integer' => 'int', 'double' => 'double', 'float' => 'double', 'real' => 'double', 'boolean' => 'boolean', 'bool' => 'boolean', 'true' => 'boolean', 'false' => 'boolean', 'string' => 'string', 'str' => 'string', 'base64' => 'base64', 'dateTime.iso8601' => 'dateTime.iso8601', 'date' => 'dateTime.iso8601', 'time' => 'dateTime.iso8601', 'time' => 'dateTime.iso8601', 'array' => 'array', 'struct' => 'struct', 'null' => 'nil', 'nil' => 'nil', 'void' => 'void', 'mixed' => 'struct' ); /** * Constructor * * Creates system.* methods. * * @return void */ public function __construct() { // Setup system.* methods $system = array( 'listMethods', 'methodHelp', 'methodSignature', 'multicall' ); $class = Zend_Server_Reflection::reflectClass($this); foreach ($system as $method) { $reflection = new Zend_Server_Reflection_Method($class, new ReflectionMethod($this, $method), 'system'); $reflection->system = true; $this->_methods[] = $reflection; } $this->_buildDispatchTable(); } /** * Map PHP parameter types to XML-RPC types * * @param Zend_Server_Reflection_Function_Abstract $method * @return void */ protected function _fixTypes(Zend_Server_Reflection_Function_Abstract $method) { foreach ($method->getPrototypes() as $prototype) { foreach ($prototype->getParameters() as $param) { $pType = $param->getType(); if (isset($this->_typeMap[$pType])) { $param->setType($this->_typeMap[$pType]); } else { $param->setType('void'); } } } } /** * Re/Build the dispatch table * * The dispatch table consists of a an array of method name => * Zend_Server_Reflection_Function_Abstract pairs * * @return void */ protected function _buildDispatchTable() { $table = array(); foreach ($this->_methods as $dispatchable) { if ($dispatchable instanceof Zend_Server_Reflection_Function_Abstract) { // function/method call $ns = $dispatchable->getNamespace(); $name = $dispatchable->getName(); $name = empty($ns) ? $name : $ns . '.' . $name; if (isset($table[$name])) { throw new Zend_XmlRpc_Server_Exception('Duplicate method registered: ' . $name); } $table[$name] = $dispatchable; $this->_fixTypes($dispatchable); continue; } if ($dispatchable instanceof Zend_Server_Reflection_Class) { foreach ($dispatchable->getMethods() as $method) { $ns = $method->getNamespace(); $name = $method->getName(); $name = empty($ns) ? $name : $ns . '.' . $name; if (isset($table[$name])) { throw new Zend_XmlRpc_Server_Exception('Duplicate method registered: ' . $name); } $table[$name] = $method; $this->_fixTypes($method); continue; } } } $this->_table = $table; } /** * Set encoding * * @param string $encoding * @return Zend_XmlRpc_Server */ public function setEncoding($encoding) { $this->_encoding = $encoding; return $this; } /** * Retrieve current encoding * * @return string */ public function getEncoding() { return $this->_encoding; } /** * Attach a callback as an XMLRPC method * * Attaches a callback as an XMLRPC method, prefixing the XMLRPC method name * with $namespace, if provided. Reflection is done on the callback's * docblock to create the methodHelp for the XMLRPC method. * * Additional arguments to pass to the function at dispatch may be passed; * any arguments following the namespace will be aggregated and passed at * dispatch time. * * @param string|array $function Valid callback * @param string $namespace Optional namespace prefix * @return void * @throws Zend_XmlRpc_Server_Exception */ public function addFunction($function, $namespace = '') { if (!is_string($function) && !is_array($function)) { throw new Zend_XmlRpc_Server_Exception('Unable to attach function; invalid', 611); } $argv = null; if (2 < func_num_args()) { $argv = func_get_args(); $argv = array_slice($argv, 2); } $function = (array) $function; foreach ($function as $func) { if (!is_string($func) || !function_exists($func)) { throw new Zend_XmlRpc_Server_Exception('Unable to attach function; invalid', 611); } $this->_methods[] = Zend_Server_Reflection::reflectFunction($func, $argv, $namespace); } $this->_buildDispatchTable(); } /** * Load methods as returned from {@link getFunctions} * * Typically, you will not use this method; it will be called using the * results pulled from {@link Zend_XmlRpc_Server_Cache::get()}. * * @param array $array * @return void * @throws Zend_XmlRpc_Server_Exception on invalid input */ public function loadFunctions($array) { if (!is_array($array)) { throw new Zend_XmlRpc_Server_Exception('Unable to load array; not an array', 612); } foreach ($array as $key => $value) { if (!$value instanceof Zend_Server_Reflection_Function_Abstract && !$value instanceof Zend_Server_Reflection_Class) { throw new Zend_XmlRpc_Server_Exception('One or more method records are corrupt or otherwise unusable', 613); } if ($value->system) { unset($array[$key]); } } foreach ($array as $dispatchable) { $this->_methods[] = $dispatchable; } $this->_buildDispatchTable(); } /** * Do nothing; persistence is handled via {@link Zend_XmlRpc_Server_Cache} *
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -