7
* This source file is subject to the new BSD license that is bundled
8
* with this package in the file LICENSE.txt.
9
* It is also available through the world-wide-web at this URL:
10
* http://framework.zend.com/license/new-bsd
11
* If you did not receive a copy of the license and are unable to
12
* obtain it through the world-wide-web, please send an email
13
* to license@zend.com so we can send you a copy immediately.
16
* @package Zend_XmlRpc
18
* @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
19
* @license http://framework.zend.com/license/new-bsd New BSD License
20
* @version $Id: Fault.php 9102 2008-03-30 20:27:03Z thomas $
26
require_once 'Zend/XmlRpc/Fault.php';
30
* XMLRPC Server Faults
32
* Encapsulates an exception for use as an XMLRPC fault response. Valid
33
* exception classes that may be used for generating the fault code and fault
34
* string can be attached using {@link attachFaultException()}; all others use a
35
* generic '404 Unknown error' response.
37
* You may also attach fault observers, which would allow you to monitor
38
* particular fault cases; this is done via {@link attachObserver()}. Observers
39
* need only implement a static 'observe' method.
41
* To allow method chaining, you may use the {@link getInstance()} factory
42
* to instantiate a Zend_XmlRpc_Server_Fault.
45
* @package Zend_XmlRpc
47
* @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
48
* @license http://framework.zend.com/license/new-bsd New BSD License
50
class Zend_XmlRpc_Server_Fault extends Zend_XmlRpc_Fault
55
protected $_exception;
58
* @var array Array of exception classes that may define xmlrpc faults
60
protected static $_faultExceptionClasses = array('Zend_XmlRpc_Server_Exception' => true);
63
* @var array Array of fault observers
65
protected static $_observers = array();
71
* @return Zend_XmlRpc_Server_Fault
73
public function __construct(Exception $e)
75
$this->_exception = $e;
77
$message = 'Unknown error';
78
$exceptionClass = get_class($e);
80
foreach (array_keys(self::$_faultExceptionClasses) as $class) {
81
if ($e instanceof $class) {
82
$code = $e->getCode();
83
$message = $e->getMessage();
88
parent::__construct($code, $message);
90
// Notify exception observers, if present
91
if (!empty(self::$_observers)) {
92
foreach (array_keys(self::$_observers) as $observer) {
93
call_user_func(array($observer, 'observe'), $this);
99
* Return Zend_XmlRpc_Server_Fault instance
101
* @param Exception $e
102
* @return Zend_XmlRpc_Server_Fault
104
public static function getInstance(Exception $e)
110
* Attach valid exceptions that can be used to define xmlrpc faults
112
* @param string|array $classes Class name or array of class names
115
public static function attachFaultException($classes)
117
if (!is_array($classes)) {
118
$classes = (array) $classes;
121
foreach ($classes as $class) {
122
if (is_string($class) && class_exists($class)) {
123
self::$_faultExceptionClasses[$class] = true;
129
* Detach fault exception classes
131
* @param string|array $classes Class name or array of class names
134
public static function detachFaultException($classes)
136
if (!is_array($classes)) {
137
$classes = (array) $classes;
140
foreach ($classes as $class) {
141
if (is_string($class) && isset(self::$_faultExceptionClasses[$class])) {
142
unset(self::$_faultExceptionClasses[$class]);
148
* Attach an observer class
150
* Allows observation of xmlrpc server faults, thus allowing logging or mail
151
* notification of fault responses on the xmlrpc server.
153
* Expects a valid class name; that class must have a public static method
154
* 'observe' that accepts an exception as its sole argument.
156
* @param string $class
159
public static function attachObserver($class)
161
if (!is_string($class)
162
|| !class_exists($class)
163
|| !is_callable(array($class, 'observe')))
168
if (!isset(self::$_observers[$class])) {
169
self::$_observers[$class] = true;
178
* @param string $class
181
public static function detachObserver($class)
183
if (!isset(self::$_observers[$class])) {
187
unset(self::$_observers[$class]);
192
* Retrieve the exception
197
public function getException()
199
return $this->_exception;