PHP 7 compat
authorMichael Wallner <mike@php.net>
Fri, 22 Jan 2016 08:53:53 +0000 (09:53 +0100)
committerMichael Wallner <mike@php.net>
Fri, 22 Jan 2016 08:53:53 +0000 (09:53 +0100)
mdref/ExceptionHandler.php
public/index.php

index 608afaadf42d3299de6a131c1be68f6cf43fbbce..b641629f3c9372a63697e29ae2c8ee738ae5af1f 100644 (file)
@@ -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;
                }
index 58ec5fe565cec71e20192088a1b0592fbc4b6099..e6d0f75a4ce4a9fe449d1cd92c8f81c97b9c7570 100644 (file)
@@ -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();