Max F 5 лет назад
Родитель
Сommit
748e20719d

+ 7 - 2
src/App/Middlewares/UrlRouter.php

@@ -34,14 +34,19 @@ class UrlRouter
 
 			ob_start();
 			
-			$response = Router::routeRequest($request, $response);
+			$route_response = Router::routeRequest($request, $response);
 
 			// en principe le contenu de la reponse est dans $response->content
 			// mais si il y a eu des "echo", ils sont capturés par le ob_start puis insérés au début de $response->content
 
 			$content = ob_get_contents();
 			ob_end_clean();
-			$response->prepend($content);
+//var_dump($response); 
+			if (! empty($route_response) && is_a($route_response, Response::class)) {
+				$response = $route_response;
+			}
+
+			$response->prepend($content); // on ajoute ici le texte capturé pendant l'execution de la route
 
 			$response = $next($request, $response);
 

+ 2 - 1
src/Http/Response.php

@@ -150,7 +150,8 @@ class Response
 
 	public function getContentLength()
 	{
-		return strlen($this->body);
+		return empty($this->body) ? 0 : strlen($this->body);
+		//return empty($this->body) || ! is_string($this->body) ? 0 : strlen($this->body);
 	}
 
 	public function setBody($body)

+ 40 - 2
src/Routing/Controllers/WebAppController.php

@@ -83,9 +83,47 @@ class WebAppController extends AppController
 
 
 
-	public function error($http_status, $meta_title=null, $h1=null, $message=null)
+	public function error($http_status = 500, $meta_title = 'Server Error', $h1 = 'Error 500 - Server Error', $message = 'an error has occured')
 	{
-		return WebApp::error($http_status, $meta_title, $h1, $message);
+		if ($template = $this->getTemplate()) {
+			$template->assign('meta_title', $meta_title);
+			$template->assign('h1', $h1);
+			$template->assign('p', $message);
+			$template->assign('http_status', $http_status);
+
+			$error_template = 'error.tpl.php';
+			if (defined('ERROR_TEMPLATE')) {
+				$error_template = ERROR_TEMPLATE;
+			}
+
+			//$template->display($error_template);
+			return $this->response->setHtml( $template->fetch($error_template) , $http_status);
+
+		} else {
+			//header("HTTP/1.0 " . $http_status . " " . $meta_title);
+
+			$output_html = '';
+			$output_html .= '<html>' . PHP_EOL;
+			$output_html .= '<head>' . PHP_EOL;
+			if (! empty($meta_title)) {
+				$output_html .= '<title>' . $meta_title . '</title>' . PHP_EOL;
+			}
+			$output_html .= '</head>' . PHP_EOL;
+			$output_html .= '<body>' . PHP_EOL;
+			if (! empty($h1)) {
+				$output_html .= '<h1>' . $h1 . '</h1>' . PHP_EOL;
+			}
+			if (! empty($message)) {
+				$output_html .= '<p>' . $message . '</p>' . PHP_EOL;
+			}
+			$output_html .= '</body>' . PHP_EOL;
+			$output_html .= '</html>' . PHP_EOL;
+
+			//echo $output_html;
+
+			return $this->response->setHtml($output_html, $http_status);
+		}
+
 	}
 
 	public function error400($title = 'Bad request', $message = '')

+ 8 - 5
src/Routing/Router.php

@@ -82,6 +82,7 @@ class Router
 
 	public static function error404($callback=null)
 	{
+		// todo: faire en sorte qu'elle soit executée en dernier
 		return self::all('.*', $callback, 'regex');
 	}
 
@@ -226,7 +227,7 @@ class Router
 				if (empty($callback)) {
 					// route found but no callback defined
 					//return 0;
-					return $response->setStatus(404)->setHtml('<h1>Page not Found</h1><p>Warning: route found but no callback defined</p>');
+					return $response->setHtml('<h1>Page not Found</h1><p>Warning: route found but no callback defined</p>', 404);
 
 				} else if (is_callable($callback)) {
 					// OK !
@@ -237,7 +238,7 @@ class Router
 				} else {
 					// route found but callback is not callable
 					//return null;
-					return $response->setStatus(404)->setHtml('<h1>Page not Found</h1><p>Warning: route callback is not callable</p>');
+					return $response->setHtml('<h1>Page not Found</h1><p>Warning: route callback is not callable</p>', 404);
 				}
 
 			}
@@ -246,7 +247,7 @@ class Router
 
 		// no matching route
 		//return false;
-		return $response->setStatus(404)->setHtml('<h1>Page not Found</h1><p>Warning: no matching route</p>');
+		return $response->setHtml('<h1>Page not Found</h1><p>Warning: no matching route</p>', 404);
 	}
 
 
@@ -268,14 +269,16 @@ class Router
 			$route_response = $callback($request, $response, $matched_params);
 		}
 
-		if ($route_response instanceof Response) {
+		if (is_object($route_response) && get_class($route_response) === Response::class) {
 			$response = $route_response;
 
 		} else if ($route_response) {
 			return $response->setHtml('<html><body><h1>Server Error</h1><p>Error: $response is not a Response</p></body></html>', 404);
 
 		} else {
-			return $response->setHtml('<html><body><h1>Server Error</h1><p>Error: $response is empty</p></body></html>', 404);
+			if (! $response->getContentLength()) {
+				return $response->setHtml('<html><body><h1>Server Error</h1><p>Error: $response is empty</p></body></html>', 404);
+			}
 		}
 
 		return $response;