PageRenderTime 74ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/src/application/libraries/Zend/XmlRpc/Server/Fault.php

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