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

Source for file myException.php

Documentation is available at myException.php

  1. <?php
  2. /**
  3.  * Project: myException
  4.  * File:    myException.php
  5.  *
  6.  * @category    Core
  7.  * @package     myException
  8.  * @author      JoungKyun.Kim <http://oops.org>
  9.  * @copyright   (c) 2018, OOPS.org
  10.  * @license     BSD License
  11.  * @link        http://pear.oops.org/package/myException
  12.  * @filesource
  13.  */
  14.  
  15. // {{{ constant
  16. /**
  17.  * defeine PHP FATAL error type
  18.  * @access public
  19.  * @name E_MY_FATAL
  20.  */
  21.     'E_MY_FATAL',
  22.     E_ERROR E_USER_ERROR E_PARSE E_CORE_ERROR E_COMPILE_ERROR E_RECOVERABLE_ERROR
  23. );
  24. // }}}
  25.  
  26. /**
  27.  * 기본 myException Class
  28.  *
  29.  * @category    Core
  30.  * @package     myException
  31.  * @author      JoungKyun.Kim <http://oops.org>
  32.  * @copyright   (c) 2018, OOPS.org
  33.  * @license     BSD License
  34.  * @version     $Id$
  35.  * @link        http://pear.oops.org/package/myException
  36.  * @example     myException/tests/myexp.php
  37.  */
  38. class myException extends Exception {
  39.     // {{{ properties
  40.     /**#@+
  41.      * @access private
  42.      */
  43.     /**
  44.      * Previous exception
  45.      * @var    Exception 
  46.      */
  47.     private $prev null;
  48.     /**
  49.      * PHP 버전이 5.3 이전 버전 마크. 5.3.0 이전 버전일 경우 true.
  50.      * @var    boolean 
  51.      */
  52.     private $early53 false;
  53.     /**#@-*/
  54.     // }}}
  55.  
  56.     // {{{ (void) myException::__construct ($message, $code, Exception $prev= null)
  57.     /** 
  58.      * myException class 초기화
  59.      *
  60.      * @access public
  61.      * @return void 
  62.      * @param  string 에러 메시지
  63.      * @param  string 에러 코드
  64.      * @param  string 이전 예외 배열
  65.      */
  66.     public function __construct($message$code 0Exception $prev null{
  67.         if version_compare (PHP_VERSION'5.3.0''>=') )
  68.             parent::__construct($message$code$prev);
  69.         else {
  70.             $this->early53 true;
  71.             $this->prev $prev;
  72.             parent::__construct($message$code);
  73.         }
  74.     }
  75.     // }}}
  76.  
  77.     // {{{ (Exception) myException::Previous (void)
  78.     /**
  79.      * __construct method의 3번째 인자값인 previous exception을 반환
  80.      *
  81.      * @access public
  82.      * @return Exception prevous exception
  83.      * @param  void 
  84.      */
  85.     function Previous ({
  86.         return $this->early53 $this->prev $this->getPrevious ();
  87.     }
  88.     // }}}
  89.  
  90.     // {{{ (array) myException::Trace (void)
  91.     /**
  92.      * Exception 스택을 배열로 반환
  93.      *
  94.      * @access public
  95.      * @return array  Exception 스택을 배열로 반환
  96.      * @param  void 
  97.      */
  98.     function Trace ({
  99.         $r $this->Previous ();
  100.         if $r instanceof Exception )
  101.             return $r->getTrace();
  102.  
  103.         return $this->getTrace ();
  104.     }
  105.     // }}}
  106.  
  107.     // {{{ (array) myException::Message (void)
  108.     /**
  109.      * Exception 에러 메시지를 PHP 에러 메시지 형식으로 반환
  110.      *
  111.      * @access public
  112.      * @return string 
  113.      * @param  void 
  114.      */
  115.     function Message ({
  116.         $r $this->Previous ();
  117.         if $r instanceof Exception )
  118.             $e &$r;
  119.         else
  120.             $e &$this;
  121.  
  122.         return sprintf (
  123.             "%s: %s [%s:%d]",
  124.             self::errStr ($e->getCode ()),
  125.             $e->getMessage (),
  126.             preg_replace ('!.*/!'''$e->getFile ()),
  127.             $e->getLine ()
  128.         );
  129.     }
  130.     // }}}
  131.  
  132.     // {{{ (string) myException::TraceAsString (void)
  133.     /**
  134.      * Exception 스택 trace를 문자열로 반환
  135.      *
  136.      * @access public
  137.      * @return string 
  138.      * @param  void 
  139.      */
  140.     function TraceAsString ({
  141.         $r $this->Previous ();
  142.         if $r instanceof Exception )
  143.             return $r->getTraceAsString ();
  144.  
  145.         return $this->getTraceAsString ();
  146.     }
  147.     // }}}
  148.  
  149.     // {{{ (array) myException::TraceAsArray (void)
  150.     /**
  151.      * Exception 스택 trace를 배열로 반환
  152.      *
  153.      *
  154.      * @access public
  155.      * @return array 
  156.      * @param  void 
  157.      */
  158.     function TraceAsArray ({
  159.         $r $this->Previous ();
  160.         if $r instanceof Exception )
  161.             $str $r->getTraceAsString ();
  162.         else
  163.             $str $this->getTraceAsString ();
  164.  
  165.         $buf preg_split ('/[\r\n]+/'$str);
  166.         $no count ($buf1;
  167.  
  168.         for $i=$no$j=0$i>-1$i--,$j++ {
  169.             $ret[$jpreg_replace ('/^#[0-9]+[\s]*/'''$buf[$i]);
  170.         }
  171.         return $ret;
  172.     }
  173.     // }}}
  174.  
  175.     // {{{ (string) myException::errStr ($errno)
  176.     /**
  177.      * PHP 에러 상수를 문자열로 반환
  178.      *
  179.      * @access public
  180.      * @return string 
  181.      * @param  int php error constants
  182.      */
  183.     static function errStr ($errno{
  184.         switch ($errno{
  185.             case :
  186.                 return 'E_ERROR';
  187.             case :
  188.                 return 'E_WARNING';
  189.             case :
  190.                 return 'E_PARSE';
  191.             case :
  192.                 return 'E_NOTICE';
  193.             case 16 :
  194.                 return 'E_CORE_ERROR';
  195.             case 32 :
  196.                 return 'E_CORE_WARNING';
  197.             case 64 :
  198.                 return 'E_COMPILE_ERROR';
  199.             case 128 :
  200.                 return 'E_COMPILE_WARNING';
  201.             case 256 :
  202.                 return 'E_USER_ERROR';
  203.             case 512 :
  204.                 return 'E_USER_WARNING';
  205.             case 1024 :
  206.                 return 'E_USER_NOTICE';
  207.             case 2048 :
  208.                 return 'E_STRICT';
  209.             case 4096 // since php 5.2
  210.                 return 'E_RECOVERABLE_ERROR';
  211.             case 8192 // since php 5.3
  212.                 return 'E_DEPRECATED';
  213.             case 16384 // since php 5.3
  214.                 return 'E_USER_DEPRECATED';
  215.             case 32767 :
  216.                 return 'E_ALL';
  217.             default :
  218.                 return 'Unknown Error';
  219.         }
  220.     }
  221.     // }}}
  222.  
  223.     // {{{ (void) myException::finalize (void)
  224.     /**
  225.      * E_ERROR 또는 E_USER_ERROR 시에 php를 중지 시킨다.
  226.      *
  227.      * @access public
  228.      * @return void 
  229.      */
  230.     function finalize ({
  231.         $r $this->Previous ();
  232.         if $r instanceof Exception )
  233.             $e &$r;
  234.         else
  235.             $e &$this;
  236.  
  237.         if $e->getCode (=== E_ERROR || $e->getCode (=== E_USER_ERROR )
  238.             exit (1);
  239.     }
  240.     // }}}
  241.  
  242.     // {{{ (void) myErrorHandler ($errno, $errstr, $errfile, $errline)
  243.     /**
  244.      * myException을 사용하기 위한 error handler.
  245.      *
  246.      * 이 method는 static으로 선언이 되어 있으므로,
  247.      * myException::myErrorHandler() 과 같이 호출해야 한다.
  248.      *
  249.      * @access public
  250.      * @return boolean 
  251.      * @param  int    에러 코드
  252.      * @param  string 에러 메시지
  253.      * @param  string 에러가 발생한 파일 경로
  254.      * @param  int    에러가 발생한 라인
  255.      */
  256.     static function myErrorHandler ($errno$errstr$errfile$errline{
  257.         if (error_reporting ($errno) )
  258.             return;
  259.  
  260.         switch ($errno {
  261.             case E_NOTICE :
  262.             case E_USER_NOTICE :
  263.             case E_STRICT :
  264.             case E_DEPRECATED :
  265.             case E_USER_DEPRECATED :
  266.                 break;
  267.             default :
  268.                 throw new myException ($errstr$errno);
  269.         }
  270.  
  271.         return true;
  272.     }
  273.     // }}}
  274.  
  275.     // {{{ (void) myShutdownHandler ($func = false) {
  276.     /**
  277.      * Fatal error 처리를 위한 shutdown handler
  278.      *
  279.      * 이 method는 static으로 선언이 되어 있으므로,
  280.      * myException::myShutdownHandler() 와 같이 호출해야 한다.
  281.      *
  282.      * @since 1.0.1
  283.      * @access public
  284.      * @return void 
  285.      * @param  string (optional) callback function
  286.      */
  287.     static function myShutdownHandler ($func false{
  288.         $error = (object) error_get_last ();
  289.  
  290.         if $error && ($error->type E_MY_FATAL) ) {
  291.             $error->type self::errStr ($error->type);
  292.  
  293.             if $func {
  294.                 printf ('%s: %s' PHP_EOL$error->type$error->message);
  295.                 echo '(' PHP_EOL;
  296.                 printf ('    [file] => %s' PHP_EOL$error->file);
  297.                 printf ('    [line] => %s' PHP_EOL$error->line);
  298.                 echo ')' PHP_EOL;
  299.                 #print_r ($error);
  300.             else {
  301.                 call_user_func ($func$error);
  302.             }
  303.         }
  304.     }
  305.     // }}}
  306. }
  307.  
  308. /*
  309.  * Local variables:
  310.  * tab-width: 4
  311.  * c-basic-offset: 4
  312.  * End:
  313.  * vim: set filetype=php noet sw=4 ts=4 fdm=marker:
  314.  * vim600: noet sw=4 ts=4 fdm=marker
  315.  * vim<600: noet sw=4 ts=4
  316.  */
  317. ?>

Documentation generated on Tue, 14 May 2019 02:00:36 +0900 by phpDocumentor 1.4.4