Max F 5 роки тому
батько
коміт
b813a82091

+ 9 - 138
src/App.php

@@ -2,26 +2,10 @@
 
 namespace KarmaFW;
 
-use \KarmaFW\Routing\Router;
 use \KarmaFW\Lib\Hooks\HooksManager;
 use \KarmaFW\Database\Sql\SqlDb;
 //use \KarmaFW\Database\Sql\SqlOrmModel;
 
-use \KarmaFW\App\Request;
-use \KarmaFW\App\Response;
-use \KarmaFW\App\Pipe;
-use \KarmaFW\App\Container;
-
-
-define('FW_SRC_DIR', __DIR__);
-define('FW_DIR', __DIR__ . "/..");
-
-if (! defined('APP_DIR')) {
-	echo "ERROR: Please, define APP_DIR" . PHP_EOL;
-	exit(1);
-}
-
-
 class App
 {
 	protected static $booted = false;
@@ -33,107 +17,21 @@ class App
 	public static $db = null;
 	public static $data = [];
 
-	protected static $instance = null;
-	protected $middlewares;
-	protected $container;
-
-
-	public function __construct($middlewares=[])
-	{
-		$this->middlewares = $middlewares;
-		$this->container = new Container;
-		self::$instance = $this;
-
-		try {
-			$this->configure();
-
-			self::loadHelpersDirs();
-
-		} catch (\Exception $e) {
-			header("HTTP/1.0 500 Internal Server Error");
-			echo "<h1>Server error</h1>";
-
-			if (ENV === 'dev') {
-				echo "<pre>";
-				print_r($e);
-				echo "</pre>";
-			}
-			exit;
-		}
-
-	}
-
-
-	public function configure()
-	{
-		if (is_file(APP_DIR . '/config/config.php')) {
-			require APP_DIR . '/config/config.php';
-		}
-
-
-		if (! defined('APP_NAME')) {
-			define('APP_NAME', "PHP Application");
-		}
-
-		if (! defined('TPL_DIR')) {
-			//define('TPL_DIR', APP_DIR . '/templates');
-		}
-
-		if (! defined('ENV')) {
-			$env = defined('ENVIRONMENT') ? ENVIRONMENT : 'prod';
-			define('ENV', $env);
-		}
-
-		if (! defined('DB_DSN')) {
-			//define('DB_DSN', 'mysql://root@localhost/my_app');
-		}
-
-		if (! defined('ERROR_TEMPLATE')) {
-			//define('ERROR_TEMPLATE', "page_error.tpl.php");
-		}
-
-
-		if (defined('DB_DSN')) {
-			self::$db = static::getDb('default', DB_DSN);
-			//self::$db->execute("set names utf8");
-		}
-
-	}
 
+	/* #### */
 
-	public function handle($request)
+	public static function boot()
 	{
-		try {
-			$response = new Response(200, [], null);
-			$pipe = new Pipe($this->middlewares);
-
-			$response = $pipe->next($request, $response);
-
-		} catch (\Exception $e) {
-            $error_code = $e->getCode();
-            $error_message = $e->getMessage();
-
-            error_log("[App] Error " . $error_code . " : " . $error_message);
 
-            $content = null;
-            if (ENV == 'dev') {
-                $title = "App CATCHED EXCEPTION CODE " . $error_code;
-                $message = '<pre>' . print_r($e, true) . '</pre>';
-                $content = '<title>' . $title . '</title><h1>' . $title . '</h1><h2>' . $error_message . '</h2><p>' . $message . '</p>';
-            }
+		define('FW_SRC_DIR', __DIR__);
+		define('FW_DIR', __DIR__ . "/..");
 
-            //throw $e;
-            $response->setStatus(500)->setHtml($content);
+		if (! defined('APP_DIR')) {
+			echo "ERROR: Please, define APP_DIR" . PHP_EOL;
+			exit(1);
 		}
 
-		return $response;
-	}
-
 
-	/* #### */
-
-	public static function boot()
-	{
 		if (defined('USE_HOOKS') && USE_HOOKS) {
 			HooksManager::applyHook('app.boot.before', []);
 		}
@@ -222,35 +120,17 @@ class App
 	}
 
 	
-	public function get($key, $default_value=null)
-	{
-		return isset($this->container[$key]) ? $this->container[$key] : $default_value;
-	}
 
-	public function set($key, $value)
-	{
-		$this->container[$key] = $value;
-	}
-
-    public function has($name)
-    {
-        return isset($this->container[$name]);
-    }
-
-
-    // DEPRECATED
-	public static function setData($key, $value=null)
+    public static function setData($key, $value=null)
 	{
 		self::$data[$key] = $value;
 	}
 
-	// DEPRECATED
 	public static function getData($key, $default_value=null)
 	{
 		return array_key_exists($key, self::$data) ? self::$data[$key] : $default_value;
 	}
 
-	// DEPRECATED
 	public static function hasData($key)
 	{
 		return array_key_exists($key, self::$data);
@@ -276,12 +156,11 @@ class App
 		}
 	}
 
-	protected static function loadHelpers($dir)
+	public static function loadHelpers($dir)
 	{
 		$helpers = glob($dir . '/helpers_*.php');
 
 		foreach ($helpers as $helper) {
-			//echo '<li>'.$helper.'</li>';
 			require $helper;
 		}
 
@@ -294,14 +173,6 @@ class App
 	}
 
 
-	public static function getApp()
-	{
-		if (isset(self::$instance)) {
-			return self::$instance;
-		}
-		throw new Exception("App is not instancied", 1);
-	}
-
 
 	public static function getDb($instance_name=null, $dsn=null)
 	{

+ 2 - 2
src/App/Middlewares/CacheHtml.php

@@ -2,8 +2,8 @@
 
 namespace KarmaFW\App\Middlewares;
 
-use \KarmaFW\App\Request;
-use \KarmaFW\App\Response;
+use \KarmaFW\Http\Request;
+use \KarmaFW\Http\Response;
 
 
 class CacheHtml

+ 2 - 4
src/App/Middlewares/CommandRouter.php

@@ -2,10 +2,8 @@
 
 namespace KarmaFW\App\Middlewares;
 
-use \KarmaFW\App\Request;
-use \KarmaFW\App\Response;
-use \KarmaFW\App\ResponseError404;
-use \KarmaFW\App\ResponseFile;
+use \KarmaFW\Http\Request;
+use \KarmaFW\Http\Response;
 
 
 class CommandRouter

+ 2 - 2
src/App/Middlewares/ErrorHandler.php

@@ -2,8 +2,8 @@
 
 namespace KarmaFW\App\Middlewares;
 
-use \KarmaFW\App\Request;
-use \KarmaFW\App\Response;
+use \KarmaFW\Http\Request;
+use \KarmaFW\Http\Response;
 
 
 class ErrorHandler

+ 2 - 3
src/App/Middlewares/ForceHttps.php

@@ -2,9 +2,8 @@
 
 namespace KarmaFW\App\Middlewares;
 
-use \KarmaFW\App\Request;
-use \KarmaFW\App\Response;
-use \KarmaFW\App\ResponseRedirect;
+use \KarmaFW\Http\Request;
+use \KarmaFW\Http\Response;
 
 
 class ForceHttps

+ 2 - 2
src/App/Middlewares/GzipEncoding.php

@@ -2,8 +2,8 @@
 
 namespace KarmaFW\App\Middlewares;
 
-use \KarmaFW\App\Request;
-use \KarmaFW\App\Response;
+use \KarmaFW\Http\Request;
+use \KarmaFW\Http\Response;
 
 
 class GzipEncoding

+ 2 - 3
src/App/Middlewares/MaintenanceMode.php

@@ -2,9 +2,8 @@
 
 namespace KarmaFW\App\Middlewares;
 
-use \KarmaFW\App\Request;
-use \KarmaFW\App\Response;
-use \KarmaFW\App\ResponseError;
+use \KarmaFW\Http\Request;
+use \KarmaFW\Http\Response;
 
 
 class MaintenanceMode

+ 2 - 2
src/App/Middlewares/MinimifierCss.php

@@ -2,8 +2,8 @@
 
 namespace KarmaFW\App\Middlewares;
 
-use \KarmaFW\App\Request;
-use \KarmaFW\App\Response;
+use \KarmaFW\Http\Request;
+use \KarmaFW\Http\Response;
 
 
 class MinimifierCss

+ 2 - 2
src/App/Middlewares/MinimifierHtml.php

@@ -2,8 +2,8 @@
 
 namespace KarmaFW\App\Middlewares;
 
-use \KarmaFW\App\Request;
-use \KarmaFW\App\Response;
+use \KarmaFW\Http\Request;
+use \KarmaFW\Http\Response;
 
 
 class MinimifierHtml

+ 2 - 2
src/App/Middlewares/MinimifierJs.php

@@ -2,8 +2,8 @@
 
 namespace KarmaFW\App\Middlewares;
 
-use \KarmaFW\App\Request;
-use \KarmaFW\App\Response;
+use \KarmaFW\Http\Request;
+use \KarmaFW\Http\Response;
 
 
 class MinimifierJs

+ 2 - 3
src/App/Middlewares/RedirectToDomain.php

@@ -2,9 +2,8 @@
 
 namespace KarmaFW\App\Middlewares;
 
-use \KarmaFW\App\Request;
-use \KarmaFW\App\Response;
-use \KarmaFW\App\ResponseRedirect;
+use \KarmaFW\Http\Request;
+use \KarmaFW\Http\Response;
 
 
 class RedirectToDomain

+ 2 - 2
src/App/Middlewares/ResponseTime.php

@@ -2,8 +2,8 @@
 
 namespace KarmaFW\App\Middlewares;
 
-use \KarmaFW\App\Request;
-use \KarmaFW\App\Response;
+use \KarmaFW\Http\Request;
+use \KarmaFW\Http\Response;
 
 
 class ResponseTime

+ 2 - 2
src/App/Middlewares/SessionHandler.php

@@ -2,8 +2,8 @@
 
 namespace KarmaFW\App\Middlewares;
 
-use \KarmaFW\App\Request;
-use \KarmaFW\App\Response;
+use \KarmaFW\Http\Request;
+use \KarmaFW\Http\Response;
 
 
 class SessionHandler

+ 2 - 2
src/App/Middlewares/TrafficLogger.php

@@ -2,8 +2,8 @@
 
 namespace KarmaFW\App\Middlewares;
 
-use \KarmaFW\App\Request;
-use \KarmaFW\App\Response;
+use \KarmaFW\Http\Request;
+use \KarmaFW\Http\Response;
 
 
 class TrafficLogger

+ 2 - 2
src/App/Middlewares/UrlPrefixRouter.php

@@ -2,8 +2,8 @@
 
 namespace KarmaFW\App\Middlewares;
 
-use \KarmaFW\App\Request;
-use \KarmaFW\App\Response;
+use \KarmaFW\Http\Request;
+use \KarmaFW\Http\Response;
 
 
 class UrlPrefixRouter

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

@@ -2,10 +2,8 @@
 
 namespace KarmaFW\App\Middlewares;
 
-use \KarmaFW\App\Request;
-use \KarmaFW\App\Response;
-use \KarmaFW\App\ResponseRedirect;
-use \KarmaFW\App\ResponseFile;
+use \KarmaFW\Http\Request;
+use \KarmaFW\Http\Response;
 use \KarmaFW\Routing\Router;
 
 

+ 2 - 2
src/App/Pipe.php

@@ -2,8 +2,8 @@
 
 namespace KarmaFW\App;
 
-//use \KarmaFW\App\Request;
-//use \KarmaFW\App\Response;
+use \KarmaFW\Http\Request;
+use \KarmaFW\Http\Response;
 
 
 // https://mnapoli.fr/presentations/forumphp-middlewares/

+ 24 - 0
src/App/Tools.php

@@ -0,0 +1,24 @@
+<?php
+
+namespace KarmaFW\App;
+
+
+class Tools
+{
+
+	public static function loadHelpers($dir)
+	{
+		$helpers = glob($dir . '/helpers_*.php');
+
+		foreach ($helpers as $helper) {
+			require $helper;
+		}
+	}
+
+
+	public static function isCli()
+	{
+		return (php_sapi_name() == 'cli');
+	}
+
+}

+ 1 - 1
src/App/Request.php → src/Http/Request.php

@@ -1,6 +1,6 @@
 <?php
 
-namespace KarmaFW\App;
+namespace KarmaFW\Http;
 
 use \KarmaFW\Routing\Route;
 

+ 1 - 1
src/App/Response.php → src/Http/Response.php

@@ -1,6 +1,6 @@
 <?php
 
-namespace KarmaFW\App;
+namespace KarmaFW\Http;
 
 use KarmaFW\App;
 

+ 4 - 1
src/App/ResponseFile.php → src/Http/ResponseFile.php

@@ -1,10 +1,13 @@
 <?php
 
-namespace KarmaFW\App;
+namespace KarmaFW\Http;
 
 use KarmaFW\App;
 
 
+throw new \Exception("DEPRECATED", 1);
+
+
 class ResponseFile extends Response
 {
 	protected $file_path;

+ 0 - 0
src/App/ResponseRedirect.php → src/Http/ResponseRedirect.php


+ 209 - 0
src/Kernel.php

@@ -0,0 +1,209 @@
+<?php
+
+namespace KarmaFW;
+
+use \KarmaFW\App\Container;
+use \KarmaFW\App\Pipe;
+use \KarmaFW\App\Tools;
+use \KarmaFW\Database\Sql\SqlDb;
+use \KarmaFW\Http\Request;
+use \KarmaFW\Http\Response;
+
+
+define('FW_SRC_DIR', __DIR__);
+define('FW_DIR', __DIR__ . "/..");
+
+if (! defined('APP_DIR')) {
+	echo "ERROR: Please, define APP_DIR" . PHP_EOL;
+	exit(1);
+}
+
+
+class Kernel
+{
+	protected static $booted = false;
+	protected static $helpers_dirs = [
+		APP_DIR . "/src/helpers",
+		FW_SRC_DIR . "/helpers",
+	];
+
+	protected $db = null;
+
+	protected $middlewares;
+	protected $container;
+
+
+	public function __construct($middlewares=[])
+	{
+		$this->middlewares = $middlewares;
+		$this->container = new Container;
+
+		try {
+			$this->configure();
+
+		} catch (\Exception $e) {
+			header("HTTP/1.0 500 Internal Server Error");
+			echo "<h1>Internal Server error</h1>";
+
+			if (ENV === 'dev') {
+				echo "<pre>";
+				print_r($e);
+				echo "</pre>";
+			}
+			exit;
+		}
+
+	}
+
+
+	public function configure()
+	{
+		if (is_file(APP_DIR . '/config/config.php')) {
+			require APP_DIR . '/config/config.php';
+		}
+
+
+		if (! defined('APP_NAME')) {
+			define('APP_NAME', "PHP Application");
+		}
+
+		if (! defined('TPL_DIR')) {
+			//define('TPL_DIR', APP_DIR . '/templates');
+		}
+
+		if (! defined('ENV')) {
+			$env = defined('ENVIRONMENT') ? ENVIRONMENT : 'prod';
+			define('ENV', $env);
+		}
+
+		if (! defined('DB_DSN')) {
+			//define('DB_DSN', 'mysql://root@localhost/my_app');
+		}
+
+		if (! defined('ERROR_TEMPLATE')) {
+			//define('ERROR_TEMPLATE', "page_error.tpl.php");
+		}
+
+
+		if (defined('DB_DSN')) {
+			//$this->db = static::getDb('default', DB_DSN);
+			$this->db = $this->connectDb('default', DB_DSN);
+		}
+
+
+		// Load helpers
+		Tools::loadHelpers(APP_DIR . '/src/helpers');
+		Tools::loadHelpers(FW_DIR . '/src/helpers');
+	}
+
+
+
+	/* MAIN APP PROCESS */
+
+	public function handle(Request $request)
+	{
+		try {
+			$response = new Response(200, [], null);
+			$pipe = new Pipe($this->middlewares);
+
+			$response = $pipe->next($request, $response);
+
+		} catch (\Exception $e) {
+            $error_code = $e->getCode();
+            $error_message = $e->getMessage();
+
+            error_log("[App] Error " . $error_code . " : " . $error_message);
+
+            $content = null;
+            if (ENV == 'dev') {
+                $title = "App CATCHED EXCEPTION CODE " . $error_code;
+                $message = '<pre>' . print_r($e, true) . '</pre>';
+                $content = '<title>' . $title . '</title><h1>' . $title . '</h1><h2>' . $error_message . '</h2><p>' . $message . '</p>';
+            }
+
+            //throw $e;
+            $response->setStatus(500)->setHtml($content);
+		}
+
+		return $response;
+	}
+
+    
+
+    /* CONTAINER */
+
+
+	// GET an element from the container
+	public function get($key, $default_value=null)
+	{
+		return isset($this->container[$key]) ? $this->container[$key] : $default_value;
+	}
+
+	// STORE an element to the container
+	public function set($key, $value=null)
+	{
+		if (is_array($key)) {
+			foreach ($key as $k => $v) {
+				$this->set($k, $v);
+			}
+			
+		} else {
+			$this->container[$key] = $value;
+		}
+		return $this;
+	}
+
+    public function has($name)
+    {
+        return isset($this->container[$name]);
+    }
+
+
+
+    /* DATABASE */
+
+	public function connectDb($instance_name=null, $dsn=null)
+	{
+		/*
+		$dsn = 'mysql://user:pass@localhost/my_app';
+		*/
+		static $instances = [];
+		//static $last_instance_name = null;
+
+		if (empty($instance_name)) {
+			if (! empty($this->db)) {
+				return $this->db;
+			}
+
+			$instance_name = 'default';
+
+			//if (! empty($last_instance_name)) {
+			//	$instance_name = $last_instance_name;
+			//}
+		}
+
+		//$last_instance_name = $instance_name;
+
+		if (empty($instances[$instance_name])) {
+			if (empty($dsn) && defined('DB_DSN')) {
+				$dsn = DB_DSN;
+			}
+			$instances[$instance_name] = new SqlDb($dsn);
+		}
+
+		return $instances[$instance_name];
+	}
+
+    public function getDb()
+    {
+    	return $this->db;
+    }
+
+    public function setDb($db)
+    {
+    	$this->db = $db;
+    	return $this;
+    }
+
+}
+

+ 2 - 2
src/Routing/Controllers/AppController.php

@@ -3,8 +3,8 @@
 namespace KarmaFW\Routing\Controllers;
 
 use \KarmaFW\App;
-use \KarmaFW\App\Request;
-use \KarmaFW\App\Response;
+use \KarmaFW\Http\Request;
+use \KarmaFW\Http\Response;
 use \KarmaFW\Lib\Hooks\HooksManager;
 
 

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

@@ -3,8 +3,8 @@
 namespace KarmaFW\Routing\Controllers;
 
 use \KarmaFW\WebApp;
-use \KarmaFW\App\Request;
-use \KarmaFW\App\Response;
+use \KarmaFW\Http\Request;
+use \KarmaFW\Http\Response;
 use \KarmaFW\Lib\Hooks\HooksManager;
 
 

+ 2 - 2
src/Routing/Router.php

@@ -3,8 +3,8 @@
 namespace KarmaFW\Routing;
 
 use \KarmaFW\WebApp;
-use \KarmaFW\App\Request;
-use \KarmaFW\App\Response;
+use \KarmaFW\Http\Request;
+use \KarmaFW\Http\Response;
 
 
 class Router