Source for file Handler.class.php

Documentation is available at Handler.class.php

  1. <?php
  2. /**
  3.  * Gumbo Library Framework
  4.  *
  5.  * LICENSE
  6.  * This library is being released under the terms of the New BSD License.  A
  7.  * copy of the license is packaged with the software (LICENSE.txt).  If no
  8.  * copy is found, a copy of the license template can be found at:
  9.  * http://www.opensource.org/licenses/bsd-license.php
  10.  * 
  11.  * @category Gumbo
  12.  * @package Error
  13.  * @copyright Copyright (c) 2007, iBayou, Michael Luster
  14.  * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
  15.  * @author Michael Luster <mluster79@yahoo.com>
  16.  * @link http://sourceforge.net/projects/phpgumbo
  17.  * @version 0.0.1
  18.  */
  19.  
  20. /**
  21.  * Error Handler Class
  22.  * 
  23.  * This class will process any Exceptions thrown by the program.  Any thrown Exceptions
  24.  * should be sent to the Exception Handler through the trigger method.  This can be
  25.  * done either by sending a thrown Exception to the handler, creating a new Exception and
  26.  * sending the object to trigger, or by using the trigger_error() function.
  27.  * 
  28.  * <pre>
  29.  * try {
  30.  *         if ( // test condition failes ) {
  31.  *             throw new Gumbo_Exception ("message");
  32.  *         }
  33.  *         // successful test
  34.  *         ...
  35.  * } catch (Exception $e) {
  36.  *         if ($e instanceof Gumbo_Interface_Exception) {
  37.  *             $e->setFunction (__METHOD__);
  38.  *         }
  39.  *         Gumbo_Error_Handler::trigger ($e);
  40.  * }
  41.  * </pre>
  42.  * 
  43.  * <pre>
  44.  * $e = new Gumbo_Exception ("message", ERROR_LEVEL, __FILE__, __LINE__, __METHOD__);
  45.  * Gumbo_Error_Handler::trigger ($e);
  46.  * </pre>
  47.  * 
  48.  * <pre>
  49.  * trigger_error ("message", ERROR_LEVEL);
  50.  * </pre>
  51.  * 
  52.  * The class file provides a simple function that wraps the trigger method.  This function
  53.  * is simply called 'trigger (...)'.  This will help from having to type the entire static
  54.  * class method.
  55.  * <pre>
  56.  * ...
  57.  * trigger ($e);
  58.  * </pre>
  59.  *
  60.  * @category Gumbo
  61.  * @package Error
  62.  * @copyright Copyright (c) 2007, iBayou, Michael Luster
  63.  * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
  64.  * @author Michael Luster <mluster79@yahoo.com>
  65.  * @link http://sourceforge.net/projects/phpgumbo
  66.  * @desc Error Handler Class
  67.  * @version 0.0.1
  68.  */
  69.  
  70. gumbo_load ("Interface_Error_Handler");
  71.  
  72. class Gumbo_Error_Handler implements Gumbo_Interface_Error_Handler {
  73.     
  74.     /**
  75.      * Catches an Error triggered by the program
  76.      * @param int|Exception$errno Error code (trigger_error), Exception object
  77.      * @param string $errstr Error message, Exception class name
  78.      * @param string $errfile location of Error occurence
  79.      * @param int $errline line number Error occured
  80.      * @param string $errfunc function or class::method where error occured
  81.      * @uses Gumbo_Exception, Gumbo_Error
  82.      */
  83.     public static function trigger ($errno, $errstr=null, $errfile=null, $errline=null, $errfunc=null) {
  84.         gumbo_load ("Exception");
  85.         gumbo_load ("Error");
  86.         
  87.         // verify precondition
  88.         if (!is_int ($errno) && !is_object ($errno)) { $errno = null; }
  89.         if (is_object ($errno) && !($errno instanceof Exception)) { $errno = null; }
  90.         if (!is_null ($errstr) && !is_string ($errstr)) { $errstr = "Unknown"; }
  91.         if (!is_null ($errfile) && !is_string ($errfile)) { $errfile = "unknown"; }
  92.         if (!is_null ($errline) && !is_int ($errline)) { $errline = null; }
  93.         if (!is_null ($errfunc) && !is_string ($errfunc)) { $errfunc = null; }
  94.         
  95.         // Exception object
  96.         if ($errno instanceof Exception) {
  97.             $err = $errno;
  98.         } else {
  99.             // recieved error through trigger_error() function
  100.             if (!strstr ($errstr, " ")) {
  101.                 if (gumbo_load ($errstr) || class_exists ($errstr, false)) {
  102.                     if (class_exists ($errstr, false) && is_subclass_of ($errstr, "Gumbo_Exception")) {
  103.                         $err = new $errstr (null, $errno, $errfile, $errline, $errfunc);
  104.                     } elseif (class_exists ($errstr, false) && is_subclass_of ($errstr, "Exception")) {
  105.                         $err = new $errstr (null, $errno, $errfile, $errline);
  106.                     }
  107.                 }
  108.             } else {
  109.                 $err = new Gumbo_Exception ($errstr, $errno, $errfile, $errline, $errfunc);
  110.             }
  111.         }
  112.         
  113.         if (!($err instanceof Exception)) {
  114.             $err = new Gumbo_Exception ("Unkown Error Occured", $errno, $errfile, $errline, $errfunc);
  115.         }
  116.         
  117.         // submit error to the Error class
  118.         Gumbo_Error::instance ()->add ($err);
  119.     }
  120.     
  121.     /**
  122.      * Activates this class as the system Error Handler to the trigger method
  123.      */
  124.     public static function activate () {
  125.         set_error_handler (array ("Gumbo_Error_Handler", "trigger"));
  126.         set_exception_handler (array ("Gumbo_Error_Handler", "trigger"));
  127.     }
  128.     
  129.     /**
  130.      * Restores the system Error Handlers
  131.      * @postcondition restore_error_handler ()
  132.      * @postcondition restore_exception_handler ()
  133.      */
  134.     public static function reset () {
  135.         restore_error_handler ();
  136.         restore_exception_handler ();
  137.     }
  138.     
  139. }
  140.  
  141. ?>