<?php
/**
  * Ahead WebSoft Technologies Pvt Ltd.
  * @copyright  Copyright 2006-2026 Ahead WebSoft Technologies Pvt Ltd.
  * @license    https://www.socialengine.com/policies
*/
class Engine_Exception extends Exception
{
  /**
   * Last error message.
   *
   * @var array
   */
  private static $last = array();

  /**
   * Static logger for exceptions
   * 
   * @var Zend_Log
   */
  static protected $_log;

  static protected $_exitImmediately = false;

  /**
   * Inject logging logic
   * @see Exception
   */
  public function __construct($message = '', $code = 0, Exception $previous = null)
  {
    if( version_compare(PHP_VERSION, '5.3.0') >= 0 ) { // Add previous if we're >= 5.3.0
      parent::__construct((string) $message, (int) $code, $previous);
    } else {
      parent::__construct((string) $message, (int) $code);
    }

    self::$last = array(
      'message' => $message,
      'code' => $code
    );

    if( null !== self::$_log )
    {
      self::$_log->log($this->__toString(), Zend_Log::WARN);
    }

    if( true === self::$_exitImmediately ) {
      echo $this->__toString();
      die();
    }
  }

  /**
   * Set the exception logger
   * 
   * @param Zend_Log $log
   */
  static public function setLog(Zend_Log $log = null)
  {
    self::$_log = $log;
  }

  /**
   * Get the excetpion logger
   *
   * @return Zend_Log
   */
  static public function getLog()
  {
    return self::$_log;
  }

  /**
   * Get the last error message, if it exists.
   *
   * @return array
   */
  public static function getLastError()
  {
    return self::$last;
  }
}
