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

+ 22 - 83
src/App.php

@@ -5,8 +5,7 @@ namespace KarmaFW;
 use KarmaFW\Routing\Router;
 use KarmaFW\Lib\Hooks\HooksManager;
 use KarmaFW\Database\Sql\SqlDb;
-use \KarmaFW\Database\Sql\SqlOrmModel;
-use KarmaFW\Templates\PhpTemplate;
+//use \KarmaFW\Database\Sql\SqlOrmModel;
 
 
 define('FW_SRC_DIR', __DIR__);
@@ -21,26 +20,15 @@ if (! defined('APP_DIR')) {
 class App
 {
 	protected static $booted = false;
-	protected static $session_user = false; // user connected with a session
-	protected static $helpers_dirs = [FW_SRC_DIR . "/../helpers"];
+	protected static $helpers_dirs = [FW_SRC_DIR . "/helpers", APP_DIR . "/src/helpers"];
 
-	public static function boot()
-	{
-		HooksManager::applyHook('app_boot__before', []);
+	public static $db = null;
 
-		// start session
-		if (empty(session_id())) {
-			if (defined('SESSION_NAME') && ! empty(SESSION_NAME)) {
-				session_name(SESSION_NAME);
-			}
-
-			if (defined('SESSION_DURATION') && is_numeric(SESSION_DURATION)) {
-				ini_set('session.gc_maxlifetime', SESSION_DURATION);
-				session_set_cookie_params(SESSION_DURATION);
-				// Note: si cron est actif, il faut modifier la valeur de session.gc_maxlifetime dans /etc/php/7.3/apache2/php.ini (voir /etc/cron.d/php)
-			}
 
-			session_start();
+	public static function boot()
+	{
+		if (defined('USE_HOOKS') && USE_HOOKS) {
+			HooksManager::applyHook('app.boot.before', []);
 		}
 
 		// move fw_helpers at the end of the list (to be loaded the last one)
@@ -68,8 +56,15 @@ class App
 		class_alias('\\KarmaFW\\Database\\Sql\\SqlTools', 'SqlTools');
 		
 
+		if (defined('DB_DSN')) {
+			self::$db = static::getDb();
+		}
+
 		self::$booted = true;
-		HooksManager::applyHook('app_boot__after', []);
+
+		if (defined('USE_HOOKS') && USE_HOOKS) {
+			HooksManager::applyHook('app.boot.after', []);
+		}
 	}	
 
 
@@ -78,6 +73,7 @@ class App
 		self::$helpers_dirs[] = $dir;
 	}
 
+
 	protected static function loadHelpers($dir)
 	{
 		$helpers = glob($dir . '/helpers_*.php');
@@ -88,52 +84,6 @@ class App
 	}
 
 
-	public static function route()
-	{
-		if (! self::$booted) {
-			self::boot();
-		}
-
-		// routing: parse l'url puis transfert au controller
-
-		HooksManager::applyHook('app_route__before', []);
-
-		$route = Router::routeByUrl( $_SERVER['REQUEST_METHOD'], $_SERVER['REQUEST_URI'], false );
-
-		HooksManager::applyHook('app_route__after', [$route]);
-
-		if ($route) {
-			//echo "success: route ok";
-			exit(0);
-
-		} else if ($route === null) {
-			// route found but callback is not callable
-			HooksManager::applyHook('app_route_404', []);
-			errorHttp(404, 'Warning: route callback is not callable', '404 Not Found');
-			exit(1);
-
-		} else if ($route === 0) {
-			// route found but no callback defined
-			HooksManager::applyHook('app_route_404', []);
-			errorHttp(404, "Warning: route found but no callback defined", '404 Not Found');
-			exit(1);
-
-		} else if ($route === false) {
-			// no matching route
-			HooksManager::applyHook('app_route_404', []);
-			errorHttp(404, "Warning: no matching route", '404 Not Found');
-			exit(1);
-
-		} else {
-			// other cases
-			HooksManager::applyHook('app_route_404', []);
-			errorHttp(404, "Warning: cannot route", '404 Not Found');
-			exit(1);
-		}
-
-	}
-
-
 	public static function getDb($instance_name=null, $dsn=null)
 	{
 		/*
@@ -143,6 +93,10 @@ class App
 		static $last_instance_name = null;
 
 		if (empty($instance_name)) {
+			if (! empty(self::$db)) {
+				return self::$db;
+			}
+
 			$instance_name = 'default';
 
 			//if (! empty($last_instance_name)) {
@@ -163,26 +117,11 @@ class App
 	}
 
 
-	public static function createTemplate($tpl_path=null, $variables=[], $layout=null, $templates_dirs=null)
-	{
-		return new PhpTemplate($tpl_path, $variables, $layout, $templates_dirs);
-	}
-
-
+	/*
 	public static function createOrmItem($table_name, $primary_key_values=[], $db=null)
 	{
 		return new SqlOrmModel($table_name, $primary_key_values, $db);
 	}
-	
-
-	public static function getUser()
-	{
-		return self::$session_user;
-	}
-
-	public static function setUser($user)
-	{
-		self::$session_user = $user;
-	}
+	*/
 
 }

+ 28 - 0
src/Routing/Controllers/AppController.php

@@ -0,0 +1,28 @@
+<?php
+
+namespace KarmaFW\Routing\Controllers;
+
+use \KarmaFW\App;
+use \KarmaFW\Lib\Hooks\HooksManager;
+
+
+class AppController
+{
+	protected $db = null;
+
+
+	public function __construct()
+	{
+		if (defined('DB_DSN')) {
+			$this->db = App::getDb();
+		}
+
+		if (defined('USE_HOOKS') && USE_HOOKS) {
+			HooksManager::applyHook('appcontroller__init', [$this]);
+		}
+
+		//echo "DEBUG " . __CLASS__ . ": controller instanced<hr />" . PHP_EOL;
+	}
+
+
+}

+ 101 - 0
src/Routing/Controllers/WebAppController.php

@@ -0,0 +1,101 @@
+<?php
+
+namespace KarmaFW\Routing\Controllers;
+
+use \KarmaFW\WebApp;
+
+
+class WebAppController extends AppController
+{
+	protected $request_uri = null;
+	protected $request_method = null;
+	protected $route = null;
+	protected $template;
+
+	
+	public function __construct($request_uri=null, $request_method=null, $route=null)
+	{
+		parent::__construct($request_uri, $request_method, $route);
+
+		$this->user_id = session('user_id');
+
+		$_SESSION['flash'] = []; // ['success' => 'action done !', 'error' => 'an error occured', 'warning' => 'notice ...']
+
+		if (defined('TPL_DIR')) {
+			$this->template = WebApp::createTemplate();
+
+			$this->template->assign('user_id', $this->user_id);
+
+			if (defined('APP_NAME')) {
+				$this->template->assign('meta_title', APP_NAME);
+				$this->template->assign('meta_description', APP_NAME);
+				$this->template->assign('h1', APP_NAME);
+			}
+		}
+	}
+
+	public function getRoute()
+	{
+		return $this->route;
+	}
+
+	public function getRequestMethod()
+	{
+		return $this->request_method;
+	}
+
+	public function getRequestUri()
+	{
+		return $this->request_uri;
+	}
+
+	public function getTemplate()
+	{
+		return $this->template;
+	}
+
+
+
+
+	public function error400()
+	{
+		$meta_title = 'Bad request';
+		$h1 = 'Error 400 - Bad request';
+		$content = '';
+		return $this->error(400, $meta_title, $h1, $content);
+	}
+
+	public function error403()
+	{
+		$meta_title = 'Forbidden';
+		$h1 = 'Error 403 - Forbidden';
+		$content = 'you are not allowed';
+		return $this->error(403, $meta_title, $h1, $content);
+	}
+
+	public function error404()
+	{
+		$meta_title = 'Page not found';
+		$h1 = 'Error 404 - Page not found';
+		$content = "The page you're looking for doesn't exist.";
+		return $this->error(404, $meta_title, $h1, $content);
+	}
+
+	public function error500()
+	{
+		$meta_title = 'Internal Server Error';
+		$h1 = 'Error 500 - Internal Server Error';
+		$content = 'An error has occured';
+		return $this->error(500, $meta_title, $h1, $content);
+	}
+
+	public function error503()
+	{
+		$meta_title = 'Service Unavailable';
+		$h1 = 'Error 503 Service Unavailable';
+		$content = 'The service is unavailable';
+		return $this->error(503, $meta_title, $h1, $content);
+	}
+
+
+}

+ 0 - 42
src/Routing/Controllers/WebController.php

@@ -1,42 +0,0 @@
-<?php
-
-namespace KarmaFW\Routing\Controllers;
-
-use \KarmaFW\Lib\Hooks\HooksManager;
-
-
-class WebController
-{
-	protected $route = null;
-	protected $request_method = null;
-	protected $request_uri = null;
-
-
-	public function __construct($route, $request_method, $request_uri)
-	{
-		$this->route = $route;
-		$this->request_method = $request_method;
-		$this->request_uri = $request_uri;
-		
-		//echo "DEBUG " . __CLASS__ . ": controller instanced<hr />" . PHP_EOL;
-
-		HooksManager::applyHook('webcontroller__init', [$this]);
-	}
-
-
-	public function getRoute()
-	{
-		return $this->route;
-	}
-
-	public function getRequestMethod()
-	{
-		return $this->request_method;
-	}
-
-	public function getRequestUri()
-	{
-		return $this->request_uri;
-	}
-
-}

+ 5 - 2
src/Routing/Router.php

@@ -2,6 +2,8 @@
 
 namespace KarmaFW\Routing;
 
+use \KarmaFW\WebApp;
+
 
 class Router
 {
@@ -128,8 +130,9 @@ class Router
 		if (gettype($callback) == 'array') {
 			//echo " => ARRAY !<br />" . PHP_EOL;
 			//pre($callback, 1);
-			$class = new $callback[0]($route, $request_method, $request_uri);
-			call_user_func([$class, $callback[1]], $matched_params);
+			$controller = new $callback[0]($request_uri, $request_method, $route);
+			WebApp::$controller = $controller;
+			call_user_func([$controller, $callback[1]], $matched_params);
 
 		} else {
 			//echo " => FUNCTION !<br />" . PHP_EOL;

+ 200 - 0
src/WebApp.php

@@ -0,0 +1,200 @@
+<?php
+
+namespace KarmaFW;
+
+use \KarmaFW\Routing\Router;
+use \KarmaFW\Routing\Controllers\WebAppController;
+use \KarmaFW\Lib\Hooks\HooksManager;
+use \KarmaFW\Templates\PhpTemplate;
+
+
+class WebApp extends App
+{
+	public static $session_user = false; // user connected with a session
+	public static $controller = null;
+
+
+	public static function boot()
+	{
+		parent::boot();
+
+		if (defined('USE_HOOKS') && USE_HOOKS) {
+			HooksManager::applyHook('webapp.boot.before', []);
+		}
+
+		// start session
+		if (empty(session_id())) {
+			if (defined('SESSION_NAME') && ! empty(SESSION_NAME)) {
+				session_name(SESSION_NAME);
+			}
+
+			if (defined('SESSION_DURATION') && is_numeric(SESSION_DURATION)) {
+				ini_set('session.gc_maxlifetime', SESSION_DURATION);
+				session_set_cookie_params(SESSION_DURATION);
+				// Note: si cron est actif, il faut modifier la valeur de session.gc_maxlifetime dans /etc/php/7.3/apache2/php.ini (voir /etc/cron.d/php)
+			}
+
+			session_start();
+		}
+
+		if (defined('USE_HOOKS') && USE_HOOKS) {
+			HooksManager::applyHook('webapp.boot.after', []);
+		}
+	}
+
+
+	public static function createTemplate($tpl_path=null, $variables=[], $layout=null, $templates_dirs=null)
+	{
+		return new PhpTemplate($tpl_path, $variables, $layout, $templates_dirs);
+	}
+
+
+	/*
+	public static function getUser()
+	{
+		return self::$session_user;
+	}
+
+	public static function setUser($user)
+	{
+		self::$session_user = $user;
+	}
+	*/
+
+
+	public static function route()
+	{
+		if (! self::$booted) {
+			self::boot();
+		}
+
+		// routing: parse l'url puis transfert au controller
+
+		if (defined('USE_HOOKS') && USE_HOOKS) {
+			HooksManager::applyHook('app.route.before', []);
+		}
+
+		$route = Router::routeByUrl( $_SERVER['REQUEST_METHOD'], $_SERVER['REQUEST_URI'], false );
+
+		if (defined('USE_HOOKS') && USE_HOOKS) {
+			HooksManager::applyHook('app.route.after', [$route]);
+		}
+
+		if ($route) {
+			//echo "success: route ok";
+			if (defined('USE_HOOKS') && USE_HOOKS) {
+				HooksManager::applyHook('app.route.success', [$_SERVER['REQUEST_URI'], $_SERVER['REQUEST_METHOD'], $route]);
+			}
+			exit(0);
+
+		} else if ($route === null) {
+			// route found but callback is not callable
+			self::error404('Warning: route callback is not callable', '404 Not Found');
+			exit(1);
+
+		} else if ($route === 0) {
+			// route found but no callback defined
+			if (defined('USE_HOOKS') && USE_HOOKS) {
+				HooksManager::applyHook('app.route.error', []);
+			}
+			self::error404("Warning: route found but no callback defined", '404 Not Found');
+			exit(1);
+
+		} else if ($route === false) {
+			// no matching route
+			if (defined('USE_HOOKS') && USE_HOOKS) {
+				HooksManager::applyHook('app.route.error', []);
+			}
+			self::error404("Warning: no matching route", '404 Not Found');
+			exit(1);
+
+		} else {
+			// other cases
+			if (defined('USE_HOOKS') && USE_HOOKS) {
+				HooksManager::applyHook('app.route.error', []);
+			}
+			self::error404("Warning: cannot route", '404 Not Found');
+			exit(1);
+		}
+
+	}
+
+
+	public static function error($http_status = 500, $meta_title = 'Server Error', $h1 = 'Error 500 - Server Error', $content = 'an error has occured', $error_template = 'error.tpl.php')
+	{
+		if (! self::$controller) {
+			self::$controller = new WebAppController();
+		}
+		if (self::$controller && $template = self::$controller->getTemplate()) {
+			$template->assign('meta_title', $meta_title);
+			$template->assign('h1', $h1);
+			$template->assign('p', $content);
+			$template->assign('http_status', $http_status);
+
+			$template->display($error_template);
+
+		} else {
+			//header("HTTP/1.0 " . $error_code . " " . $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($content)) {
+				$output_html .= '<p>' . $content . '</p>' . PHP_EOL;
+			}
+			$output_html .= '</body>' . PHP_EOL;
+			$output_html .= '</html>' . PHP_EOL;
+
+			echo $output_html;
+		}
+
+		exit;
+	}
+
+
+	public static function error400($title = 'Bad request')
+	{
+		$meta_title = $title;
+		$h1 = $title;
+		$content = '';
+		return self::error(400, $meta_title, $h1, $content);
+	}
+
+	public static function error403($title = 'Forbidden')
+	{
+		$meta_title = $title;
+		$h1 = $title;
+		$content = 'You are not allowed';
+		return self::error(403, $meta_title, $h1, $content);
+	}
+
+	public static function error404($message = "The page you're looking for doesn't exist.", $title = 'Page not Found')
+	{
+		return self::error(404, $title, $title, $message);
+	}
+
+	public static function error500($title = 'Internal Server Error')
+	{
+		$meta_title = $title;
+		$h1 = $title;
+		$content = 'An error has occured';
+		return $this->error(500, $meta_title, $h1, $content);
+	}
+
+	public static function error503($title = 'Service Unavailable')
+	{
+		$meta_title = $title;
+		$h1 = $title;
+		$content = 'The service is unavailable';
+		return $this->error(503, $meta_title, $h1, $content);
+	}
+
+}

+ 0 - 0
helpers/helpers_array.php → src/helpers/helpers_array.php


+ 0 - 0
helpers/helpers_default.php → src/helpers/helpers_default.php


+ 0 - 0
helpers/helpers_form.php → src/helpers/helpers_form.php


+ 0 - 0
helpers/helpers_smtp_hooks.php → src/helpers/helpers_smtp_hooks.php