PEAR
[ class tree: PEAR ] [ index: PEAR ] [ all elements ]

Source for file Exception.php

Documentation is available at Exception.php

  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4 foldmethod=marker: */
  3. // +----------------------------------------------------------------------+
  4. // | PEAR_Exception                                                       |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 2004 The PEAR Group                                    |
  7. // +----------------------------------------------------------------------+
  8. // | This source file is subject to version 3.0 of the PHP license,       |
  9. // | that is bundled with this package in the file LICENSE, and is        |
  10. // | available at through the world-wide-web at                           |
  11. // | http://www.php.net/license/3_0.txt.                                  |
  12. // | If you did not receive a copy of the PHP license and are unable to   |
  13. // | obtain it through the world-wide-web, please send a note to          |
  14. // | license@php.net so we can mail you a copy immediately.               |
  15. // +----------------------------------------------------------------------+
  16. // | Authors: Tomas V.V.Cox <cox@idecnet.com>                             |
  17. // |          Hans Lellelid <hans@velum.net>                              |
  18. // +----------------------------------------------------------------------+
  19. //
  20. // $Id: Exception.php,v 1.1 2004/08/19 18:28:15 gurugeek Exp $
  21.  
  22. define('PEAR_OBSERVER_PRINT',      -2);
  23. define('PEAR_OBSERVER_TRIGGER',    -4);
  24. define('PEAR_OBSERVER_DIE',        -8);
  25.  
  26. /**
  27.  * Base PEAR_Exception Class
  28.  *
  29.  * Features:
  30.  *
  31.  * - Nestable exceptions (throw new PEAR_Exception($msg, $prev_exception))
  32.  * - Definable triggers, shot when exceptions occur
  33.  * - Pretty and informative error messages
  34.  * - Added more context info avaible (like class, method or cause)
  35.  *
  36.  * Ideas:
  37.  *
  38.  * - Maybe a way to define a 'template' for the output
  39.  *
  40.  * 1) Inherited properties from PHP Exception Class:
  41.  *
  42.  * protected $message
  43.  * protected $code
  44.  * protected $line
  45.  * protected $file
  46.  * private   $trace
  47.  *
  48.  * 2) Inherited methods from PHP Exception Class:
  49.  *
  50.  * __clone
  51.  * __construct
  52.  * getMessage
  53.  * getCode
  54.  * getFile
  55.  * getLine
  56.  * getTrace
  57.  * getTraceAsString
  58.  * __toString
  59.  *
  60.  * 3) Usage example
  61.  *
  62.  * <code>
  63.  *  require_once 'PEAR/Exception.php';
  64.  *
  65.  *  class Test {
  66.  *     function foo() {
  67.  *         throw new PEAR_Exception('Error Message', ERROR_CODE);
  68.  *     }
  69.  *  }
  70.  *
  71.  *  function myLogger($pear_exception) {
  72.  *     echo $pear_exception->getMessage();
  73.  *  }
  74.  *  // each time a exception is thrown the 'myLogger' will be called
  75.  *  // (its use is completely optional)
  76.  *  PEAR_Exception::addObserver('mylogger', 'myLogger');
  77.  *  $test = new Test;
  78.  *  try {
  79.  *     $test->foo();
  80.  *  } catch (PEAR_Exception $e) {
  81.  *     print $e;
  82.  *  }
  83.  * </code>
  84.  *
  85.  * @since PHP 5
  86.  * @package PEAR
  87.  * @version $Rev:$
  88.  * @author Tomas V.V.Cox <cox@idecnet.com>
  89.  * @author Hans Lellelid <hans@velum.net>
  90.  *
  91.  */
  92. class DB_Sqlite_Tools_Exception extends Exception
  93. {
  94.     protected $cause;
  95.     protected $error_class;
  96.     protected $error_method;
  97.  
  98.     private $method;
  99.     private static $_observers = array();
  100.  
  101.     /**
  102.      * Supported signatures:
  103.      * PEAR_Exception(string $message);
  104.      * PEAR_Exception(string $message, int $code);
  105.      * PEAR_Exception(string $message, Exception $cause);
  106.      * PEAR_Exception(string $message, Exception $cause, int $code);
  107.      */
  108.     public function __construct($message$p2 = null$p3 = null)
  109.     {
  110.         $code = null;
  111.         $cause = null;
  112.         if (is_int($p3&& $p2 instanceof Exception{
  113.             $code $p3;
  114.             $cause $p2;
  115.         elseif (is_int($p2)) {
  116.             $code $p2;
  117.         elseif ($p2 instanceof Exception{
  118.             $cause $p2;
  119.         }
  120.         $this->cause = $cause;
  121.         $trace       = parent::getTrace();
  122.         $this->error_class  = $trace[0]['class'];
  123.         $this->error_method = $trace[0]['function'];
  124.         $this->method $this->error_class.'::'.$this->error_method.'()';
  125.         parent::__construct($message$code);
  126.  
  127.         $this->signal();
  128.     }
  129.  
  130.     public static function addObserver($label$callback)
  131.     {
  132.         self::$_observers[$label$callback;
  133.     }
  134.  
  135.     public static function delObserver($label)
  136.     {
  137.         unset(self::$_observers[$label]);
  138.     }
  139.  
  140.     private function signal()
  141.     {
  142.         foreach (self::$_observers as $func{
  143.             if (is_callable($func)) {
  144.                 call_user_func($func$this);
  145.                 continue;
  146.             }
  147.             settype($func'array');
  148.             switch ($func[0]{
  149.                 case PEAR_OBSERVER_PRINT:
  150.                     $f (isset($func[1])) $func[1'%s';
  151.                     printf($f$this->getMessage());
  152.                     break;
  153.                 case PEAR_OBSERVER_TRIGGER:
  154.                     $f (isset($func[1])) $func[1: E_USER_NOTICE;
  155.                     trigger_error($this->getMessage()$f);
  156.                     break;
  157.                 case PEAR_OBSERVER_DIE:
  158.                     $f (isset($func[1])) $func[1'%s';
  159.                     die(printf($f$this->getMessage()));
  160.                     break;
  161.                 default:
  162.                     trigger_error('invalid observer type'E_USER_WARNING);
  163.             }
  164.         }
  165.     }
  166.  
  167.     public function getCauseMessage($obj)
  168.     {
  169.         $msg '     ' $obj->method . " at {$obj->file} ({$obj->line})\n";
  170.         if ($obj->cause instanceof Exception{
  171.             return $msg.$obj->getCauseMessage($obj->cause);
  172.         }
  173.         return $msg;
  174.     }
  175.  
  176.     /**
  177.      * @return Exception_object The context of the exception
  178.      */
  179.     public function getCause()
  180.     {
  181.         return $this->cause;
  182.     }
  183.  
  184.     public function getErrorClass()
  185.     {
  186.         return $this->error_class;
  187.     }
  188.  
  189.     public function getErrorMethod()
  190.     {
  191.         return $this->error_method;
  192.     }
  193.  
  194.     public function __toString()
  195.     {
  196.         $str get_class($this" occurred: \n" .
  197.                '  Error message: ' $this->message "\n" .
  198.                '  Error code   : ' $this->code "\n" .
  199.                '  File (Line)  : ' . "{$this->file} ({$this->line})\n" .
  200.                '  Method       : ' . $this->method "\n";
  201.         if ($this->cause instanceof Exception{
  202.             $str .= "  Nested Error : \n".$this->getCauseMessage($this);
  203.         }
  204.         if (isset($_SERVER['REQUEST_URI'])) {
  205.             return nl2br('<pre>'.htmlentities($str).'</pre>');
  206.         }
  207.         return $str;
  208.     }
  209. }
  210.  

Documentation generated on Mon, 11 Mar 2019 13:55:48 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.