/** * Exception handler * * This will log the exception and output the exception properties * formatted as html or a 500 response depending on your application config * * @param object The uncaught exception */ public static function exception($e) { static::log($e); if (Config::error('report')) { // clear output buffer while (ob_get_level() > 1) { ob_end_clean(); } if (Request::cli()) { Cli::write(PHP_EOL . 'Uncaught Exception', 'light_red'); Cli::write($e->getMessage() . PHP_EOL); Cli::write('Origin', 'light_red'); Cli::write(substr($e->getFile(), strlen(PATH)) . ' on line ' . $e->getLine() . PHP_EOL); Cli::write('Trace', 'light_red'); Cli::write($e->getTraceAsString() . PHP_EOL); } else { $html = file_get_contents(PATH . '../assets/error_document.tpl.html'); $lines = explode("\n", htmlentities(file_get_contents($e->getFile()))); $line = $e->getLine(); $file = $e->getFile(); $start_line = $line - 5 <= 0 ? 0 : $line - 5; $file_contents = array_slice($lines, $start_line, 1); $array = ['debug.message' => $e->getMessage(), 'debug.location' => "<strong>{$file}</strong> on line <strong>{$line}</strong>", 'debug.backtrace' => $e->getTraceAsString(), 'copyright.year' => date('Y')]; foreach ($array as $find => $replace) { $html = str_replace('{{' . $find . '}}', $replace, $html); } echo $html; } } else { // issue a 500 response Response::error(500, array('exception' => $e))->send(); } exit(1); }
/** * Create a new instance of the Router class for chaining * * @return object */ public static function create() { if (Request::cli()) { // get cli arguments $args = Arr::get($_SERVER, 'argv', array()); $uri = implode('/', array_slice($args, 1)); return new static('cli', trim($uri, '/') ?: '/'); } return new static(Request::method(), Uri::current()); }
/** * Exception handler * * This will log the exception and output the exception properties * formatted as html or a 500 response depending on your application config * * @param object The uncaught exception */ public static function exception($e) { static::log($e); if (Config::error('report')) { // clear output buffer while (ob_get_level() > 1) { ob_end_clean(); } if (Request::cli()) { Cli::write(PHP_EOL . 'Uncaught Exception', 'light_red'); Cli::write($e->getMessage() . PHP_EOL); Cli::write('Origin', 'light_red'); Cli::write(substr($e->getFile(), strlen(PATH)) . ' on line ' . $e->getLine() . PHP_EOL); Cli::write('Trace', 'light_red'); Cli::write($e->getTraceAsString() . PHP_EOL); } else { echo '<html> <head> <title>Uncaught Exception</title> <style> body{font-family:"Open Sans",arial,sans-serif;background:#FFF;color:#333;margin:2em} code{background:#D1E751;border-radius:4px;padding:2px 6px} </style> </head> <body> <h1>Uncaught Exception</h1> <p><code>' . $e->getMessage() . '</code></p> <h3>Origin</h3> <p><code>' . substr($e->getFile(), strlen(PATH)) . ' on line ' . $e->getLine() . '</code></p> <h3>Trace</h3> <pre>' . $e->getTraceAsString() . '</pre> </body> </html>'; } } else { // issue a 500 response Response::error(500, array('exception' => $e))->send(); } exit(1); }
/** * Sends the final headers cookies and output */ public function send() { // dont send headers for CLI if (!Request::cli()) { // create a status header Status::create($this->status)->header(); // always make sure we send the content type if (!array_key_exists('content-type', $this->headers)) { $this->headers['content-type'] = 'text/html; charset=' . Config::app('encoding', 'UTF-8'); } // output headers foreach ($this->headers as $name => $value) { header($name . ': ' . $value); } // send any cookies we may have stored in the cookie class foreach (Cookie::$bag as $cookie) { call_user_func_array('setcookie', array_values($cookie)); } } // output the final content if ($this->output) { echo $this->output; } }