<?php
/**
 * Part of Showkase web site management package
 *
 * @package Showkase
 * @author Jack Hardie {@link http://www.jhardie.com}
 * @copyright Copyright (c) 2012, SimpleViewer Inc.
 */
defined('SK_ACCESS')||die('<h1>403: Forbidden</h1>'); 
require_once 'classes'.DIRECTORY_SEPARATOR.'errorscreen.php';
require_once 'settings'.DIRECTORY_SEPARATOR.'constants.php';
 
/**
  * Exception handler
  *
  * The PHP Anthology, sitepoint
  * @package Showkase
  */
class ExceptionHandler
{
    protected $_exception;
    /**
     * Constructor
     *
     * @param object, Exception object
     */
    public function __construct(Exception $e)
    {
        $this->_exception = $e;
    }
    
    /**
     * Prints error screen
     * 
     * @param object, Exception object 
     */
    public static function handle(Exception $e)
    {
        $self = new self($e);
        while (@ob_end_clean());
        echo $self;
    }
     
    /**
     * Formats error screen
     */
    public function __toString()
    {
      // we don't want to accidentally throw any exceptions here
      try {
          $screen = new ErrorScreen();
          $message = $this->_exception->getMessage();
          if (DEBUG) $message .= '<br style="margin-bottom: 1em" />'.nl2br($this->_exception->getTraceAsString());
          $context['message'] = $message;
          return $screen->getHtml($context);
      } catch (Exception $e) {
          return 'Exception: '.$e->getMessage();
      } 
    }
}