Browse Source

* Move query class to subfolder
* create App\Exception class
* Better handle app errors by always passing a similar datatype
* Make map_domain able to take an array of other domains to map to the primary domain
* Since _onerror will always get a Response make the default expect it
* make each app have a response and request object (Request gets repopulated each time handle is called)

Nathaniel van Diepen 7 years ago
parent
commit
1b7da240d1
4 changed files with 51 additions and 30 deletions
  1. 23 0
      App/exception.class.php
  2. 0 0
      SQL/query.class.php
  3. 27 29
      app.class.php
  4. 1 1
      sql.class.php

+ 23 - 0
App/exception.class.php

@@ -0,0 +1,23 @@
+<?php
+	namespace App {
+		class Exception extends \Exception {
+			private $included;
+			public function __construct($message = null, $code = 0, Exception $previous = null, string $file = null, int $line = null, array $trace = null, array $included = []){
+				parent::__construct($message, $code, $previous);
+				if(!is_null($file)){
+					$this->file = $file;
+				}
+				if(!is_null($line)){
+					$this->line = $line;
+				}
+				if(!is_null($trace)){
+					$this->trace = $trace;
+				}
+				$this->included = $included;
+			}
+			public function getIncluded(){
+				return $this->included;
+			}
+		}
+	}
+?>

+ 0 - 0
query.class.php → SQL/query.class.php


+ 27 - 29
app.class.php

@@ -5,6 +5,7 @@
 	require_once('request.class.php');
 	require_once('uri.class.php');
 	require_once('events.trait.php');
+	require_once('App/exception.class.php');
 	class App extends Base {
 		use Events;
 		private static $apps = [];
@@ -12,9 +13,12 @@
 		private $routers;
 		private $router;
 		public $_onerror;
+		public $request;
+		public $response;
 		public function __construct(string $name, Callable $fn = null, array $events = null){
 			$this->name = $name;
 			$this->router = new Router();
+			$this->response =  = new Response();
 			$this->routers = [];
 			$this->domains = [];
 			$this->_onerror = function($req, $res, $error){
@@ -25,15 +29,10 @@
 					'line'=>$error->getLine(),
 					'traceback'=>$error->getTrace()
 				]);
-				if($res){
-					$res->code(404);
-					$res->header('Content-Type', 'application/json');
-					$res->end($o);
-				}else{
-					http_response_code(404);
-					header('Content-Type: application/json');
-					print($o);
-				}
+				$res->code(404);
+				$res->header('Content-Type', 'application/json');
+				$res->end($o);
+				$res->shutdown();
 			};
 			if(!is_null($events)){
 				foreach($events as $name => $handler){
@@ -77,10 +76,12 @@
 			}
 		}
 		public static function shutdown_error($error){
-			$req = new Request(App::get_url());
-			$res = new Response();
 			foreach(static::$apps as $k => $app){
-				$app->onerror($req, $res, $error);
+				if(is_null($app->request)){
+					$app->$request = new Request(static::get_url(), getallheaders(), file_get_contents( 'php://input','r'));
+				}
+				$app->onerror($app->request, $app->response, $error);
+				$app->response->shutdown();
 			}
 		}
 		public static function get_url(){
@@ -101,8 +102,8 @@
 			if(is_null($headers)){
 				$headers = getallheaders();
 			}
-			$res = new Response();
-			$req = new Request($url, $headers, $data);
+			$res = $this->response;
+			$this->request = $req = new Request($url, $headers, $data);
 			if($this->fire('handle', $req, $res)){
 				$self = $this;
 				$onerror = function($res, $error) use($self){
@@ -156,16 +157,20 @@
 			$fn($this->domains[$host]);
 			return $this;
 		}
-		public function map_domain(string $host1, string $host2){
-			$this->domains[$host2] = $host1;
+		public function map_domain(string $host, $handle){
+			if(!is_array($handle)){
+				$handle = [$handle];
+			}
+			foreach($handle as $host2){
+				$this->domains[$host] = $host2;
+			}
 			return $this;
 		}
 		public function onerror(Request $req, Response $res, $error){
-			if($this->fire('error', $error, $req, $res)){
-				$fn = $this->_onerror;
-				if(is_callable($fn)){
-					$fn($req, $res, $error);
-				}
+			$this->fire('error', $error);
+			$fn = $this->_onerror;
+			if(is_callable($fn)){
+				$fn($req, $res, $error);
 			}
 		}
 	}
@@ -175,13 +180,6 @@
 		App::shutdown();
 	});
 	set_error_handler(function($errno, $errstr, $errfile, $errline){
-		App::shutdown_error([
-			'number'=> $errno,
-			'msg'=> $errstr,
-			'file'=> $errfile,
-			'line'=> $errline,
-			'backtrace'=> debug_backtrace(),
-			'included'=> get_included_files()
-		]);
+		App::shutdown_error(new App\Exception($errstr, $errno, null, $errfile, $errline, debug_backtrace(), get_included_files()));
 	},E_ALL);
 ?>

+ 1 - 1
sql.class.php

@@ -1,5 +1,5 @@
 <?php
-	require_once('query.class.php');
+	require_once('SQL/query.class.php');
 	/**
 	* SQL class. Used for handling SQL connections
 	*