From: Tim Düsterhus Date: Wed, 22 Sep 2021 12:28:26 +0000 (+0200) Subject: Inline error handler and exception handler in install.php X-Git-Tag: 5.5.0_Alpha_1~417 X-Git-Url: https://git.stricted.de/?a=commitdiff_plain;h=01bf429b3338a3f5ec7c13a43c719ed2bed79c2f;p=GitHub%2FWoltLab%2FWCF.git Inline error handler and exception handler in install.php --- diff --git a/wcfsetup/install.php b/wcfsetup/install.php index 1f2563977c..64095308dc 100644 --- a/wcfsetup/install.php +++ b/wcfsetup/install.php @@ -7,18 +7,11 @@ * @license GNU Lesser General Public License */ -// define constants define('INSTALL_SCRIPT', __FILE__); define('INSTALL_SCRIPT_DIR', dirname(__FILE__).'/'); define('SETUP_FILE', INSTALL_SCRIPT_DIR . 'WCFSetup.tar.gz'); define('NO_IMPORTS', 1); -// set exception handler -set_exception_handler('handleException'); -// set php error handler -set_error_handler('handleError', E_ALL); - -// define list of needed file $neededFilesPattern = [ '!^setup/.*!', '!^install/files/acp/images/woltlabSuite.*!', @@ -29,105 +22,8 @@ $neededFilesPattern = [ '!^install/files/lib/system/.*!', '!^install/files/lib/util/.*!', '!^install/lang/.*!', - '!^install/packages/.*!']; - -/** @noinspection PhpMultipleClassesDeclarationsInOneFile */ -/** - * A SystemException is thrown when an unexpected error occurs. - * - * @package com.woltlab.wcf - * @author Marcel Werk - */ -class SystemException extends \Exception { - protected $description; - protected $information = ''; - protected $functions = ''; - - /** - * Creates a new SystemException. - * - * @param string $message error message - * @param int $code error code - * @param string $description description of the error - * @param \Exception $previous repacked Exception - */ - public function __construct($message = '', $code = 0, $description = '', \Exception $previous = null) { - parent::__construct((string) $message, (int) $code, $previous); - $this->description = $description; - } - - /** - * Returns the description of this exception. - * - * @return string - */ - public function getDescription() { - return $this->description; - } - - /** - * Prints this exception. - * This method is called by WCF::handleException(). - */ - public function show() { - } -} - -/** - * Loads the required classes automatically. - */ -spl_autoload_register(function($className) { - $namespaces = explode('\\', $className); - if (count($namespaces) > 1) { - // remove 'wcf' component - array_shift($namespaces); - - $className = implode('/', $namespaces); - $classPath = TMP_DIR . 'install/files/lib/' . $className . '.class.php'; - if (file_exists($classPath)) { - require_once($classPath); - } - } -}); - -/** - * Helper method to output debug data for all passed variables, - * uses `print_r()` for arrays and objects, `var_dump()` otherwise. - */ -function wcfDebug() { - echo "
";
-	
-	$args = func_get_args();
-	$length = count($args);
-	if ($length === 0) {
-		echo "ERROR: No arguments provided.
"; - } - else { - for ($i = 0; $i < $length; $i++) { - $arg = $args[$i]; - - echo "

Argument {$i} (" . gettype($arg) . ")

"; - - if (is_array($arg) || is_object($arg)) { - print_r($arg); - } - else { - var_dump($arg); - } - - echo "
"; - } - } - - $backtrace = debug_backtrace(); - - // output call location to help finding these debug outputs again - echo "wcfDebug() called in {$backtrace[0]['file']} on line {$backtrace[0]['line']}"; - - echo "
"; - - exit; -} + '!^install/packages/.*!', +]; function sanitizeStacktrace(\Throwable $e, bool $ignorePaths = false) { $trace = $e->getTrace(); @@ -583,7 +479,7 @@ function printException($e) { show(); @@ -597,15 +493,112 @@ function handleException($e) { catch (\Throwable $exception) { die("
WCF::handleException() Unhandled exception: ".$exception->getMessage()."\n\n".$exception->getTraceAsString());
 	}
-}
-
-function handleError($severity, $message, $file, $line) {
+});
+set_error_handler(static function ($severity, $message, $file, $line) {
 	// this is necessary for the shut-up operator
 	if (!(\error_reporting() & $severity)) {
 		return;
 	}
 
 	throw new ErrorException($message, 0, $severity, $file, $line);
+}, E_ALL);
+
+/** @noinspection PhpMultipleClassesDeclarationsInOneFile */
+/**
+ * A SystemException is thrown when an unexpected error occurs.
+ *
+ * @package	com.woltlab.wcf
+ * @author	Marcel Werk
+ */
+class SystemException extends \Exception {
+	protected $description;
+	protected $information = '';
+	protected $functions = '';
+	
+	/**
+	 * Creates a new SystemException.
+	 *
+	 * @param	string		$message	error message
+	 * @param	int		$code		error code
+	 * @param	string		$description	description of the error
+	 * @param	\Exception	$previous	repacked Exception
+	 */
+	public function __construct($message = '', $code = 0, $description = '', \Exception $previous = null) {
+		parent::__construct((string) $message, (int) $code, $previous);
+		$this->description = $description;
+	}
+	
+	/**
+	 * Returns the description of this exception.
+	 *
+	 * @return	string
+	 */
+	public function getDescription() {
+		return $this->description;
+	}
+	
+	/**
+	 * Prints this exception.
+	 * This method is called by WCF::handleException().
+	 */
+	public function show() {
+	}
+}
+
+/**
+ * Loads the required classes automatically.
+ */
+spl_autoload_register(function($className) {
+	$namespaces = explode('\\', $className);
+	if (count($namespaces) > 1) {
+		// remove 'wcf' component
+		array_shift($namespaces);
+		
+		$className = implode('/', $namespaces);
+		$classPath = TMP_DIR . 'install/files/lib/' . $className . '.class.php';
+		if (file_exists($classPath)) {
+			require_once($classPath);
+		}
+	}
+});
+
+/**
+ * Helper method to output debug data for all passed variables,
+ * uses `print_r()` for arrays and objects, `var_dump()` otherwise.
+ */
+function wcfDebug() {
+	echo "
";
+	
+	$args = func_get_args();
+	$length = count($args);
+	if ($length === 0) {
+		echo "ERROR: No arguments provided.
"; + } + else { + for ($i = 0; $i < $length; $i++) { + $arg = $args[$i]; + + echo "

Argument {$i} (" . gettype($arg) . ")

"; + + if (is_array($arg) || is_object($arg)) { + print_r($arg); + } + else { + var_dump($arg); + } + + echo "
"; + } + } + + $backtrace = debug_backtrace(); + + // output call location to help finding these debug outputs again + echo "wcfDebug() called in {$backtrace[0]['file']} on line {$backtrace[0]['line']}"; + + echo "
"; + + exit; } /** @noinspection PhpMultipleClassesDeclarationsInOneFile */