[ Index ]

PHP Cross Reference of moodle-2.8

title

Body

[close]

/lib/zend/Zend/XmlRpc/Server/ -> Fault.php (source)

   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 [email protected] so we can send you a copy immediately.
  14   *
  15   * @category   Zend
  16   * @package    Zend_XmlRpc
  17   * @subpackage Server
  18   * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  19   * @license    http://framework.zend.com/license/new-bsd     New BSD License
  20   * @version    $Id$
  21   */
  22  
  23  /**
  24   * Zend_XmlRpc_Fault
  25   */
  26  require_once 'Zend/XmlRpc/Fault.php';
  27  
  28  
  29  /**
  30   * XMLRPC Server Faults
  31   *
  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.
  36   *
  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.
  40   *
  41   * To allow method chaining, you may use the {@link getInstance()} factory
  42   * to instantiate a Zend_XmlRpc_Server_Fault.
  43   *
  44   * @category   Zend
  45   * @package    Zend_XmlRpc
  46   * @subpackage Server
  47   * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  48   * @license    http://framework.zend.com/license/new-bsd     New BSD License
  49   */
  50  class Zend_XmlRpc_Server_Fault extends Zend_XmlRpc_Fault
  51  {
  52      /**
  53       * @var Exception
  54       */
  55      protected $_exception;
  56  
  57      /**
  58       * @var array Array of exception classes that may define xmlrpc faults
  59       */
  60      protected static $_faultExceptionClasses = array('Zend_XmlRpc_Server_Exception' => true);
  61  
  62      /**
  63       * @var array Array of fault observers
  64       */
  65      protected static $_observers = array();
  66  
  67      /**
  68       * Constructor
  69       *
  70       * @param Exception $e
  71       * @return Zend_XmlRpc_Server_Fault
  72       */
  73      public function __construct(Exception $e)
  74      {
  75          $this->_exception = $e;
  76          $code             = 404;
  77          $message          = 'Unknown error';
  78          $exceptionClass   = get_class($e);
  79  
  80          foreach (array_keys(self::$_faultExceptionClasses) as $class) {
  81              if ($e instanceof $class) {
  82                  $code    = $e->getCode();
  83                  $message = $e->getMessage();
  84                  break;
  85              }
  86          }
  87  
  88          parent::__construct($code, $message);
  89  
  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);
  94              }
  95          }
  96      }
  97  
  98      /**
  99       * Return Zend_XmlRpc_Server_Fault instance
 100       *
 101       * @param Exception $e
 102       * @return Zend_XmlRpc_Server_Fault
 103       */
 104      public static function getInstance(Exception $e)
 105      {
 106          return new self($e);
 107      }
 108  
 109      /**
 110       * Attach valid exceptions that can be used to define xmlrpc faults
 111       *
 112       * @param string|array $classes Class name or array of class names
 113       * @return void
 114       */
 115      public static function attachFaultException($classes)
 116      {
 117          if (!is_array($classes)) {
 118              $classes = (array) $classes;
 119          }
 120  
 121          foreach ($classes as $class) {
 122              if (is_string($class) && class_exists($class)) {
 123                  self::$_faultExceptionClasses[$class] = true;
 124              }
 125          }
 126      }
 127  
 128      /**
 129       * Detach fault exception classes
 130       *
 131       * @param string|array $classes Class name or array of class names
 132       * @return void
 133       */
 134      public static function detachFaultException($classes)
 135      {
 136          if (!is_array($classes)) {
 137              $classes = (array) $classes;
 138          }
 139  
 140          foreach ($classes as $class) {
 141              if (is_string($class) && isset(self::$_faultExceptionClasses[$class])) {
 142                  unset(self::$_faultExceptionClasses[$class]);
 143              }
 144          }
 145      }
 146  
 147      /**
 148       * Attach an observer class
 149       *
 150       * Allows observation of xmlrpc server faults, thus allowing logging or mail
 151       * notification of fault responses on the xmlrpc server.
 152       *
 153       * Expects a valid class name; that class must have a public static method
 154       * 'observe' that accepts an exception as its sole argument.
 155       *
 156       * @param string $class
 157       * @return boolean
 158       */
 159      public static function attachObserver($class)
 160      {
 161          if (!is_string($class)
 162              || !class_exists($class)
 163              || !is_callable(array($class, 'observe')))
 164          {
 165              return false;
 166          }
 167  
 168          if (!isset(self::$_observers[$class])) {
 169              self::$_observers[$class] = true;
 170          }
 171  
 172          return true;
 173      }
 174  
 175      /**
 176       * Detach an observer
 177       *
 178       * @param string $class
 179       * @return boolean
 180       */
 181      public static function detachObserver($class)
 182      {
 183          if (!isset(self::$_observers[$class])) {
 184              return false;
 185          }
 186  
 187          unset(self::$_observers[$class]);
 188          return true;
 189      }
 190  
 191      /**
 192       * Retrieve the exception
 193       *
 194       * @access public
 195       * @return Exception
 196       */
 197      public function getException()
 198      {
 199          return $this->_exception;
 200      }
 201  }


Generated: Fri Nov 28 20:29:05 2014 Cross-referenced by PHPXref 0.7.1