📄 server.php
字号:
$class = false; } $method = $function->getName(); $dom = new DOMDocument('1.0', 'UTF-8'); if ($class) { $root = $dom->createElement($class); $method = $dom->createElement($method); $root->appendChild($method); } else { $root = $dom->createElement($method); $method = $root; } $root->setAttribute('generator', 'zend'); $root->setAttribute('version', '1.0'); $dom->appendChild($root); $this->_structValue($struct, $dom, $method); $struct = (array) $struct; if (!isset($struct['status'])) { $status = $dom->createElement('status', 'success'); $method->appendChild($status); } return $dom->saveXML(); } /** * Recursively iterate through a struct * * Recursively iterates through an associative array or object's properties * to build XML response. * * @param mixed $struct * @param DOMDocument $dom * @param DOMElement $parent * @return void */ protected function _structValue($struct, DOMDocument $dom, DOMElement $parent) { $struct = (array) $struct; foreach ($struct as $key => $value) { if ($value === false) { $value = 0; } elseif ($value === true) { $value = 1; } if (ctype_digit((string) $key)) { $key = 'key_' . $key; } if (is_array($value) || is_object($value)) { $element = $dom->createElement($key); $this->_structValue($value, $dom, $element); } else { $element = $dom->createElement($key); $element->appendChild($dom->createTextNode($value)); } $parent->appendChild($element); } } /** * Handle a single value * * @param string|int|boolean $value Result value * @return string XML Response */ protected function _handleScalar($value) { $function = $this->_functions[$this->_method]; if ($function instanceof Zend_Server_Reflection_Method) { $class = $function->getDeclaringClass()->getName(); } else { $class = false; } $method = $function->getName(); $dom = new DOMDocument('1.0', 'UTF-8'); if ($class) { $xml = $dom->createElement($class); $methodNode = $dom->createElement($method); $xml->appendChild($methodNode); } else { $xml = $dom->createElement($method); $methodNode = $xml; } $xml->setAttribute('generator', 'zend'); $xml->setAttribute('version', '1.0'); $dom->appendChild($xml); if ($value === false) { $value = 0; } elseif ($value === true) { $value = 1; } if (isset($value)) { $element = $dom->createElement('response'); $element->appendChild($dom->createTextNode($value)); $methodNode->appendChild($element); } else { $methodNode->appendChild($dom->createElement('response')); } $methodNode->appendChild($dom->createElement('status', 'success')); return $dom->saveXML(); } /** * Implement Zend_Server_Interface::fault() * * Creates XML error response, returning DOMDocument with response. * * @param string|Exception $fault Message * @param int $code Error Code * @return DOMDocument */ public function fault($exception = null, $code = null) { if (isset($this->_functions[$this->_method])) { $function = $this->_functions[$this->_method]; } elseif (isset($this->_method)) { $function = $this->_method; } else { $function = 'rest'; } if ($function instanceof Zend_Server_Reflection_Method) { $class = $function->getDeclaringClass()->getName(); } else { $class = false; } if ($function instanceof Zend_Server_Reflection_Function_Abstract) { $method = $function->getName(); } else { $method = $function; } $dom = new DOMDocument('1.0', 'UTF-8'); if ($class) { $xml = $dom->createElement($class); $xmlMethod = $dom->createElement($method); $xml->appendChild($xmlMethod); } else { $xml = $dom->createElement($method); $xmlMethod = $xml; } $xml->setAttribute('generator', 'zend'); $xml->setAttribute('version', '1.0'); $dom->appendChild($xml); $xmlResponse = $dom->createElement('response'); $xmlMethod->appendChild($xmlResponse); if ($exception instanceof Exception) { $element = $dom->createElement('message'); $element->appendChild($dom->createTextNode($exception->getMessage())); $xmlResponse->appendChild($element); $code = $exception->getCode(); } elseif (!is_null($exception) || 'rest' == $function) { $xmlResponse->appendChild($dom->createElement('message', 'An unknown error occured. Please try again.')); } else { $xmlResponse->appendChild($dom->createElement('message', 'Call to ' . $method . ' failed.')); } $xmlMethod->appendChild($xmlResponse); $xmlMethod->appendChild($dom->createElement('status', 'failed')); // Headers to send if (is_null($code) || (404 != $code)) { $this->_headers[] = 'HTTP/1.0 400 Bad Request'; } else { $this->_headers[] = 'HTTP/1.0 404 File Not Found'; } return $dom; } /** * Retrieve any HTTP extra headers set by the server * * @return array */ public function getHeaders() { return $this->_headers; } /** * Implement Zend_Server_Interface::addFunction() * * @param string $function Function Name * @param string $namespace Function namespace (unused) */ public function addFunction($function, $namespace = '') { if (!is_array($function)) { $function = (array) $function; } foreach ($function as $func) { if (is_callable($func) && !in_array($func, self::$magic_methods)) { $this->_functions[$func] = $this->_reflection->reflectFunction($func); } else { throw new Zend_Rest_Server_Exception("Invalid Method Added to Service."); } } } /** * Implement Zend_Server_Interface::getFunctions() * * @return array An array of Zend_Server_Reflection_Method's */ public function getFunctions() { return $this->_functions; } /** * Implement Zend_Server_Interface::loadFunctions() * * @todo Implement * @param array $functions */ public function loadFunctions($functions) { } /** * Implement Zend_Server_Interface::setPersistence() * * @todo Implement * @param int $mode */ public function setPersistence($mode) { }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -