Source for file Error.php

Documentation is available at Error.php

  1. <?php
  2. // vim: foldmethod=marker
  3. /**
  4.  *  Error.php
  5.  *
  6.  *  @author     Masaki Fujimoto <fujimoto@php.net>
  7.  *  @license    http://www.opensource.org/licenses/bsd-license.php The BSD License
  8.  *  @package    Ethna
  9.  *  @version    $Id: 90163f82f7d1e6b9cd7ff17912f64935b341d5f0 $
  10.  */
  11.  
  12. // {{{ ethna_error_handler
  13. /**
  14.  *  エラーコールバック関数
  15.  *
  16.  *  @param  int     $errno      エラーレベル
  17.  *  @param  string  $errstr     エラーメッセージ
  18.  *  @param  string  $errfile    エラー発生箇所のファイル名
  19.  *  @param  string  $errline    エラー発生箇所の行番号
  20.  */
  21. function ethna_error_handler($errno$errstr$errfile$errline)
  22. {
  23.     if (($errno error_reporting()) === 0{
  24.         return;
  25.     }
  26.  
  27.     list($level$nameEthna_Logger::errorLevelToLogLevel($errno);
  28.     switch ($errno{
  29.     case E_ERROR:
  30.     case E_CORE_ERROR:
  31.     case E_COMPILE_ERROR:
  32.     case E_USER_ERROR:
  33.         $php_errno 'Fatal error'break;
  34.     case E_WARNING:
  35.     case E_CORE_WARNING:
  36.     case E_COMPILE_WARNING:
  37.     case E_USER_WARNING:
  38.         $php_errno 'Warning'break;
  39.     case E_PARSE:
  40.         $php_errno 'Parse error'break;
  41.     case E_NOTICE:
  42.     case E_USER_NOTICE:
  43.     case E_STRICT:
  44.         $php_errno 'Notice'break;
  45.     case E_USER_DEPRECATED:
  46.     case E_DEPRECATED:
  47.         $php_errno 'Deprecated'break;
  48.     case E_RECOVERABLE_ERROR:
  49.         $php_errno 'Recoverable error'break;
  50.         break;
  51.     default:
  52.         $php_errno 'Unknown error'break;
  53.     }
  54.     $php_errstr sprintf('PHP %s: %s in %s on line %d',
  55.                           $php_errno$errstr$errfile$errline);
  56.  
  57.     // error_log()
  58.     if (ini_get('log_errors')) {
  59.         $locale setlocale(LC_TIME0);
  60.         setlocale(LC_TIME'C');
  61.         error_log($php_errstr0);
  62.         setlocale(LC_TIME$locale);
  63.     }
  64.  
  65.     // $logger->log()
  66.     $c Ethna_Controller::getInstance();
  67.     if ($c !== null{
  68.         $logger $c->getLogger();
  69.         $logger->log($levelsprintf("[PHP] %s: %s in %s on line %d",
  70.                                      $name$errstr$errfile$errline));
  71.     }
  72.  
  73.     // ignore these errors because so many errors occurs in external libraries (like PEAR)
  74.     if ($errno === E_STRICT{
  75.         return true;
  76.     }
  77.     if ($errno === E_RECOVERABLE_ERROR{
  78.         return true;
  79.     }
  80.  
  81.     // printf()
  82.     if (ini_get('display_errors')) {
  83.         $is_debug true;
  84.         $has_echo false;
  85.         if ($c !== null{
  86.             $config $c->getConfig();
  87.             $is_debug $config->get('debug');
  88.             $facility $logger->getLogFacility();
  89.             $has_echo is_array($facility)
  90.                         ? in_array('echo'$facility$facility === 'echo';
  91.         }
  92.         if ($is_debug == true && $has_echo === false
  93.             && $errno !== E_DEPRECATED{
  94.             return false;
  95.         }
  96.     }
  97. }
  98. set_error_handler('ethna_error_handler');
  99. // }}}
  100.  
  101. // {{{ ethna_exception_handler
  102.     //  TODO: Implement ethna_exception_handler function.
  103. // }}}
  104.  
  105. // {{{ Ethna_Error
  106. /**
  107.  *  エラークラス
  108.  *
  109.  *  @author     Masaki Fujimoto <fujimoto@php.net>
  110.  *  @access     public
  111.  *  @package    Ethna
  112.  */
  113. {
  114.     /**#@+
  115.      *  @access private
  116.      */
  117.  
  118.     /** @protected    object  Ethna_I18N  i18nオブジェクト */
  119.     protected $i18n;
  120.  
  121.     /** @protected    object  Ethna_Logger    loggerオブジェクト */
  122.     protected $logger;
  123.  
  124.     /** @protected    string  エラーメッセージ */
  125.     protected $message;
  126.  
  127.     /** @protected    integer エラーコード */
  128.     protected $code;
  129.  
  130.     /** @protected    integer エラーモード */
  131.     protected $mode;
  132.  
  133.     /** @protected    array   エラーモード依存のオプション */
  134.     protected $options;
  135.  
  136.     /** @protected    string  ユーザー定義もしくはデバッグ関連の追加情報を記した文字列。 */
  137.     protected $userinfo;
  138.  
  139.     /**#@-*/
  140.  
  141.     /**
  142.      *  Ethna_Errorクラスのコンストラクタ
  143.      *  $userinfo は第5引数に設定すること。
  144.      *
  145.      *  @access public
  146.      *  @param  string  $message            エラーメッセージ
  147.      *  @param  int     $code               エラーコード
  148.      *  @param  int     $mode               エラーモード(Ethna_Errorはコールバックを
  149.      *                                       常に使用するので実質無視される)
  150.      *  @param  array   $options            エラーモード依存のオプション
  151.      *  @param  array   $userinfo           エラー追加情報($options より後の全ての引数)
  152.      *  @see http://pear.php.net/manual/ja/core.pear.pear-error.pear-error.php
  153.      */
  154.     public function __construct($message null$code null$mode null$options null)
  155.     {
  156.         $controller Ethna_Controller::getInstance();
  157.         if ($controller !== null{
  158.             $this->i18n = $controller->getI18N();
  159.         }
  160.  
  161.         // $options 以降の引数 -> $userinfo
  162.         if (func_num_args(4{
  163.             $userinfo array_slice(func_get_args()4);
  164.             if (count($userinfo== 1{
  165.                 if (is_array($userinfo[0])) {
  166.                     $this->userinfo = $userinfo[0];
  167.                 else if (is_null($userinfo[0])) {
  168.                     $this->userinfo = array();
  169.                 }
  170.             else {
  171.                 $this->userinfo = $userinfo[0];
  172.             }
  173.         else {
  174.             $this->userinfo = array();
  175.         }
  176.  
  177.         // メッセージ補正処理 ($message)
  178.         if (is_null($message)) {
  179.             // $codeからメッセージを取得する
  180.             $message $controller->getErrorMessage($code);
  181.             if (is_null($message)) {
  182.                 $message 'unknown error';
  183.             }
  184.         }
  185.         $this->message = $message;
  186.  
  187.         //  その他メンバ変数設定
  188.         $this->code = $code;
  189.         $this->mode = $mode;
  190.         $this->options = $options;
  191.         $this->level ($this->options === NULLE_USER_NOTICE $options;
  192.  
  193.         //  Ethnaフレームワークのエラーハンドラ(callback)
  194.         Ethna::handleError($this);
  195.     }
  196.  
  197.     /**
  198.      * エラーオブジェクトに関連付けられたエラーコードを返します。
  199.      *
  200.      * @return integer - エラー番号
  201.      */
  202.     public function getCode()
  203.     {
  204.         return $this->code;
  205.     }
  206.  
  207.     /**
  208.      *  levelへのアクセサ(R)
  209.      *
  210.      *  @access public
  211.      *  @return int     エラーレベル
  212.      */
  213.     public function getLevel()
  214.     {
  215.         return $this->level;
  216.     }
  217.  
  218.     /**
  219.      *  messageへのアクセサ(R)
  220.      *
  221.      *  以下の処理を行う
  222.      *  - エラーメッセージのi18n処理
  223.      *  - $userinfoとして渡されたデータによるvsprintf()処理
  224.      *
  225.      *  @access public
  226.      *  @return string  エラーメッセージ
  227.      */
  228.     public function getMessage()
  229.     {
  230.         $tmp_message $this->i18n ? $this->i18n->get($this->message$this->message;
  231.         $tmp_userinfo to_array($this->userinfo);
  232.         $tmp_message_arg_list array();
  233.         for ($i 0$i count($tmp_userinfo)$i++{
  234.             $tmp_message_arg_list[$this->i18n ? $this->i18n->get($tmp_userinfo[$i]$tmp_userinfo[$i];
  235.         }
  236.         return vsprintf($tmp_message$tmp_message_arg_list);
  237.     }
  238.  
  239.     /**
  240.      *  エラー追加情報へのアクセサ(R)
  241.      *
  242.      *  エラー追加情報配列の個々のエントリへのアクセスをサポート
  243.      *
  244.      *  @access public
  245.      *  @param  int     $n      エラー追加情報のインデックス(省略可)
  246.      *  @return mixed   message引数
  247.      */
  248.     public function getUserInfo($n null)
  249.     {
  250.         if (is_null($n)) {
  251.             return $this->userinfo;
  252.         }
  253.  
  254.         if (isset($this->userinfo[$n])) {
  255.             return $this->userinfo[$n];
  256.         else {
  257.             return null;
  258.         }
  259.     }
  260.  
  261.     /**
  262.      *  エラー追加情報へのアクセサ(W)
  263.      *
  264.      *  @access public
  265.      *  @param  string  $info   追加するエラー情報
  266.      */
  267.     public function addUserInfo($info)
  268.     {
  269.         $this->userinfo[$info;
  270.     }
  271. }
  272. // }}}

Documentation generated on Fri, 11 Nov 2011 03:58:02 +0900 by phpDocumentor 1.4.3