Source for file Ethna_Error.php

Documentation is available at Ethna_Error.php

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

Documentation generated on Fri, 11 Nov 2011 03:59:42 +0900 by phpDocumentor 1.4.3