From: Michael Wallner Date: Fri, 22 Jan 2016 08:53:53 +0000 (+0100) Subject: PHP 7 compat X-Git-Url: https://git.m6w6.name/?p=mdref%2Fmdref;a=commitdiff_plain;h=d88268c43ab5d4230fd20408248fd239635d354c PHP 7 compat --- diff --git a/mdref/ExceptionHandler.php b/mdref/ExceptionHandler.php index 608afaa..b641629 100644 --- a/mdref/ExceptionHandler.php +++ b/mdref/ExceptionHandler.php @@ -9,25 +9,40 @@ use http\Env as HTTP; */ class ExceptionHandler { + /** + * @var \http\Env\Response + */ + private $response; + /** * Set up error/exception/shutdown handler */ - public function __construct() { + public function __construct(\http\Env\Response $r) { + $this->response = $r; set_exception_handler($this); set_error_handler($this); register_shutdown_function($this); } - + + private static function cleanBuffers() { + while (ob_get_level()) { + if (!@ob_end_clean()) { + break; + } + } + } + /** * The exception/error/shutdown handler callback */ public function __invoke($e = null, $msg = null) { - if ($e instanceof \Exception) { + if ($e instanceof \Exception || $e instanceof \Throwable) { try { + self::cleanBuffers(); echo static::htmlException($e); } catch (\Exception $ignore) { headers_sent() or HTTP::setResponseCode(500); - die("FATAL ERROR"); + die("FATAL ERROR:\n$e\n$ignore"); } } elseif (isset($e, $msg)) { throw new \Exception($msg, $e); @@ -38,33 +53,26 @@ class ExceptionHandler case E_USER_ERROR: case E_CORE_ERROR: case E_COMPILE_ERROR: - while (ob_get_level()) { - if (!@ob_end_clean()) { - break; - } - } - $message = sprintf("%s in %s at line %d", + self::cleanBuffers(); + $message = sprintf("%s in %s at line %d", $error["message"], $error["file"], $error["line"]); echo static::htmlError("Application Error", $message, 500, ""); break; } } } - + /** * Format an exception as HTML and send appropriate exception info as HTTP headers - * @param \Exception $e + * @param Throwable $e * @param array $title_tag * @param array $message_tag * @param array $trace_tag * @return string */ - public static function htmlException(\Exception $e, array $title_tag = ["h1"], array $message_tag = ["p"], array $trace_tag = ["pre", "style='font-size:smaller;overflow-x:scroll'"]) { + public static function htmlException(/*\Throwable*/ $e, array $title_tag = ["h1"], array $message_tag = ["p"], array $trace_tag = ["pre", "style='font-size:smaller;overflow-x:scroll'"]) { if ($e instanceof Exception) { $code = $e->getCode() ?: 500; - #foreach ($e->getHeaders() as $key => $val) { - # HTTP::setResponseHeader($key, $val); - #} } else { $code = 500; } diff --git a/public/index.php b/public/index.php index 58ec5fe..e6d0f75 100644 --- a/public/index.php +++ b/public/index.php @@ -13,14 +13,18 @@ define("REFS", getenv("REFPATH") ?: implode(PATH_SEPARATOR, glob(ROOT."/refs/*") ini_set("open_basedir", ROOT.PATH_SEPARATOR.REFS); +if (!ini_get("date.timezone")) { + date_default_timezone_set("UTC"); +} + spl_autoload_register(function($c) { if (!strncmp($c, "mdref\\", 6)) { return require ROOT . "/" . strtr($c, "\\", "/") . ".php"; } }); -new ExceptionHandler; - +$response = new Response; +$ehandler = new ExceptionHandler($response); $reference = new Reference(explode(PATH_SEPARATOR, REFS)); -$action = new Action($reference, new Request, new Response, new BaseUrl); +$action = new Action($reference, new Request, $response, new BaseUrl); $action->handle();