Parcourir la source

* Add access to all templates
* When returning views or controllers, fix the array indexes
* Handle errors better.

Nathaniel van Diepen il y a 7 ans
Parent
commit
3176e5f85b
4 fichiers modifiés avec 32 ajouts et 19 suppressions
  1. 2 2
      App/controller.abstract.class.php
  2. 2 2
      App/view.abstract.class.php
  3. 3 0
      Data/template.class.php
  4. 25 15
      app.class.php

+ 2 - 2
App/controller.abstract.class.php

@@ -6,9 +6,9 @@
 		private $name;
 		public abstract static function handle(Router $router);
 		public static function controllers(){
-			return array_filter(get_declared_classes(), function($class){
+			return array_values(array_filter(get_declared_classes(), function($class){
 				return 0 === strpos($class, "Controller\\");
-			});
+			}));
 		}
 		public static function handle_all(Router $router){
 			foreach(self::controllers() as $controller){

+ 2 - 2
App/view.abstract.class.php

@@ -8,9 +8,9 @@
 		protected  static $name;
 		protected static $template;
 		public static function views() : array{
-			return array_filter(get_declared_classes(), function($class){
+			return array_values(array_filter(get_declared_classes(), function($class){
 				return 0 === strpos($class, "View\\");
-			});
+			}));
 		}
 		public static function name() : string{
 			if(is_null(static::$name)){

+ 3 - 0
Data/template.class.php

@@ -297,5 +297,8 @@
 			ob_end_clean();
 			return $output;
 		}
+		public static function templates(){
+			return static::$templates;
+		}
 	}
 ?>

+ 25 - 15
app.class.php

@@ -86,14 +86,23 @@
 			if(count(ob_list_handlers())){
 				ob_end_clean();
 			}
-			foreach(static::$apps as $k => $app){
-				if(is_null($app->request)){
-					$app->request = new Request(Request::get_verb(), Request::get_url(), Request::get_headers(), Request::get_body());
+			if(count(static::$apps)){
+				foreach(static::$apps as $k => $app){
+					if(is_null($app->request)){
+						$app->request = new Request(Request::get_verb(), Request::get_url(), Request::get_headers(), Request::get_body());
+					}
+					$app->response->open(true);
+					$app->onerror($app->request, $app->response, $error);
+					$app->response->shutdown();
 				}
-				$app->response->open(true);
-				$app->onerror($app->request, $app->response, $error);
-				$app->response->shutdown();
+			}else{
+				$res =new Response();
+				$res->code(500)
+					->header('Content-Type', 'text/plain')
+					->write(print_r($error, true))
+					->shutdown();
 			}
+			flush();
 			if(is_callable('parent::__destruct')){
 				parent::__destruct();
 			}
@@ -205,7 +214,7 @@
 					$fn($req, $res, $error);
 				}
 			}catch(Exception $e){
-				die($e);
+				die("Error handlers failed {$e}");
 			}
 		}
 		public function locale(string $domain, string $folder, string $codeset = 'UTF-8'){
@@ -215,17 +224,18 @@
 			return $this;
 		}
 	}
+	set_exception_handler(function($error){
+		App::shutdown_error($error);
+	});
 	set_error_handler(function($errno, $errstr, $errfile, $errline){
-		App::shutdown_error(new App\Exception($errstr, $errno, null, $errfile, $errline, debug_backtrace()));
-	}, E_ALL);
+		App::shutdown_error(App\Exception($errstr, $errno, null, $errfile, $errline, debug_backtrace()));
+	}, E_ALL & ~E_WARNING);
 	register_shutdown_function(function(){
-		error_reporting(E_ALL);
+		error_reporting(E_ALL & ~E_WARNING);
 		ini_set('display_errors', 'On');
-		try{
-			App::shutdown();
-		}catch(Exception $error){
-			App::shutdown_error(new App\Exception($error->getMessage(), $error->getCode(), $error, $error->getFile(), $error->getLine(), $error->getTrace()));
-		}
+		App::shutdown();
 	});
+	error_reporting(E_ALL & ~E_WARNING);
+	ini_set('display_errors', 'On');
 	gc_enable();
 ?>