Fault.php
5.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
<?php
/**
* Zend Framework
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://framework.zend.com/license/new-bsd
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@zend.com so we can send you a copy immediately.
*
* @category Zend
* @package Zend_XmlRpc
* @subpackage Server
* @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @version $Id: Fault.php 23775 2011-03-01 17:25:24Z ralph $
*/
/**
* Zend_XmlRpc_Fault
*/
require_once 'Zend/XmlRpc/Fault.php';
/**
* XMLRPC Server Faults
*
* Encapsulates an exception for use as an XMLRPC fault response. Valid
* exception classes that may be used for generating the fault code and fault
* string can be attached using {@link attachFaultException()}; all others use a
* generic '404 Unknown error' response.
*
* You may also attach fault observers, which would allow you to monitor
* particular fault cases; this is done via {@link attachObserver()}. Observers
* need only implement a static 'observe' method.
*
* To allow method chaining, you may use the {@link getInstance()} factory
* to instantiate a Zend_XmlRpc_Server_Fault.
*
* @category Zend
* @package Zend_XmlRpc
* @subpackage Server
* @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
class Zend_XmlRpc_Server_Fault extends Zend_XmlRpc_Fault
{
/**
* @var Exception
*/
protected $_exception;
/**
* @var array Array of exception classes that may define xmlrpc faults
*/
protected static $_faultExceptionClasses = array('Zend_XmlRpc_Server_Exception' => true);
/**
* @var array Array of fault observers
*/
protected static $_observers = array();
/**
* Constructor
*
* @param Exception $e
* @return Zend_XmlRpc_Server_Fault
*/
public function __construct(Exception $e)
{
$this->_exception = $e;
$code = 404;
$message = 'Unknown error';
$exceptionClass = get_class($e);
foreach (array_keys(self::$_faultExceptionClasses) as $class) {
if ($e instanceof $class) {
$code = $e->getCode();
$message = $e->getMessage();
break;
}
}
parent::__construct($code, $message);
// Notify exception observers, if present
if (!empty(self::$_observers)) {
foreach (array_keys(self::$_observers) as $observer) {
call_user_func(array($observer, 'observe'), $this);
}
}
}
/**
* Return Zend_XmlRpc_Server_Fault instance
*
* @param Exception $e
* @return Zend_XmlRpc_Server_Fault
*/
public static function getInstance(Exception $e)
{
return new self($e);
}
/**
* Attach valid exceptions that can be used to define xmlrpc faults
*
* @param string|array $classes Class name or array of class names
* @return void
*/
public static function attachFaultException($classes)
{
if (!is_array($classes)) {
$classes = (array) $classes;
}
foreach ($classes as $class) {
if (is_string($class) && class_exists($class)) {
self::$_faultExceptionClasses[$class] = true;
}
}
}
/**
* Detach fault exception classes
*
* @param string|array $classes Class name or array of class names
* @return void
*/
public static function detachFaultException($classes)
{
if (!is_array($classes)) {
$classes = (array) $classes;
}
foreach ($classes as $class) {
if (is_string($class) && isset(self::$_faultExceptionClasses[$class])) {
unset(self::$_faultExceptionClasses[$class]);
}
}
}
/**
* Attach an observer class
*
* Allows observation of xmlrpc server faults, thus allowing logging or mail
* notification of fault responses on the xmlrpc server.
*
* Expects a valid class name; that class must have a public static method
* 'observe' that accepts an exception as its sole argument.
*
* @param string $class
* @return boolean
*/
public static function attachObserver($class)
{
if (!is_string($class)
|| !class_exists($class)
|| !is_callable(array($class, 'observe')))
{
return false;
}
if (!isset(self::$_observers[$class])) {
self::$_observers[$class] = true;
}
return true;
}
/**
* Detach an observer
*
* @param string $class
* @return boolean
*/
public static function detachObserver($class)
{
if (!isset(self::$_observers[$class])) {
return false;
}
unset(self::$_observers[$class]);
return true;
}
/**
* Retrieve the exception
*
* @access public
* @return Exception
*/
public function getException()
{
return $this->_exception;
}
}