Source for file Exception_Handler.php

Documentation is available at Exception_Handler.php

  1. <?php
  2. /**
  3.  *    LAIKA FRAMEWORK Release Notes:
  4.  *
  5.  *    @filesource     Exception_Handler.php
  6.  *
  7.  *    @version        0.1.0b
  8.  *    @package        Laika
  9.  *    @subpackage     core
  10.  *    @category
  11.  *    @date           2011-05-22 08:49:46 -0400 (Sun, 22 May 2011)
  12.  *
  13.  *    @author         Leonard M. Witzel <witzel@post.harvard.edu>
  14.  *    @copyright      Copyright (c) 2011  Laika Soft <{@link http://oafbot.com}>
  15.  *
  16.  */
  17. /**
  18.  * Laika_Exception_Handler class.
  19.  * 
  20.  * Intercepts uncaught exceptions.
  21.  * Notifies Observers of intercepted exceptions.
  22.  *
  23.  * @extends Laika_Singleton
  24.  * @implements SplSubject
  25.  */
  26. class Laika_Exception_Handler extends Laika_Singleton implements SplSubject{
  27.  
  28. //-------------------------------------------------------------------
  29. //    VARIABLES
  30. //-------------------------------------------------------------------
  31.     /**
  32.     * instance of Laika_Exception_Handler
  33.     * 
  34.     * @var    object 
  35.     * @access protected
  36.     * @static
  37.     */
  38.     protected static $instance;
  39.     /**
  40.     * Array of SplObserver objects
  41.     *
  42.     * @var array 
  43.     */
  44.     private $observers = array();
  45.  
  46.     /**
  47.     * Uncaught exception
  48.     *
  49.     * @var Exception 
  50.     */
  51.     public $exception;
  52.  
  53.  
  54. //-------------------------------------------------------------------
  55. //    METHODS
  56. //-------------------------------------------------------------------
  57.     /**
  58.     * Attaches an SplObserver object to the handler
  59.     *
  60.     * @param SplObserver 
  61.     * @return void 
  62.     */
  63.     public function attach(SplObserver $observer){
  64.         $id spl_object_hash($observer);
  65.         $this->observers[$id$observer;
  66.     }
  67.  
  68.     /**
  69.     * Detaches the SplObserver object from the handler
  70.     *
  71.     * @param SplObserver 
  72.     * @return void 
  73.     */
  74.     public function detach(SplObserver $observer){
  75.         $id spl_object_hash($observer);
  76.         unset($this->observers[$id]);
  77.     }
  78.  
  79.     /**
  80.     * Notify all observers
  81.     *
  82.     * @return void 
  83.     */
  84.     public function notify(){
  85.         foreach($this->observers as $observer){
  86.             $observer->update($this);
  87.         }
  88.     }
  89.  
  90.     /**
  91.     * The Exception Handler calls notify() and outputs
  92.     * a notice onscreen if DEVELOPMENT_ENVIRONMENT is set
  93.     *
  94.     * @return void 
  95.     */
  96.     public function handle(Exception $e){
  97.         $this->exception = $e;
  98.         $this->notify();
  99.         if(DEVELOPMENT_ENVIRONMENT == true):
  100.             if(is_a($e,'ErrorException'))
  101.                 $trace 
  102.                     '<div id="php_error"><strong><span style="color:#faa51a;">PHP Interpreter ERROR ['.
  103.                     $e->getCode().']:</span>  '.
  104.                     $e->getMessage().' at Line( '.
  105.                     $e->getLine().' )in File: '.
  106.                     $e->getFile().'</strong><br /><br /><p><pre>'.
  107.                     $e->getTraceAsString().'</pre></p></div>';
  108.             else
  109.                 $trace =
  110.                     '<div id="php_error"><strong><span style="color:#faa51a;">LAIKA ERROR ['.
  111.                     $e->getCode().']:</span> '.
  112.                     $e->getMessage().' at Line( '.
  113.                     $e->getLine().' ) in File: '.
  114.                     $e->getFile().'</strong><br /><br /><p><pre>'.
  115.                     $e->getTraceAsString().'</pre></p></div>';
  116.             $this->display($trace,$e->getFile(),$e->getLine());
  117.         else:
  118.             $message '<div id="php_error">
  119.                         <strong><span style="color:#faa51a;">APPLICATION ERROR</span></strong>
  120.                         </div>';
  121.             $this->display($message,NULL,NULL)
  122.         endif;        
  123.     }
  124.     
  125.     /**
  126.      * display function.
  127.      * 
  128.      * @access public
  129.      * @param mixed $trace 
  130.      * @param mixed $file 
  131.      * @param mixed $line 
  132.      * @return void 
  133.      */
  134.     public function display($trace,$file,$line){
  135.         $exception_css HTTP_ROOT.'/stylesheets/exception.css';
  136.         $reset_css     HTTP_ROOT.'/stylesheets/reset.css';
  137.         isset($file$source highlight_file($filetrue$source "";
  138.         $page "<!DOCTYPE html>
  139.                 <html lang=en>
  140.                 <head>
  141.                 <meta charset=utf-8>
  142.                 <title>FRAMEWORK EXCEPTION</title>
  143.                 <link rel=\"shortcut icon\" href=/favicon.ico type=image/x-icon />
  144.                 <link rel=stylesheet href=$reset_css type=text/css>
  145.                 <link rel=stylesheet href=$exception_css type=text/css>
  146.                 </head>
  147.                 <body>
  148.                 <div id=main>
  149.                 $trace
  150.                 <div id=source>
  151.                 <h2>Source Code:</h2>
  152.                 $source
  153.                 </div>
  154.                 </div>
  155.                 </body>
  156.                 </html>";
  157.  
  158.        //$_SESSION['ERROR_MSG'] = $page;
  159.        //self::redirect_to('/error/exceptions');
  160.          
  161.         echo $page;
  162.     }
  163. }

Documentation generated on Sat, 19 May 2012 02:17:00 -0400 by phpDocumentor 1.4.4