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: Ethna_Error.php 478 2007-07-20 18:08:22Z mumumu-org $
  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 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.     default:
  46.         $php_errno 'Unknown error'break;
  47.     }
  48.     $php_errstr sprintf('PHP %s: %s in %s on line %d',
  49.                           $php_errno$errstr$errfile$errline);
  50.  
  51.     // error_log()
  52.     if (ini_get('log_errors')) {
  53.         $locale setlocale(LC_TIME0);
  54.         setlocale(LC_TIME'C');
  55.         error_log($php_errstr0);
  56.         setlocale(LC_TIME$locale);
  57.     }
  58.  
  59.     // $logger->log()
  60.     $c =Ethna_Controller::getInstance();
  61.     if ($c !== null{
  62.         $logger =$c->getLogger();
  63.         $logger->log($levelsprintf("[PHP] %s: %s in %s on line %d",
  64.                                      $name$errstr$errfile$errline));
  65.     }
  66.  
  67.     // printf()
  68.     if (ini_get('display_errors')) {
  69.         $is_debug true;
  70.         $has_echo false;
  71.         if ($c !== null{
  72.             $config =$c->getConfig();
  73.             $is_debug $config->get('debug');
  74.             $facility $logger->getLogFacility();
  75.             $has_echo is_array($facility)
  76.                         ? in_array('echo'$facility$facility === 'echo';
  77.         }
  78.         if ($is_debug == true && $has_echo === false{
  79.             if ($c !== null && $c->getGateway(=== GATEWAY_WWW{
  80.                 $format "<b>%s</b>: %s in <b>%s</b> on line <b>%d</b><br />\n";
  81.             else {
  82.                 $format "%s: %s in %s on line %d\n";
  83.             }
  84.             printf($format$php_errno$errstr$errfile$errline);
  85.         }
  86.     }
  87. }
  88. set_error_handler('ethna_error_handler');
  89. // }}}
  90.  
  91. // {{{ Ethna_Error
  92. /**
  93.  *  エラークラス
  94.  *
  95.  *  @author     Masaki Fujimoto <fujimoto@php.net>
  96.  *  @access     public
  97.  *  @package    Ethna
  98.  */
  99. class Ethna_Error extends PEAR_Error
  100. {
  101.     /**#@+
  102.      *  @access private
  103.      */
  104.  
  105.     /** @var    object  Ethna_I18N  i18nオブジェクト */
  106.     var $i18n;
  107.  
  108.     /** @var    object  Ethna_Logger    loggerオブジェクト */
  109.     var $logger;
  110.  
  111.     /**#@-*/
  112.  
  113.     /**
  114.      *  Ethna_Errorクラスのコンストラクタ
  115.      *
  116.      *  @access public
  117.      *  @param  int     $level              エラーレベル
  118.      *  @param  string  $message            エラーメッセージ
  119.      *  @param  int     $code               エラーコード
  120.      *  @param  array   $userinfo           エラー追加情報(エラーコード以降の全ての引数)
  121.      */
  122.     function Ethna_Error($message null$code null$mode null$options null)
  123.     {
  124.         $controller =Ethna_Controller::getInstance();
  125.         if ($controller !== null{
  126.             $this->i18n =$controller->getI18N();
  127.         }
  128.  
  129.         // $options以降の引数->$userinfo
  130.         if (func_num_args(4{
  131.             $userinfo array_slice(func_get_args()4);
  132.             if (count($userinfo== 1{
  133.                 if (is_array($userinfo[0])) {
  134.                     $userinfo $userinfo[0];
  135.                 else if (is_null($userinfo[0])) {
  136.                     $userinfo array();
  137.                 }
  138.             }
  139.         else {
  140.             $userinfo array();
  141.         }
  142.  
  143.         // メッセージ補正処理
  144.         if (is_null($message)) {
  145.             // $codeからメッセージを取得する
  146.             $message $controller->getErrorMessage($code);
  147.             if (is_null($message)) {
  148.                 $message 'unknown error';
  149.             }
  150.         }
  151.  
  152.         parent::PEAR_Error($message$code$mode$options$userinfo);
  153.  
  154.         // Ethnaフレームワークのエラーハンドラ(PEAR_Errorのコールバックとは異なる)
  155.         Ethna::handleError($this);
  156.     }
  157.  
  158.     /**
  159.      *  levelへのアクセサ(R)
  160.      *
  161.      *  @access public
  162.      *  @return int     エラーレベル
  163.      */
  164.     function getLevel()
  165.     {
  166.         return $this->level;
  167.     }
  168.  
  169.     /**
  170.      *  messageへのアクセサ(R)
  171.      *
  172.      *  PEAR_Error::getMessage()をオーバーライドして以下の処理を行う
  173.      *  - エラーメッセージのi18n処理
  174.      *  - $userinfoとして渡されたデータによるvsprintf()処理
  175.      *
  176.      *  @access public
  177.      *  @return string  エラーメッセージ
  178.      */
  179.     function getMessage()
  180.     {
  181.         $tmp_message $this->i18n $this->i18n->get($this->message$this->message;
  182.         $tmp_userinfo to_array($this->userinfo);
  183.         $tmp_message_arg_list array();
  184.         for ($i 0$i count($tmp_userinfo)$i++{
  185.             $tmp_message_arg_list[$this->i18n $this->i18n->get($tmp_userinfo[$i]$tmp_userinfo[$i];
  186.         }
  187.         return vsprintf($tmp_message$tmp_message_arg_list);
  188.     }
  189.  
  190.     /**
  191.      *  エラー追加情報へのアクセサ(R)
  192.      *
  193.      *  PEAR_Error::getUserInfo()をオーバーライドして、配列の個々の
  194.      *  エントリへのアクセスをサポート
  195.      *
  196.      *  @access public
  197.      *  @param  int     $n      エラー追加情報のインデックス(省略可)
  198.      *  @return mixed   message引数
  199.      */
  200.     function getUserInfo($n null)
  201.     {
  202.         if (is_null($n)) {
  203.             return $this->userinfo;
  204.         }
  205.  
  206.         if (isset($this->userinfo[$n])) {
  207.             return $this->userinfo[$n];
  208.         else {
  209.             return null;
  210.         }
  211.     }
  212.  
  213.     /**
  214.      *  エラー追加情報へのアクセサ(W)
  215.      *
  216.      *  PEAR_Error::addUserInfo()をオーバーライド
  217.      *
  218.      *  @access public
  219.      *  @param  string  $info   追加するエラー情報
  220.      */
  221.     function addUserInfo($info)
  222.     {
  223.         $this->userinfo[$info;
  224.     }
  225. }
  226. // }}}
  227. ?>

Documentation generated on Thu, 08 May 2008 00:14:50 +0900 by phpDocumentor 1.4.2