[ Index ]
 

Code source de PHP PEAR 1.4.5

Accédez au Source d'autres logiciels libresSoutenez Angelica Josefina !

title

Body

[fermer]

/PEAR/ -> Exception.php (source)

   1  <?php
   2  /* vim: set expandtab tabstop=4 shiftwidth=4 foldmethod=marker: */
   3  /**
   4   * PEAR_Exception
   5   *
   6   * PHP versions 4 and 5
   7   *
   8   * LICENSE: This source file is subject to version 3.0 of the PHP license
   9   * that is available through the world-wide-web at the following URI:
  10   * http://www.php.net/license/3_0.txt.  If you did not receive a copy of
  11   * the PHP License and are unable to obtain it through the web, please
  12   * send a note to license@php.net so we can mail you a copy immediately.
  13   *
  14   * @category   pear
  15   * @package    PEAR
  16   * @author     Tomas V. V. Cox <cox@idecnet.com>
  17   * @author     Hans Lellelid <hans@velum.net>
  18   * @author     Bertrand Mansion <bmansion@mamasam.com>
  19   * @author     Greg Beaver <cellog@php.net>
  20   * @copyright  1997-2006 The PHP Group
  21   * @license    http://www.php.net/license/3_0.txt  PHP License 3.0
  22   * @version    CVS: $Id: Exception.php,v 1.26 2006/10/30 03:47:48 cellog Exp $
  23   * @link       http://pear.php.net/package/PEAR
  24   * @since      File available since Release 1.3.3
  25   */
  26  
  27  
  28  /**
  29   * Base PEAR_Exception Class
  30   *
  31   * 1) Features:
  32   *
  33   * - Nestable exceptions (throw new PEAR_Exception($msg, $prev_exception))
  34   * - Definable triggers, shot when exceptions occur
  35   * - Pretty and informative error messages
  36   * - Added more context info available (like class, method or cause)
  37   * - cause can be a PEAR_Exception or an array of mixed
  38   *   PEAR_Exceptions/PEAR_ErrorStack warnings
  39   * - callbacks for specific exception classes and their children
  40   *
  41   * 2) Ideas:
  42   *
  43   * - Maybe a way to define a 'template' for the output
  44   *
  45   * 3) Inherited properties from PHP Exception Class:
  46   *
  47   * protected $message
  48   * protected $code
  49   * protected $line
  50   * protected $file
  51   * private   $trace
  52   *
  53   * 4) Inherited methods from PHP Exception Class:
  54   *
  55   * __clone
  56   * __construct
  57   * getMessage
  58   * getCode
  59   * getFile
  60   * getLine
  61   * getTraceSafe
  62   * getTraceSafeAsString
  63   * __toString
  64   *
  65   * 5) Usage example
  66   *
  67   * <code>
  68   *  require_once 'PEAR/Exception.php';
  69   *
  70   *  class Test {
  71   *     function foo() {
  72   *         throw new PEAR_Exception('Error Message', ERROR_CODE);
  73   *     }
  74   *  }
  75   *
  76   *  function myLogger($pear_exception) {
  77   *     echo $pear_exception->getMessage();
  78   *  }
  79   *  // each time a exception is thrown the 'myLogger' will be called
  80   *  // (its use is completely optional)
  81   *  PEAR_Exception::addObserver('myLogger');
  82   *  $test = new Test;
  83   *  try {
  84   *     $test->foo();
  85   *  } catch (PEAR_Exception $e) {
  86   *     print $e;
  87   *  }
  88   * </code>
  89   *
  90   * @category   pear
  91   * @package    PEAR
  92   * @author     Tomas V.V.Cox <cox@idecnet.com>
  93   * @author     Hans Lellelid <hans@velum.net>
  94   * @author     Bertrand Mansion <bmansion@mamasam.com>
  95   * @author     Greg Beaver <cellog@php.net>
  96   * @copyright  1997-2006 The PHP Group
  97   * @license    http://www.php.net/license/3_0.txt  PHP License 3.0
  98   * @version    Release: 1.5.0
  99   * @link       http://pear.php.net/package/PEAR
 100   * @since      Class available since Release 1.3.3
 101   *
 102   */
 103  class PEAR_Exception extends Exception
 104  {
 105      const OBSERVER_PRINT = -2;
 106      const OBSERVER_TRIGGER = -4;
 107      const OBSERVER_DIE = -8;
 108      protected $cause;
 109      private static $_observers = array();
 110      private static $_uniqueid = 0;
 111      private $_trace;
 112  
 113      /**
 114       * Supported signatures:
 115       *  - PEAR_Exception(string $message);
 116       *  - PEAR_Exception(string $message, int $code);
 117       *  - PEAR_Exception(string $message, Exception $cause);
 118       *  - PEAR_Exception(string $message, Exception $cause, int $code);
 119       *  - PEAR_Exception(string $message, PEAR_Error $cause);
 120       *  - PEAR_Exception(string $message, PEAR_Error $cause, int $code);
 121       *  - PEAR_Exception(string $message, array $causes);
 122       *  - PEAR_Exception(string $message, array $causes, int $code);
 123       * @param string exception message
 124       * @param int|Exception|PEAR_Error|array|null exception cause
 125       * @param int|null exception code or null
 126       */
 127      public function __construct($message, $p2 = null, $p3 = null)
 128      {
 129          if (is_int($p2)) {
 130              $code = $p2;
 131              $this->cause = null;
 132          } elseif (is_object($p2) || is_array($p2)) {
 133              // using is_object allows both Exception and PEAR_Error
 134              if (is_object($p2) && !($p2 instanceof Exception)) {
 135                  if (!class_exists('PEAR_Error') || !($p2 instanceof PEAR_Error)) {
 136                      throw new PEAR_Exception('exception cause must be Exception, ' .
 137                          'array, or PEAR_Error');
 138                  }
 139              }
 140              $code = $p3;
 141              if (is_array($p2) && isset($p2['message'])) {
 142                  // fix potential problem of passing in a single warning
 143                  $p2 = array($p2);
 144              }
 145              $this->cause = $p2;
 146          } else {
 147              $code = null;
 148              $this->cause = null;
 149          }
 150          parent::__construct($message, $code);
 151          $this->signal();
 152      }
 153  
 154      /**
 155       * @param mixed $callback  - A valid php callback, see php func is_callable()
 156       *                         - A PEAR_Exception::OBSERVER_* constant
 157       *                         - An array(const PEAR_Exception::OBSERVER_*,
 158       *                           mixed $options)
 159       * @param string $label    The name of the observer. Use this if you want
 160       *                         to remove it later with removeObserver()
 161       */
 162      public static function addObserver($callback, $label = 'default')
 163      {
 164          self::$_observers[$label] = $callback;
 165      }
 166  
 167      public static function removeObserver($label = 'default')
 168      {
 169          unset(self::$_observers[$label]);
 170      }
 171  
 172      /**
 173       * @return int unique identifier for an observer
 174       */
 175      public static function getUniqueId()
 176      {
 177          return self::$_uniqueid++;
 178      }
 179  
 180      private function signal()
 181      {
 182          foreach (self::$_observers as $func) {
 183              if (is_callable($func)) {
 184                  call_user_func($func, $this);
 185                  continue;
 186              }
 187              settype($func, 'array');
 188              switch ($func[0]) {
 189                  case self::OBSERVER_PRINT :
 190                      $f = (isset($func[1])) ? $func[1] : '%s';
 191                      printf($f, $this->getMessage());
 192                      break;
 193                  case self::OBSERVER_TRIGGER :
 194                      $f = (isset($func[1])) ? $func[1] : E_USER_NOTICE;
 195                      trigger_error($this->getMessage(), $f);
 196                      break;
 197                  case self::OBSERVER_DIE :
 198                      $f = (isset($func[1])) ? $func[1] : '%s';
 199                      die(printf($f, $this->getMessage()));
 200                      break;
 201                  default:
 202                      trigger_error('invalid observer type', E_USER_WARNING);
 203              }
 204          }
 205      }
 206  
 207      /**
 208       * Return specific error information that can be used for more detailed
 209       * error messages or translation.
 210       *
 211       * This method may be overridden in child exception classes in order
 212       * to add functionality not present in PEAR_Exception and is a placeholder
 213       * to define API
 214       *
 215       * The returned array must be an associative array of parameter => value like so:
 216       * <pre>
 217       * array('name' => $name, 'context' => array(...))
 218       * </pre>
 219       * @return array
 220       */
 221      public function getErrorData()
 222      {
 223          return array();
 224      }
 225  
 226      /**
 227       * Returns the exception that caused this exception to be thrown
 228       * @access public
 229       * @return Exception|array The context of the exception
 230       */
 231      public function getCause()
 232      {
 233          return $this->cause;
 234      }
 235  
 236      /**
 237       * Function must be public to call on caused exceptions
 238       * @param array
 239       */
 240      public function getCauseMessage(&$causes)
 241      {
 242          $trace = $this->getTraceSafe();
 243          $cause = array('class'   => get_class($this),
 244                         'message' => $this->message,
 245                         'file' => 'unknown',
 246                         'line' => 'unknown');
 247          if (isset($trace[0])) {
 248              if (isset($trace[0]['file'])) {
 249                  $cause['file'] = $trace[0]['file'];
 250                  $cause['line'] = $trace[0]['line'];
 251              }
 252          }
 253          $causes[] = $cause;
 254          if ($this->cause instanceof PEAR_Exception) {
 255              $this->cause->getCauseMessage($causes);
 256          } elseif ($this->cause instanceof Exception) {
 257              $causes[] = array('class'   => get_class($this->cause),
 258                                'message' => $this->cause->getMessage(),
 259                                'file' => $this->cause->getFile(),
 260                                'line' => $this->cause->getLine());
 261          } elseif (class_exists('PEAR_Error') && $this->cause instanceof PEAR_Error) {
 262              $causes[] = array('class' => get_class($this->cause),
 263                                'message' => $this->cause->getMessage());
 264          } elseif (is_array($this->cause)) {
 265              foreach ($this->cause as $cause) {
 266                  if ($cause instanceof PEAR_Exception) {
 267                      $cause->getCauseMessage($causes);
 268                  } elseif ($cause instanceof Exception) {
 269                      $causes[] = array('class'   => get_class($cause),
 270                                     'message' => $cause->getMessage(),
 271                                     'file' => $cause->getFile(),
 272                                     'line' => $cause->getLine());
 273                  } elseif (class_exists('PEAR_Error') && $cause instanceof PEAR_Error) {
 274                      $causes[] = array('class' => get_class($cause),
 275                                        'message' => $cause->getMessage());
 276                  } elseif (is_array($cause) && isset($cause['message'])) {
 277                      // PEAR_ErrorStack warning
 278                      $causes[] = array(
 279                          'class' => $cause['package'],
 280                          'message' => $cause['message'],
 281                          'file' => isset($cause['context']['file']) ?
 282                                              $cause['context']['file'] :
 283                                              'unknown',
 284                          'line' => isset($cause['context']['line']) ?
 285                                              $cause['context']['line'] :
 286                                              'unknown',
 287                      );
 288                  }
 289              }
 290          }
 291      }
 292  
 293      public function getTraceSafe()
 294      {   
 295          if (!isset($this->_trace)) {
 296              $this->_trace = $this->getTrace();
 297              if (empty($this->_trace)) {
 298                  $backtrace = debug_backtrace();
 299                  $this->_trace = array($backtrace[count($backtrace)-1]);
 300              }
 301          }
 302          return $this->_trace;
 303      }
 304  
 305      public function getErrorClass()
 306      {
 307          $trace = $this->getTraceSafe();
 308          return $trace[0]['class'];
 309      }
 310  
 311      public function getErrorMethod()
 312      {
 313          $trace = $this->getTraceSafe();
 314          return $trace[0]['function'];
 315      }
 316  
 317      public function __toString()
 318      {
 319          if (isset($_SERVER['REQUEST_URI'])) {
 320              return $this->toHtml();
 321          }
 322          return $this->toText();
 323      }
 324  
 325      public function toHtml()
 326      {
 327          $trace = $this->getTraceSafe();
 328          $causes = array();
 329          $this->getCauseMessage($causes);
 330          $html =  '<table border="1" cellspacing="0">' . "\n";
 331          foreach ($causes as $i => $cause) {
 332              $html .= '<tr><td colspan="3" bgcolor="#ff9999">'
 333                 . str_repeat('-', $i) . ' <b>' . $cause['class'] . '</b>: '
 334                 . htmlspecialchars($cause['message']) . ' in <b>' . $cause['file'] . '</b> '
 335                 . 'on line <b>' . $cause['line'] . '</b>'
 336                 . "</td></tr>\n";
 337          }
 338          $html .= '<tr><td colspan="3" bgcolor="#aaaaaa" align="center"><b>Exception trace</b></td></tr>' . "\n"
 339                 . '<tr><td align="center" bgcolor="#cccccc" width="20"><b>#</b></td>'
 340                 . '<td align="center" bgcolor="#cccccc"><b>Function</b></td>'
 341                 . '<td align="center" bgcolor="#cccccc"><b>Location</b></td></tr>' . "\n";
 342  
 343          foreach ($trace as $k => $v) {
 344              $html .= '<tr><td align="center">' . $k . '</td>'
 345                     . '<td>';
 346              if (!empty($v['class'])) {
 347                  $html .= $v['class'] . $v['type'];
 348              }
 349              $html .= $v['function'];
 350              $args = array();
 351              if (!empty($v['args'])) {
 352                  foreach ($v['args'] as $arg) {
 353                      if (is_null($arg)) $args[] = 'null';
 354                      elseif (is_array($arg)) $args[] = 'Array';
 355                      elseif (is_object($arg)) $args[] = 'Object('.get_class($arg).')';
 356                      elseif (is_bool($arg)) $args[] = $arg ? 'true' : 'false';
 357                      elseif (is_int($arg) || is_double($arg)) $args[] = $arg;
 358                      else {
 359                          $arg = (string)$arg;
 360                          $str = htmlspecialchars(substr($arg, 0, 16));
 361                          if (strlen($arg) > 16) $str .= '&hellip;';
 362                          $args[] = "'" . $str . "'";
 363                      }
 364                  }
 365              }
 366              $html .= '(' . implode(', ',$args) . ')'
 367                     . '</td>'
 368                     . '<td>' . (isset($v['file']) ? $v['file'] : 'unknown')
 369                     . ':' . (isset($v['line']) ? $v['line'] : 'unknown')
 370                     . '</td></tr>' . "\n";
 371          }
 372          $html .= '<tr><td align="center">' . ($k+1) . '</td>'
 373                 . '<td>{main}</td>'
 374                 . '<td>&nbsp;</td></tr>' . "\n"
 375                 . '</table>';
 376          return $html;
 377      }
 378  
 379      public function toText()
 380      {
 381          $causes = array();
 382          $this->getCauseMessage($causes);
 383          $causeMsg = '';
 384          foreach ($causes as $i => $cause) {
 385              $causeMsg .= str_repeat(' ', $i) . $cause['class'] . ': '
 386                     . $cause['message'] . ' in ' . $cause['file']
 387                     . ' on line ' . $cause['line'] . "\n";
 388          }
 389          return $causeMsg . $this->getTraceAsString();
 390      }
 391  }
 392  
 393  ?>


Généré le : Sun Feb 25 14:08:00 2007 par Balluche grâce à PHPXref 0.7