*prechod na novsiu verziu ZF
[sport-group.git] / library / Zend / XmlRpc / Server / Fault.php
blob5261bf497c1cb7f971fc55d3324eb638b38c5f9c
1 <?php
2 /**
3 * Zend Framework
5 * LICENSE
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.
15 * @category Zend
16 * @package Zend_XmlRpc
17 * @subpackage Server
18 * @copyright Copyright (c) 2005-2009 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 16208 2009-06-21 19:19:26Z thomas $
23 /**
24 * Zend_XmlRpc_Fault
26 require_once 'Zend/XmlRpc/Fault.php';
29 /**
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.
44 * @category Zend
45 * @package Zend_XmlRpc
46 * @subpackage Server
47 * @copyright Copyright (c) 2005-2009 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
52 /**
53 * @var Exception
55 protected $_exception;
57 /**
58 * @var array Array of exception classes that may define xmlrpc faults
60 protected static $_faultExceptionClasses = array('Zend_XmlRpc_Server_Exception' => true);
62 /**
63 * @var array Array of fault observers
65 protected static $_observers = array();
67 /**
68 * Constructor
70 * @param Exception $e
71 * @return Zend_XmlRpc_Server_Fault
73 public function __construct(Exception $e)
75 $this->_exception = $e;
76 $code = 404;
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();
84 break;
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);
98 /**
99 * Return Zend_XmlRpc_Server_Fault instance
101 * @param Exception $e
102 * @return Zend_XmlRpc_Server_Fault
104 public static function getInstance(Exception $e)
106 return new self($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
113 * @return void
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
132 * @return void
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
157 * @return boolean
159 public static function attachObserver($class)
161 if (!is_string($class)
162 || !class_exists($class)
163 || !is_callable(array($class, 'observe')))
165 return false;
168 if (!isset(self::$_observers[$class])) {
169 self::$_observers[$class] = true;
172 return true;
176 * Detach an observer
178 * @param string $class
179 * @return boolean
181 public static function detachObserver($class)
183 if (!isset(self::$_observers[$class])) {
184 return false;
187 unset(self::$_observers[$class]);
188 return true;
192 * Retrieve the exception
194 * @access public
195 * @return Exception
197 public function getException()
199 return $this->_exception;