Browse Source

dev 2020-09

Max F 5 years ago
parent
commit
5fd91032bf
7 changed files with 190 additions and 4 deletions
  1. 1 1
      Readme.md
  2. 16 3
      src/App.php
  3. 55 0
      src/ConsoleApp.md
  4. 54 0
      src/ConsoleApp.php
  5. 5 0
      src/Readme.md
  6. 29 0
      src/Routing/Readme.md
  7. 30 0
      src/Routing/Router.php

+ 1 - 1
Readme.md

@@ -171,9 +171,9 @@ $whoops->register();
 require APP_DIR . '/config/routes.php';
 
 
-\KarmaFW\WebApp::registerHelpersDir(APP_DIR . '/src/helpers');
 
 // APP BOOT
+\KarmaFW\WebApp::registerHelpersDir(APP_DIR . '/src/helpers');
 \KarmaFW\WebApp::boot();
 
 

+ 16 - 3
src/App.php

@@ -33,8 +33,8 @@ class App
 
 		// move fw_helpers at the end of the list (to be loaded the last one)
 		if (count(self::$helpers_dirs) > 1) {
-		$fw_helpers = array_shift(self::$helpers_dirs);
-		self::$helpers_dirs[] = $fw_helpers;
+			$fw_helpers = array_shift(self::$helpers_dirs);
+			self::$helpers_dirs[] = $fw_helpers;
 		}
 
 		// include helpers
@@ -70,7 +70,20 @@ class App
 
 	public static function registerHelpersDir($dir)
 	{
-		self::$helpers_dirs[] = $dir;
+		$dir = rtrim($dir, '/');
+		if (! in_array($dir, self::$helpers_dirs)) {
+			self::$helpers_dirs[] = $dir;
+		}
+	}
+
+
+	public static function unregisterHelpersDir($dir)
+	{
+		$dir = rtrim($dir, '/');
+		$k = array_search($dir, self::$helpers_dirs);
+		if ($k !== false) {
+			unset(self::$helpers_dirs[$k]);
+		}
 	}
 
 

+ 55 - 0
src/ConsoleApp.md

@@ -0,0 +1,55 @@
+
+
+
+## Structure d'une [app console](src/)
+
+```
+bin
+    app_console.php
+config
+    config.php
+src
+    Models
+    helpers
+vendor
+```
+
+
+
+
+## Console command
+```
+mkdir -p bin
+nano bin/app_console.php
+```
+
+```
+<?php
+
+// CONFIG
+define('APP_DIR', realpath(__DIR__ . '/..'));
+define('VENDOR_DIR', realpath(__DIR__ . '/../vendor'));
+
+require APP_DIR . '/config/config.php';
+
+
+// AUTOLOAD
+$loader = require VENDOR_DIR . '/autoload.php';
+$loader->setPsr4('MyApp\\', __DIR__ . '/../src');
+
+
+
+
+// APP BOOT
+\KarmaFW\ConsoleApp::registerHelpersDir(APP_DIR . '/src/helpers');
+\KarmaFW\ConsoleApp::boot();
+
+
+// YOUR INIT CODE HERE
+\KarmaFW\ConsoleApp::getDb()->execute("set names utf8");
+
+
+// APP ROUTE
+\KarmaFW\ConsoleApp::routeFromArgs();
+
+```

+ 54 - 0
src/ConsoleApp.php

@@ -0,0 +1,54 @@
+<?php
+
+namespace KarmaFW;
+
+use \KarmaFW\Routing\Router;
+use \KarmaFW\Lib\Hooks\HooksManager;
+
+
+class ConsoleApp 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('consoleapp.boot.before', []);
+		}
+
+
+		if (defined('USE_HOOKS') && USE_HOOKS) {
+			HooksManager::applyHook('consoleapp.boot.after', []);
+		}
+
+	}
+
+
+	public static function routeFromArgs($argv=[])
+	{
+		if (! self::$booted) {
+			self::boot();
+		}
+
+		if (defined('USE_HOOKS') && USE_HOOKS) {
+			HooksManager::applyHook('consoleapp.route.before', []);
+		}
+
+		$bin_path = array_shift($argv);
+
+		print_r($argv); // TODO: call defined class/method
+
+
+		if (defined('USE_HOOKS') && USE_HOOKS) {
+			HooksManager::applyHook('consoleapp.route.after', []);
+		}
+
+
+	}
+
+
+}

+ 5 - 0
src/Readme.md

@@ -17,6 +17,11 @@ public static function registerHelpersDir($dir)
 # Add a directory to the helpers directories
 ```
 
+```
+public static function unregisterHelpersDir($dir)
+# Remove a directory from the helpers directories
+```
+
 ```
 public static function loadHelpers($dir)
 # Load all helpers files in a directory

+ 29 - 0
src/Routing/Readme.md

@@ -0,0 +1,29 @@
+
+```
+use \KarmaFW\Routing\Router;
+
+
+// Homepage
+Router::get('/', ['App\\Controllers\\PublicController', 'homepage'])->setName('home');
+
+
+// login/logout
+Router::get('/logout', ['App\\Controllers\\PublicController', 'logout'])->setName('logout');
+Router::get('/login', ['App\\Controllers\\PublicController', 'login'])->setName('login');
+Router::post('/login', ['App\\Controllers\\PublicController', 'login_post']);
+
+
+// clients (example)
+Router::get('/clients', ['App\\Controllers\\ClientController', 'clients_list'])->setName('clients_list');
+Router::get('/clients/nouveau-client', ['App\\Controllers\\ClientController', 'client_new'])->setName('client_new');
+Router::get('/clients/([0-9]+)-([^/]+)$', ['App\\Controllers\\ClientController', 'client_edit'], 'regex', ['client_id', 'slug'])->setName('client_edit');
+Router::post('/clients/save-client', ['App\\Controllers\\ClientController', 'client_save'])->setName('client_save');
+Router::delete('/clients/delete-client', ['App\\Controllers\\ClientController', 'client_delete'])->setName('client_delete');
+
+
+
+// 404
+Router::get('.*', ['App\\Controllers\\ErrorController', 'error404']);
+
+```
+

+ 30 - 0
src/Routing/Router.php

@@ -75,6 +75,36 @@ class Router
 		return self::Add('POST', $url_match, $callback, $type_match, $regex_params);
 	}
 
+	// DELETE method
+	public static function delete($url_match, $callback=null, $type_match='exact', $regex_params=[])
+	{
+		return self::Add('DELETE', $url_match, $callback, $type_match, $regex_params);
+	}
+
+	// PUT method
+	public static function put($url_match, $callback=null, $type_match='exact', $regex_params=[])
+	{
+		return self::Add('PUT', $url_match, $callback, $type_match, $regex_params);
+	}
+
+	// HEAD method
+	public static function head($url_match, $callback=null, $type_match='exact', $regex_params=[])
+	{
+		return self::Add('HEAD', $url_match, $callback, $type_match, $regex_params);
+	}
+
+	// PATCH method
+	public static function patch($url_match, $callback=null, $type_match='exact', $regex_params=[])
+	{
+		return self::Add('PATCH', $url_match, $callback, $type_match, $regex_params);
+	}
+
+	// OPTIONS method
+	public static function options($url_match, $callback=null, $type_match='exact', $regex_params=[])
+	{
+		return self::Add('OPTIONS', $url_match, $callback, $type_match, $regex_params);
+	}
+
 
 	// Lookup the first matching route then execute it 
 	public static function routeByUrl($request_method, $request_uri, $debug = false)