2 // vim: foldmethod=marker
6 * @author Masaki Fujimoto <fujimoto@php.net>
7 * @license http://www.opensource.org/licenses/bsd-license.php The BSD License
12 // {{{ ethna_error_handler
16 * @param int $errno エラーレベル
17 * @param string $errstr エラーメッセージ
18 * @param string $errfile エラー発生箇所のファイル名
19 * @param string $errline エラー発生箇所の行番号
21 function ethna_error_handler($errno, $errstr, $errfile, $errline)
23 if ($errno === E_STRICT || $errno === E_DEPRECATED
24 || ($errno & error_reporting()) === 0) {
28 list($level, $name) = Ethna_Logger::errorLevelToLogLevel($errno);
34 $php_errno = 'Fatal error'; break;
37 case E_COMPILE_WARNING:
39 $php_errno = 'Warning'; break;
41 $php_errno = 'Parse error'; break;
46 $php_errno = 'Notice'; break;
48 $php_errno = 'Unknown error'; break;
50 $php_errstr = sprintf('PHP %s: %s in %s on line %d',
51 $php_errno, $errstr, $errfile, $errline);
54 if (ini_get('log_errors')) {
55 $locale = setlocale(LC_TIME, 0);
56 setlocale(LC_TIME, 'C');
57 error_log($php_errstr, 0);
58 setlocale(LC_TIME, $locale);
62 $c = Ethna_Controller::getInstance();
64 $logger = $c->getLogger();
65 $logger->log($level, sprintf("[PHP] %s: %s in %s on line %d",
66 $name, $errstr, $errfile, $errline));
70 if (ini_get('display_errors')) {
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';
80 if ($is_debug == true && $has_echo === false) {
85 set_error_handler('ethna_error_handler');
88 // {{{ ethna_exception_handler
89 // TODO: Implement ethna_exception_handler function.
96 * @author Masaki Fujimoto <fujimoto@php.net>
106 /** @var object Ethna_I18N i18nオブジェクト */
109 /** @var object Ethna_Logger loggerオブジェクト */
112 /** @var string エラーメッセージ */
115 /** @var integer エラーコード */
118 /** @var integer エラーモード */
121 /** @var array エラーモード依存のオプション */
124 /** @var string ユーザー定義もしくはデバッグ関連の追加情報を記した文字列。 */
130 * Ethna_Errorクラスのコンストラクタ
131 * $userinfo は第5引数に設定すること。
134 * @param string $message エラーメッセージ
135 * @param int $code エラーコード
136 * @param int $mode エラーモード(Ethna_Errorはコールバックを
138 * @param array $options エラーモード依存のオプション
139 * @param array $userinfo エラー追加情報($options より後の全ての引数)
140 * @see http://pear.php.net/manual/ja/core.pear.pear-error.pear-error.php
142 public function __construct($message = null, $code = null, $mode = null, $options = null)
144 $controller = Ethna_Controller::getInstance();
145 if ($controller !== null) {
146 $this->i18n = $controller->getI18N();
149 // $options 以降の引数 -> $userinfo
150 if (func_num_args() > 4) {
151 $userinfo = array_slice(func_get_args(), 4);
152 if (count($userinfo) == 1) {
153 if (is_array($userinfo[0])) {
154 $this->userinfo = $userinfo[0];
155 } else if (is_null($userinfo[0])) {
156 $this->userinfo = array();
159 $this->userinfo = $userinfo[0];
162 $this->userinfo = array();
165 // メッセージ補正処理 ($message)
166 if (is_null($message)) {
168 $message = $controller->getErrorMessage($code);
169 if (is_null($message)) {
170 $message = 'unknown error';
173 $this->message = $message;
178 $this->options = $options;
179 $this->level = ($this->options === NULL) ? E_USER_NOTICE : $options;
181 // Ethnaフレームワークのエラーハンドラ(callback)
182 Ethna::handleError($this);
186 * エラーオブジェクトに関連付けられたエラーコードを返します。
188 * @return integer - エラー番号
190 public function getCode()
201 public function getLevel()
211 * - $userinfoとして渡されたデータによるvsprintf()処理
214 * @return string エラーメッセージ
216 public function getMessage()
218 $tmp_message = $this->i18n ? $this->i18n->get($this->message) : $this->message;
219 $tmp_userinfo = to_array($this->userinfo);
220 $tmp_message_arg_list = array();
221 for ($i = 0; $i < count($tmp_userinfo); $i++) {
222 $tmp_message_arg_list[] = $this->i18n ? $this->i18n->get($tmp_userinfo[$i]) : $tmp_userinfo[$i];
224 return vsprintf($tmp_message, $tmp_message_arg_list);
230 * エラー追加情報配列の個々のエントリへのアクセスをサポート
233 * @param int $n エラー追加情報のインデックス(省略可)
234 * @return mixed message引数
236 public function getUserInfo($n = null)
239 return $this->userinfo;
242 if (isset($this->userinfo[$n])) {
243 return $this->userinfo[$n];
253 * @param string $info 追加するエラー情報
255 public function addUserInfo($info)
257 $this->userinfo[] = $info;