Przeglądaj źródła

* Allow forcing responses open if they have shutdown
* Allow forcing a template to compile
* Allow forcing a views to compile their template
* Fix shutdown_error when ob_start was called
* Attempt to handle when error handlers throw errors

Nathaniel van Diepen 7 lat temu
rodzic
commit
93be25e1c6
4 zmienionych plików z 58 dodań i 15 usunięć
  1. 20 6
      App/view.abstract.class.php
  2. 18 3
      Data/template.class.php
  3. 4 1
      Http/response.class.php
  4. 16 5
      app.class.php

+ 20 - 6
App/view.abstract.class.php

@@ -5,7 +5,7 @@
 	use Juju\Data\{Response, Template};
 
 	abstract class View {
-		private  static $name;
+		protected  static $name;
 		protected static $template;
 		public static function views() : array{
 			return array_filter(get_declared_classes(), function($class){
@@ -24,18 +24,32 @@
 				'view'=>get_called_class()
 			];
 		}
-		final public static function render(array $data = []) : string{
+		final public static function template(){
 			if(is_null(static::$template) && method_exists(get_called_class(), 'setup')){
 				static::setup();
 			}
 			$template = static::$template ?? static::name();
-			$data = array_merge(static::data(),$data);
 			if(is_string($template)){
-				return Template::from($template, $data);
+				$template = Template::get($template);
 			}elseif(is_array($template)){
-					$template = new Template($template['name'], $template['fn']);
+				$template = new Template($template['name'], $template['fn']);
+			}
+			return $template;
+		}
+		final public static function render(array $data = []) : string{
+			$data = array_merge(static::data(), $data);
+			return static::template()->run($data);
+		}
+		final public static function cache(){
+			foreach(self::views() as $view){
+				if(method_exists($view, 'setup')){
+					$view::setup();
+				}
+				$template = $view::template();
+				if(!is_null($template)){
+					$template->to_file();
+				}
 			}
-			return $template->run($data);
 		}
 	}
 ?>

+ 18 - 3
Data/template.class.php

@@ -200,18 +200,30 @@
 			$this->path = static::$cachedir."/{$this->name}.".md5($this->template).'.php';
 			static::$templates[$name] = $this;
 		}
+		public function __get(string $name){
+			switch($name){
+				case 'name':case 'template':case 'path':
+					return $this->$name;
+				break;
+				default:
+					throw new \Exception("Property {$name} doesn't exist");
+			}
+		}
+		public function to_file(){
+			file_put_contents($this->path, static::compile($this->template));
+		}
 		public function run(array $data) : string{
 			$data = EArray::from($data);
 			if($this->fire('before', $data) === false){
 				throw new Exception("Render on template {$this->name} cancelled. Before.");
 			}
 			if(!file_exists($this->path)){
-				file_put_contents($this->path, static::compile($this->template));
+				$this->to_file();
 			}
 			try{
 				$output = static::execute($this->path, $data);
 			}catch(Exception $e){
-				file_put_contents($this->path, static::compile($this->template));
+				$this->to_file();
 				$output = static::execute($this->path, $data);
 			}
 			if(class_exists('tidy')){
@@ -231,12 +243,15 @@
 			return (string)$output;
 		}
 		public static function from(string $name, array $data = []) : string{
-			$template = static::$templates[$name] ?? null;
+			$template = static::get($name);
 			if(is_null($template)){
 				throw new Exception("Template {$name} does not exist");
 			}
 			return $template->run($data);
 		}
+		public static function get(string $name){
+			return static::$templates[$name] ?? null;
+		}
 		public static function compile(string $template) : string{
 			$ignored = [];
 			$output = $template;

+ 4 - 1
Http/response.class.php

@@ -144,7 +144,10 @@
 			}
 			return $this;
 		}
-		public function open(){
+		public function open(bool $force = false){
+			if($force){
+				$this->shutdown = false;
+			}
 			if(!$this->shutdown){
 				$this->open = true;
 			}

+ 16 - 5
app.class.php

@@ -83,15 +83,22 @@
 			}
 		}
 		public static function shutdown_error($error){
+			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());
 				}
+				$app->response->open(true);
 				$app->onerror($app->request, $app->response, $error);
 				$app->response->shutdown();
 			}
+			if(is_callable('parent::__destruct')){
+				parent::__destruct();
+			}
 		}
-		public function handle(string $verb, array $url, string $data, array $headers = null){
+		public function handle(string $verb, array $url, string $data, array $headers = null) : Response{
 			if(is_null($headers)){
 				$headers = Request::get_headers();
 			}
@@ -191,10 +198,14 @@
 			return $this->map_domain($host, $hosts)->domain($host, $fn);
 		}
 		public function onerror(Request $req, Response $res, $error){
-			$this->fire('error', $error);
-			$fn = $this->_onerror;
-			if(is_callable($fn)){
-				$fn($req, $res, $error);
+			try{
+				$this->fire('error', $error);
+				$fn = $this->_onerror;
+				if(is_callable($fn)){
+					$fn($req, $res, $error);
+				}
+			}catch(Exception $e){
+				die($e);
 			}
 		}
 		public function locale(string $domain, string $folder, string $codeset = 'UTF-8'){