소스 검색

* Add guid to sql so that array_diff will work
* Fix error raising in query
* Fix destruct in app and orm
* event trait fire will return true/false depening on if it should be cancelled or not
* App::get_url added
* App error event will pass request/response
* App handle event can cancel normal handle
* App error event can cancel normal onerror

Nathaniel van Diepen 7 년 전
부모
커밋
9716442c9d
5개의 변경된 파일60개의 추가작업 그리고 61개의 파일을 삭제
  1. 48 56
      app.class.php
  2. 4 2
      events.trait.php
  3. 1 1
      orm.abstract.class.php
  4. 2 2
      query.class.php
  5. 5 0
      sql.class.php

+ 48 - 56
app.class.php

@@ -47,7 +47,7 @@
 		}
 		public function __destruct(){
 			$this->routers = [];
-			$index = array_search(static::$apps, $this);
+			$index = array_search($this, static::$apps);
 			if($index !== false){
 				array_splice(static::$apps, $index, 1);
 			}
@@ -65,18 +65,7 @@
 		}
 		public static function shutdown(){
 			$verb = $_SERVER['REQUEST_METHOD'];
-			if(isset($_SERVER['REDIRECT_URL'])){
-				$url = 'REDIRECT_URL';
-			}else{
-				$url = 'REQUEST_URI';
-			}
-			if(!empty($_SERVER['HTTP_X_FORWARDED_PROTO'])){
-				$root = $_SERVER['HTTP_X_FORWARDED_PROTO'].'://';
-			}else{
-				$root = !empty($_SERVER['HTTPS']) ? "https://" : "http://";
-			}
-			// @todo - determine if http or http and also check cloudflare
-			$url = parse_url($root.$_SERVER['HTTP_HOST'].$_SERVER[$url]);
+			$url = App::get_url();
 			$data = file_get_contents( 'php://input','r');
 			foreach(static::$apps as $k => $app){
 				if($app instanceof App){
@@ -88,46 +77,61 @@
 			}
 		}
 		public static function shutdown_error($error){
+			$req = new Request(App::get_url());
+			$res = new Response();
 			foreach(static::$apps as $k => $app){
-				if(is_callable($app->onerror)){
-					$app->onerror(null, null, $error);
-				}
+				$app->onerror($req, $res, $error);
 			}
 		}
+		public static function get_url(){
+			if(isset($_SERVER['REDIRECT_URL'])){
+				$url = 'REDIRECT_URL';
+			}else{
+				$url = 'REQUEST_URI';
+			}
+			if(!empty($_SERVER['HTTP_X_FORWARDED_PROTO'])){
+				$root = $_SERVER['HTTP_X_FORWARDED_PROTO'].'://';
+			}else{
+				$root = !empty($_SERVER['HTTPS']) ? "https://" : "http://";
+			}
+			// @todo - determine if http or http and also check cloudflare
+			return parse_url($root.$_SERVER['HTTP_HOST'].$_SERVER[$url]);
+		}
 		public function handle(string $verb, array $url, string $data, array $headers = null){
 			if(is_null($headers)){
 				$headers = getallheaders();
 			}
 			$res = new Response();
 			$req = new Request($url, $headers, $data);
-			$this->fire('handle', $req, $res);
-			$self = $this;
-			$onerror = function($res, $error) use($self){
-				$self->onerror($req, $res, $error);
-			};
-			$handled = false;
-			// Domain routers
-			foreach($this->domains as $host => $router){
-				if($url['host'] == $host){
-					while(is_string($router)){
-						$router = $this->domains[$router];
+			if($this->fire('handle', $req, $res)){
+				$self = $this;
+				$onerror = function($res, $error) use($self){
+					$self->onerror($req, $res, $error);
+				};
+				$handled = false;
+				// Domain routers
+				foreach($this->domains as $host => $router){
+					if($url['host'] == $host){
+						while(is_string($router)){
+							$router = $this->domains[$router];
+						}
+						$router->handle($url["path"], $req, $res, null, $onerror);
+						$handled = $handled || $router->handled;
 					}
+				}
+				// Prefixed path routers
+				foreach($this->routers as $prefix => $router){
 					$router->handle($url["path"], $req, $res, null, $onerror);
 					$handled = $handled || $router->handled;
 				}
+				// Base router for non-prefixed paths
+				$this->router->handle($url["path"], $req, $res, function($req, $res) use($handled, $self){
+					if(!$handled){
+						$self->onerror($req, $res,new Error("Not Found", 404));
+					}
+				}, $onerror);
+				$this->fire('afterhandle', $req, $res);
 			}
-			// Prefixed path routers
-			foreach($this->routers as $prefix => $router){
-				$router->handle($url["path"], $req, $res, null, $onerror);
-				$handled = $handled || $router->handled;
-			}
-			// Base router for non-prefixed paths
-			$this->router->handle($url["path"], $req, $res, function($req, $res) use($handled, $self){
-				if(!$handled){
-					$self->onerror($req, $res,new Error("Not Found", 404));
-				}
-			}, $onerror);
-			$this->fire('afterhandle', $req, $res);
 			return $res;
 		}
 		public function error(Callable $fn){
@@ -157,10 +161,11 @@
 			return $this;
 		}
 		public function onerror(Request $req, Response $res, $error){
-			$this->fire('error', $error);
-			$fn = $this->_onerror;
-			if(is_callable($fn)){
-				$fn($req, $res, $error);
+			if($this->fire('error', $error, $req, $res)){
+				$fn = $this->_onerror;
+				if(is_callable($fn)){
+					$fn($req, $res, $error);
+				}
 			}
 		}
 	}
@@ -179,17 +184,4 @@
 			'included'=> get_included_files()
 		]);
 	},E_ALL);
-	register_shutdown_function(function(){
-		$error = error_get_last();
-		if($error['type'] == 1){
-			App::shutdown_error([
-				'number'=> $error['type'],
-				'msg'=> $error['message'],
-				'file'=> $error['file'],
-				'line'=> $error['line'],
-				'backtrace'=> [],
-				'included'=> get_included_files()
-			]);
-		}
-	});
 ?>

+ 4 - 2
events.trait.php

@@ -22,10 +22,12 @@
 		public function fire(string $name, ...$args){
 			if(isset($this->events[$name])){
 				foreach($this->events[$name] as $fn){
-					$fn(...$args);
+					if($fn(...$args) === false){
+						return false;
+					}
 				}
 			}
-			return $this;
+			return true;
 		}
 	}
 ?>

+ 1 - 1
orm.abstract.class.php

@@ -70,7 +70,7 @@
 			$this->_changed = [];
 			$this->_data = [];
 			$this->_related = [];
-			$key = array_search(self::$instances, $this);
+			$key = array_search($this, self::$instances);
 			if($key !== false){
 				array_splice(self::$instances, $key, 1);
 			}

+ 2 - 2
query.class.php

@@ -16,11 +16,11 @@
 				$this->sql = $sql();
 				$this->query = $this->sql->stmt_init();
 				if(!$this->query->prepare($source)){
-					throw new Exception($this->query->error);
+					throw new \RuntimeException($this->query->error);
 				}
 				if(!is_null($types)){
 					if(!$this->query->bind_param(...\SQL::make_referenced($args))){
-						throw new Exception("Unable to bind parameter {$this->query->error}");
+						throw new \RuntimeException("Unable to bind parameter {$this->query->error}");
 					}
 				}
 			}

+ 5 - 0
sql.class.php

@@ -16,10 +16,12 @@
 		* @private
 		* @required
 		*/
+		private $guid;
 		private $sql;
 		public $queries = [];
 		private static $connections = [];
 		public function __construct($server,$user,$pass,$db){
+			$this->guid = uniqid();
 			$this->sql = new mysqli('p:'.$server,$user,$pass,$db) or die('Unable to connect to mysql');
 			self::$connections[] = $sql;
 		}
@@ -44,6 +46,9 @@
 				break;
 			}
 		}
+		public function __toString(){
+			return $this->guid;
+		}
 		/**
 		* Returns a Query object based on inputs
 		*