Selaa lähdekoodia

Fxied error page

Zhu Jinhui 7 vuotta sitten
vanhempi
commit
527cfe4076
3 muutettua tiedostoa jossa 298 lisäystä ja 304 poistoa
  1. 1 1
      Qii/Autoloader/Psr4.php
  2. 134 133
      Qii/Exceptions/Error.php
  3. 163 170
      Qii/Exceptions/Errors.php

+ 1 - 1
Qii/Autoloader/Psr4.php

@@ -203,7 +203,7 @@ class Psr4
     /**
      * 从Map中获取文件
      */
-    protected function searchMappedFile($class)
+    public function searchMappedFile($class)
     {
         $prefix = $class;
         // work backwards through the namespace names of the fully-qualified

+ 134 - 133
Qii/Exceptions/Error.php

@@ -1,4 +1,5 @@
 <?php
+
 namespace Qii\Exceptions;
 
 /**
@@ -8,139 +9,139 @@ namespace Qii\Exceptions;
  */
 class Error
 {
-	const VERSION = '1.2';
-
-	public function __construct()
-	{
-
-	}
-
-	public static function index()
-	{
-		$args = func_get_args();
-		echo \Qii::i('1108', $args[0], $args[1]);
-	}
-
-	/**
-	 * 返回程序的调用栈
-	 */
-	static public function getTrace()
-	{
-		return debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
-	}
-
-	/**
-	 * 程序的调用栈生成字符串
-	 */
-	static public function getTraceAsString()
-	{
-		$e = new \Exception();
-		$trace = explode("\n", $e->getTraceAsString());
-		$trace = array_reverse($trace);
-		array_shift($trace);
-		array_pop($trace);
-		$length = count($trace);
-		$result = array();
-
-		for ($i = 0; $i < $length; $i++) {
-			$result[] = ($i + 1) . ')' . substr($trace[$i], strpos($trace[$i], ' ')); // replace '#someNum' with '$i)', set the right ordering
-		}
-
-		return "\t" . implode("\n\t", $result);
-	}
-
-	/**
-	 * 错误描述
-	 * @return string
-	 */
-	static public function getMessage()
-	{
-		$e = new \Exception();
-		return $e->getMessage();
-	}
-
-	/**
-	 * 错误码
-	 * @return int
-	 */
-	static public function getCode()
-	{
-		return __LINE__;
-	}
-
-	/**
-	 * 错误设置,如果满足给定的条件就直接返回false,否则在设置了错误页面的情况下返回true
-	 * 如果出错后要终止的话,需要自行处理,此方法不错停止不执行
-	 *
-	 * @param bool $condition
-	 * @param int $line 出错的行数,这样以便轻松定位错误
-	 * @param string $msg
-	 * @param int|string $code
-	 * @param string $args ...
-	 * @return bool
-	 */
-	public static function setError($condition, $line = 0, $code, $args = null, $msg = null)
-	{
-		if ($condition) {
-			return false;
-		}
-		$appConfigure = \Qii::appConfigure();
-		//如果是调试模式就直接抛出异常
-		$isDebug = $appConfigure['debug'];
-		$message = array();
-		$message[] = explode("\n", self::getTraceAsString());
-		if (\Qii::getInstance()->logerWriter != null) {
-			$message[] = 'Referer:' . (isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : \Qii::getInstance()->request->url->getCurrentURL());
-			\Qii::getInstance()->logerWriter->writeLog($message);
-		}
-		if ($isDebug) {
-			$args = array_slice(func_get_args(), 2);
-			throw new \Exception(call_user_func_array(array('\Qii', 'i'), $args), $line);
-		}
-		$errorPage = $appConfigure['errorPage'];
-		$env = \Qii\Config\Register::get(\Qii\Config\Consts::APP_ENVIRON, 'dev');
-		if ($env != 'product' && $errorPage != null) {
-			list($controller, $action) = explode(':', $appConfigure['errorPage']);
-			$controllerCls = \Qii\Config\Register::get(\Qii\Config\Consts::APP_DEFAULT_CONTROLLER_PREFIX) . '_' . $controller;
-			$action = preg_replace('/(Action)$/i', "", $action);
-			$filePath = \Qii\Autoloader\Import::requireByClass($controllerCls);
-			if (!is_file($filePath)) {
-				if ($env == 'product') return '';
-				\Qii\Autoloader\Import::requires(Qii_DIR . DS . 'Exceptions' . DS . 'Error.php');
-				call_user_func_array(array('\Qii\Exceptions\Error', 'index'), array($controller, $action));
-				die();
-			} else {
-				\Qii::getInstance()->request->setControllerName($controller);
+    const VERSION = '1.2';
+    
+    public function __construct()
+    {
+    
+    }
+    
+    public static function index()
+    {
+        $args = func_get_args();
+        echo \Qii::i('1108', $args[0], $args[1]);
+    }
+    
+    /**
+     * 返回程序的调用栈
+     */
+    static public function getTrace()
+    {
+        return debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
+    }
+    
+    /**
+     * 程序的调用栈生成字符串
+     */
+    static public function getTraceAsString()
+    {
+        $e = new \Exception();
+        $trace = explode("\n", $e->getTraceAsString());
+        $trace = array_reverse($trace);
+        array_shift($trace);
+        array_pop($trace);
+        $length = count($trace);
+        $result = array();
+        
+        for ($i = 0; $i < $length; $i++) {
+            $result[] = ($i + 1) . ')' . substr($trace[$i], strpos($trace[$i], ' ')); // replace '#someNum' with '$i)', set the right ordering
+        }
+        
+        return "\t" . implode("\n\t", $result);
+    }
+    
+    /**
+     * 错误描述
+     * @return string
+     */
+    static public function getMessage()
+    {
+        $e = new \Exception();
+        return $e->getMessage();
+    }
+    
+    /**
+     * 错误码
+     * @return int
+     */
+    static public function getCode()
+    {
+        return __LINE__;
+    }
+    
+    /**
+     * 错误设置,如果满足给定的条件就直接返回false,否则在设置了错误页面的情况下返回true
+     * 如果出错后要终止的话,需要自行处理,此方法不错停止不执行
+     *
+     * @param bool $condition
+     * @param int $line 出错的行数,这样以便轻松定位错误
+     * @param string $msg
+     * @param int|string $code
+     * @param string $args ...
+     * @return bool
+     */
+    public static function setError($condition, $line = 0, $code, $args = null, $msg = null)
+    {
+        if ($condition) {
+            return false;
+        }
+        $appConfigure = \Qii::appConfigure();
+        //如果是调试模式就直接抛出异常
+        $isDebug = $appConfigure['debug'];
+        $message = array();
+        $message[] = explode("\n", self::getTraceAsString());
+        if (\Qii::getInstance()->logerWriter != null) {
+            $message[] = 'Referer:' . (isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : \Qii::getInstance()->request->url->getCurrentURL());
+            \Qii::getInstance()->logerWriter->writeLog($message);
+        }
+        if ($isDebug) {
+            $args = array_slice(func_get_args(), 2);
+            throw new \Exception(call_user_func_array(array('\Qii', 'i'), $args), $line);
+        }
+        $errorPage = $appConfigure['errorPage'];
+        $env = \Qii\Config\Register::get(\Qii\Config\Consts::APP_ENVIRON, 'dev');
+        if ($env != 'product' && $errorPage != null) {
+            list($controller, $action) = explode(':', $appConfigure['errorPage']);
+            $controllerCls = \Qii\Config\Register::get(\Qii\Config\Consts::APP_DEFAULT_CONTROLLER_PREFIX) . '\\' . $controller;
+            $action = preg_replace('/(Action)$/i', "", $action);
+            $filePath = \Qii\Autoloader\Psr4::getInstance()->searchMappedFile($controllerCls);
+            if (!is_file($filePath)) {
+                if ($env == 'product') return '';
+                \Qii\Autoloader\Import::requires(Qii_DIR . DS . 'Exceptions' . DS . 'Error.php');
+                call_user_func_array(array('\Qii\Exceptions\Error', 'index'), array($controller, $action));
+                die();
+            } else {
+                \Qii::getInstance()->request->setControllerName($controller);
                 \Qii::getInstance()->request->setActionName($action);
                 \Qii::getInstance()->dispatcher->setRequest(\Qii::getInstance()->request);
-				\Qii::getInstance()->dispatcher->dispatch($controller, $action, new \Exception($msg ? $msg : self::getTraceAsString(), $code));
-				die();
-			}
-			return;
-		}
-		return true;
-	}
-
-	/**
-	 * 显示错误信息
-	 * @param Array $message
-	 */
-	public static function showError($message)
-	{
-		include(join(DS, array(Qii_DIR ,'Exceptions', 'view', 'error.php')));
-	}
-
-	/**
-	 * 显示错误信息
-	 * @param Array $message
-	 */
-	public static function showMessage($message)
-	{
-		include(join(DS, array(Qii_DIR, 'Exceptions', 'view', 'message.php')));
-	}
-
-	public function __call($method, $args)
-	{
-		if (method_exists(self, $method)) return call_user_func_array(array('Qii\Exceptions\Error', $method), $args);
-	}
+                \Qii::getInstance()->dispatcher->dispatch($controller, $action, new \Exception($msg ? $msg : self::getTraceAsString(), $code));
+                die();
+            }
+            return;
+        }
+        return true;
+    }
+    
+    /**
+     * 显示错误信息
+     * @param Array $message
+     */
+    public static function showError($message)
+    {
+        include(join(DS, array(Qii_DIR, 'Exceptions', 'view', 'error.php')));
+    }
+    
+    /**
+     * 显示错误信息
+     * @param Array $message
+     */
+    public static function showMessage($message)
+    {
+        include(join(DS, array(Qii_DIR, 'Exceptions', 'view', 'message.php')));
+    }
+    
+    public function __call($method, $args)
+    {
+        if (method_exists(self, $method)) return call_user_func_array(array('Qii\Exceptions\Error', $method), $args);
+    }
 }

+ 163 - 170
Qii/Exceptions/Errors.php

@@ -1,176 +1,169 @@
 <?php
+
 namespace Qii\Exceptions;
 
 class Errors extends \Exception
 {
-	const VERSION = '1.2';
-
-	/**
-	 * 获取两个文件的相对路径
-	 * @param String $cur
-	 * @param String $absp
-	 * @return String
-	 */
-	public static function getRelatePath($cur, $absp)
-	{
-		$cur = str_replace('\\', '/', $cur);
-		$absp = str_replace('\\', '/', $absp);
-		$sabsp = explode('/', $absp);
-		$scur = explode('/', $cur);
-		$la = count($sabsp) - 1;
-		$lb = count($scur) - 1;
-		$l = max($la, $lb);
-
-		for ($i = 0; $i <= $l; $i++) {
-			if ($sabsp[$i] != $scur[$i])
-				break;
-		}
-		$k = $i - 1;
-		$path = "";
-		for ($i = 1; $i <= ($lb - $k - 1); $i++)
-			$path .= "../";
-		for ($i = $k + 1; $i <= ($la - 1); $i++)
-			$path .= $sabsp[$i] . "/";
-		$path .= $sabsp[$la];
-		return $path;
-	}
-
-	/**
-	 * 显示错误
-	 *
-	 * @param Object $e Exception
-	 */
-	public static function getError($e)
-	{
-		$message = array();
-		if (isset($_GET['isAjax']) && $_GET['isAjax'] == 1) {
-			$code = $e->getCode();
-			if($code == 0) $code = 1;
-			echo json_encode(array('code' => $code, 'line' => $e->getFile() . ' line :' . $e->getLine(), 'msg' => strip_tags($e->getMessage())), JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_AMP | JSON_UNESCAPED_UNICODE);
-			return;
-		}
-		$message[] = (IS_CLI ? QII_EOL : '') . \Qii::i('Error file', self::getRelatePath($_SERVER['SCRIPT_FILENAME'], $e->getFile())) . (IS_CLI ? QII_EOL : '');
-		$message[] = \Qii::i('Error code', $e->getCode()) . (IS_CLI ? QII_EOL : '');
-		$message[] = \Qii::i('Error description', $e->getMessage()) . (IS_CLI ? QII_EOL : '');
-		$message[] = \Qii::i('Error line', $e->getLine() . ' on ' . self::getLineMessage($e->getFile(), $e->getLine())) . (IS_CLI ? QII_EOL : '');
-		$traceString = \Qii::i('Trace as below') . QII_EOL;
-		$traces = explode("\n", $e->getTraceAsString());
-		foreach ($traces AS $trance) 
-		{
-			$traceString .= str_repeat(QII_SPACE, 4) . $trance . QII_EOL;
-		}
-		$message[] = $traceString;
-		if (\Qii::getInstance()->logerWriter != null) {
-			$message[] = 'Source URL:' . \Qii::getInstance()->request->url->getCurrentURL();
-			$message[] = 'Referer URL:' . (isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : \Qii::getInstance()->request->url->getCurrentURL());
-			\Qii::getInstance()->logerWriter->writeLog($message);
-		}
-		$appConfigure = \Qii::appConfigure();
-
-		$env = \Qii\Config\Register::get(\Qii\Config\Consts::APP_ENVIRON, 'dev');
-		if ($env == 'product' || ($appConfigure['errorPage'] && (isset($appConfigure['debug']) && $appConfigure['debug'] == 0))) {
-			list($controller, $action) = explode(':', $appConfigure['errorPage']);
-			$controllerCls = \Qii\Config\Register::get(\Qii\Config\Consts::APP_DEFAULT_CONTROLLER_PREFIX) . '_' . $controller;
-			$action = preg_replace('/(Action)$/i', "", $action);
-			$filePath = \Qii\Autoloader\Import::requireByClass($controllerCls);
-			if (!is_file($filePath)) {
-				if ($env == 'product') return '';
-				\Qii\Autoloader\Import::requires(Qii_DIR . DS . 'Exceptions' . DS . 'Error.php');
-				call_user_func_array(array('\Qii\Exceptions\Error', 'index'), array($controller, $action));
-				die();
-			} else {
-				\Qii::getInstance()->request->setControllerName($controller);
-				\Qii::getInstance()->request->setActionName($action);
-				\Qii::getInstance()->dispatcher->setRequest(\Qii::getInstance()->request);
-				\Qii::getInstance()->dispatcher->dispatch($controller, $action, $e);
-				die();
-			}
-		}
-		ob_start();
-		include(join(DS, array(Qii_DIR, 'Exceptions', 'View', 'error.php')));
-		$html = ob_get_contents();
-		ob_clean();
-		
-		if(!IS_CLI)
-		{
-			echo $html;
-			return;
-		}
-		return (new \Qii\Response\Cli())->stdout(
-			str_replace("&nbsp;"
-						, " "
-						, strip_tags(join(PHP_EOL, preg_replace("/[\n|\r\n]/", PHP_EOL, $message)))
-			)
-		);
-	}
-
-	/**
-	 * 获取指定文件的指定行内容
-	 *
-	 * @param String $fileName 文件名
-	 * @param Int $line 行号
-	 * @return String
-	 */
-	public static function getLineMessage($fileName, $line)
-	{
-		$seekline = max(0, $line - 1);
-		$spl = new \SplFileObject($fileName);
-		$code = array();
-		if ($line > 1) {
-			$maxLine = 10;
-			$firstLine = max(0, $seekline - $maxLine);
-
-			$spl->seek($firstLine);
-			$min = $seekline - $maxLine;
-			$max = $seekline + $maxLine + 1;
-
-			for ($i = $min; $i < $max; $i++) {
-				$currentLine = $i + ($min < 0 ? abs($min) : 0) + 1;
-				$color = $currentLine == $line ? ' color="red"' : '';
-				if ($spl->eof()) break;
-				if(IS_CLI)
-				{
-					$code[] = $currentLine . ($color != '' ? ' 行发生错误' : '') . rtrim($spl->current());
-				}
-				else
-				{
-					$code[] = '<font ' . $color . '>' . $currentLine . ':</font>' . "\t" . '<font ' . $color . '>' . htmlspecialchars(rtrim($spl->current())) . '</font>';
-				}
-				$spl->next();
-			}
-		} else {
-			$spl->seek($seekline);
-			if(IS_CLI)
-			{
-				$code[] = rtrim($spl->current());
-			}
-			else
-			{
-				$code[] = htmlspecialchars(rtrim($spl->current()));
-			}
-			
-		}
-		return IS_CLI ? PHP_EOL . join(PHP_EOL, $code) : '<pre style="font-weight:bold;">' . join("<br />", $code) . '</pre>';
-	}
-
-	/**
-	 * sprintf 格式化语言错误信息内容
-	 *
-	 *
-	 * Qii::e($message, $argv1, $argv2, ..., $line);
-	 * $message = sprintf($message, $argv1, $argv2, ...);
-	 * throw new \Qii_Exceptions_Errors($message, $line);
-	 */
-	public static function e()
-	{
-		$argvs = func_get_args();
-		$count = count($argvs);
-		$message = array_shift($argvs);
-		$line = (int) array_pop($argvs);
-		if ($count == 2) {
-			throw new \Qii\Exceptions\Errors($message, $line);
-		}
-		$message = vsprintf($message, $argvs);
-		throw new \Qii\Exceptions\Errors($message, $line);
-	}
+    const VERSION = '1.2';
+    
+    /**
+     * 获取两个文件的相对路径
+     * @param String $cur
+     * @param String $absp
+     * @return String
+     */
+    public static function getRelatePath($cur, $absp)
+    {
+        $cur = str_replace('\\', '/', $cur);
+        $absp = str_replace('\\', '/', $absp);
+        $sabsp = explode('/', $absp);
+        $scur = explode('/', $cur);
+        $la = count($sabsp) - 1;
+        $lb = count($scur) - 1;
+        $l = max($la, $lb);
+        
+        for ($i = 0; $i <= $l; $i++) {
+            if ($sabsp[$i] != $scur[$i])
+                break;
+        }
+        $k = $i - 1;
+        $path = "";
+        for ($i = 1; $i <= ($lb - $k - 1); $i++)
+            $path .= "../";
+        for ($i = $k + 1; $i <= ($la - 1); $i++)
+            $path .= $sabsp[$i] . "/";
+        $path .= $sabsp[$la];
+        return $path;
+    }
+    
+    /**
+     * 显示错误
+     *
+     * @param Object $e Exception
+     */
+    public static function getError($e)
+    {
+        $message = array();
+        if (isset($_GET['isAjax']) && $_GET['isAjax'] == 1) {
+            $code = $e->getCode();
+            if ($code == 0) $code = 1;
+            echo json_encode(array('code' => $code, 'line' => $e->getFile() . ' line :' . $e->getLine(), 'msg' => strip_tags($e->getMessage())), JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_AMP | JSON_UNESCAPED_UNICODE);
+            return;
+        }
+        $message[] = (IS_CLI ? QII_EOL : '') . \Qii::i('Error file', self::getRelatePath($_SERVER['SCRIPT_FILENAME'], $e->getFile())) . (IS_CLI ? QII_EOL : '');
+        $message[] = \Qii::i('Error code', $e->getCode()) . (IS_CLI ? QII_EOL : '');
+        $message[] = \Qii::i('Error description', $e->getMessage()) . (IS_CLI ? QII_EOL : '');
+        $message[] = \Qii::i('Error line', $e->getLine() . ' on ' . self::getLineMessage($e->getFile(), $e->getLine())) . (IS_CLI ? QII_EOL : '');
+        $traceString = \Qii::i('Trace as below') . QII_EOL;
+        $traces = explode("\n", $e->getTraceAsString());
+        foreach ($traces AS $trance) {
+            $traceString .= str_repeat(QII_SPACE, 4) . $trance . QII_EOL;
+        }
+        $message[] = $traceString;
+        if (\Qii::getInstance()->logerWriter != null) {
+            $message[] = 'Source URL:' . \Qii::getInstance()->request->url->getCurrentURL();
+            $message[] = 'Referer URL:' . (isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : \Qii::getInstance()->request->url->getCurrentURL());
+            \Qii::getInstance()->logerWriter->writeLog($message);
+        }
+        $appConfigure = \Qii::appConfigure();
+        
+        $env = \Qii\Config\Register::get(\Qii\Config\Consts::APP_ENVIRON, 'dev');
+        if ($env == 'product' || ($appConfigure['errorPage'] && (isset($appConfigure['debug']) && $appConfigure['debug'] == 0))) {
+            list($controller, $action) = explode(':', $appConfigure['errorPage']);
+            $controllerCls = \Qii\Config\Register::get(\Qii\Config\Consts::APP_DEFAULT_CONTROLLER_PREFIX) . '\\' . $controller;
+            $action = preg_replace('/(Action)$/i', "", $action);
+            $filePath = \Qii\Autoloader\Psr4::getInstance()->searchMappedFile($controllerCls);
+            if (!is_file($filePath)) {
+                if ($env == 'product') return '';
+                \Qii\Autoloader\Import::requires(Qii_DIR . DS . 'Exceptions' . DS . 'Error.php');
+                call_user_func_array(array('\Qii\Exceptions\Error', 'index'), array($controller, $action));
+                die();
+            } else {
+                \Qii::getInstance()->request->setControllerName($controller);
+                \Qii::getInstance()->request->setActionName($action);
+                \Qii::getInstance()->dispatcher->setRequest(\Qii::getInstance()->request);
+                \Qii::getInstance()->dispatcher->dispatch($controller, $action, $e);
+                die();
+            }
+        }
+        ob_start();
+        include(join(DS, array(Qii_DIR, 'Exceptions', 'View', 'error.php')));
+        $html = ob_get_contents();
+        ob_clean();
+        
+        if (!IS_CLI) {
+            echo $html;
+            return;
+        }
+        return (new \Qii\Response\Cli())->stdout(
+            str_replace("&nbsp;"
+                , " "
+                , strip_tags(join(PHP_EOL, preg_replace("/[\n|\r\n]/", PHP_EOL, $message)))
+            )
+        );
+    }
+    
+    /**
+     * 获取指定文件的指定行内容
+     *
+     * @param String $fileName 文件名
+     * @param Int $line 行号
+     * @return String
+     */
+    public static function getLineMessage($fileName, $line)
+    {
+        $seekline = max(0, $line - 1);
+        $spl = new \SplFileObject($fileName);
+        $code = array();
+        if ($line > 1) {
+            $maxLine = 10;
+            $firstLine = max(0, $seekline - $maxLine);
+            
+            $spl->seek($firstLine);
+            $min = $seekline - $maxLine;
+            $max = $seekline + $maxLine + 1;
+            
+            for ($i = $min; $i < $max; $i++) {
+                $currentLine = $i + ($min < 0 ? abs($min) : 0) + 1;
+                $color = $currentLine == $line ? ' color="red"' : '';
+                if ($spl->eof()) break;
+                if (IS_CLI) {
+                    $code[] = $currentLine . ($color != '' ? ' 行发生错误' : '') . rtrim($spl->current());
+                } else {
+                    $code[] = '<font ' . $color . '>' . $currentLine . ':</font>' . "\t" . '<font ' . $color . '>' . htmlspecialchars(rtrim($spl->current())) . '</font>';
+                }
+                $spl->next();
+            }
+        } else {
+            $spl->seek($seekline);
+            if (IS_CLI) {
+                $code[] = rtrim($spl->current());
+            } else {
+                $code[] = htmlspecialchars(rtrim($spl->current()));
+            }
+            
+        }
+        return IS_CLI ? PHP_EOL . join(PHP_EOL, $code) : '<pre style="font-weight:bold;">' . join("<br />", $code) . '</pre>';
+    }
+    
+    /**
+     * sprintf 格式化语言错误信息内容
+     *
+     *
+     * Qii::e($message, $argv1, $argv2, ..., $line);
+     * $message = sprintf($message, $argv1, $argv2, ...);
+     * throw new \Qii_Exceptions_Errors($message, $line);
+     */
+    public static function e()
+    {
+        $argvs = func_get_args();
+        $count = count($argvs);
+        $message = array_shift($argvs);
+        $line = (int)array_pop($argvs);
+        if ($count == 2) {
+            throw new \Qii\Exceptions\Errors($message, $line);
+        }
+        $message = vsprintf($message, $argvs);
+        throw new \Qii\Exceptions\Errors($message, $line);
+    }
 }