Browse Source

dev 2019-11

Max YO 6 years ago
parent
commit
46d263c288

+ 132 - 0
Readme.md

@@ -0,0 +1,132 @@
+
+# Nouveau projet
+
+
+## 0) se positionner dans le dossier du projet
+
+## 1) lancer `composer init`
+
+## 2) modifier composer.json
+
+```
+# Ajouter ceci dans composer.json
+
+{
+    "repositories": [
+        {
+            "type": "path",
+            "url": "/put/here/the/path/to/karmafw"
+        }
+    ],
+    "require": {
+        "karmasolutions/karmafw": "dev-master",
+        "filp/whoops": "^2.2@dev"
+    }
+}
+```
+
+
+## 3) créer le dossier public et le fichier public/index.php et le remplir avec ceci :
+```
+<?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('App\\', __DIR__ . '/../src');
+
+
+// ERRORS HANDLER
+$whoops = new \Whoops\Run;
+$whoops->prependHandler(new \Whoops\Handler\PrettyPageHandler);
+$whoops->register();
+
+
+// ROUTE
+require APP_DIR . '/config/routes.php';
+
+
+// APP BOOT & ROUTE
+\KarmaFW\App::boot();
+\KarmaFW\App::route();
+
+```
+
+
+## 4) créer le dossier config
+
+## 4a) créer le fichier config/config.php et le remplir avec ceci :
+```
+<?php
+
+ini_set('display_errors', 1);
+
+define('TPL_DIR', APP_DIR . '/templates');
+
+```
+
+## 4b) créer le fichier config/routes.php et le remplir avec ceci :
+```
+<?php
+
+namespace App\config;
+
+use \KarmaFW\Routing\Router;
+
+
+// Homepage
+Router::get('/', ['App\\Controllers\\AppController', 'homepage'])->setName('home');
+
+```
+
+## 5) Homepage controller : src/Controllers/AppController.php
+```
+<?php
+
+namespace App\Controllers;
+
+use \KarmaFW\App;
+use \KarmaFW\Routing\Controllers\WebController;
+use \KarmaFW\Templates\Templater;
+use \KarmaFW\Database\Sql\SqlDb;
+
+
+class AppController extends WebController
+{
+
+	public function homepage()
+	{
+		$db = App::getDb();
+		$db->connect();
+
+		$rs = $db->execute('show databases');
+		$databases = $rs->fetchAll();
+
+		$templater = new Templater();
+		$templater->assign('databases', $databases);
+		$templater->display('homepage.tpl.php');
+	}
+	
+}
+
+```
+
+
+## 6) Homepage template : templates/homepage.tpl.php
+```
+<html>
+<body>
+<h1>hello world</h1>
+<pre>
+<?php print_r($databases); ?>
+</pre>
+</body>
+</html>
+
+```

+ 13 - 6
src/App.php

@@ -24,13 +24,13 @@ class App
 
 	public static function boot()
 	{
-		HooksManager::applyHook('fw_app_boot__before', []);
+		HooksManager::applyHook('app_boot__before', []);
 
 		// include helpers
 		self::loadHelpers(FW_SRC_DIR . "/../helpers");
 
 		self::$booted = true;
-		HooksManager::applyHook('fw_app_boot__after', []);
+		HooksManager::applyHook('app_boot__after', []);
 	}	
 
 
@@ -52,31 +52,38 @@ class App
 
 		// 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('fw_bootstrap_404', []);
+			HooksManager::applyHook('app_route_404', []);
 			errorHttp(404, 'Warning: route callback is not callable', '404 Not Found');
 			exit(1);
 
-		} else if ($route === true) {
+		} else if ($route === 0) {
 			// route found but no callback defined
-			HooksManager::applyHook('fw_bootstrap_404', []);
+			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('fw_bootstrap_404', []);
+			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);
 		}
 

+ 47 - 5
src/Database/Sql/SqlDb.php

@@ -25,6 +25,11 @@ class SqlDb
 			$driver_class = $this->getDriverClass($driver_name);
 		}
 
+		if (empty($driver_class)) {
+			throw new \Exception("No SQL driver specified. Please define DB_DSN with something like 'mysql://root:pass@localhost/my_app'", 1);
+		}
+
+
 		$this->driver = new $driver_class($this, $credentials);
 
 	}
@@ -46,7 +51,7 @@ class SqlDb
 			'mysql' => \KarmaFW\Database\Sql\Drivers\Mysqli\MySqliDriver::class,
 			'sqlite' => \KarmaFW\Database\Sql\Drivers\Mysqli\SqliteDriver::class,
 		];
-		return isset($available_drivers[$driver_name]) ? $available_drivers[$driver_name] : null;
+		return (! empty($driver_name) && isset($available_drivers[$driver_name])) ? $available_drivers[$driver_name] : null;
 	}
 
 
@@ -68,14 +73,14 @@ class SqlDb
 
 
 
-	public function createQuery($query=null)
+	public function createQuery($sql=null)
 	{
-		return new SqlQuery($this, $query);
+		return new SqlQuery($this, $sql);
 	}
 
 
 
-	public function getTable($table_name) : SqlTable
+	public function getTable($table_name) /* : SqlTable */
 	{
 		return new SqlTable($this, $table_name);
 	}
@@ -104,6 +109,43 @@ class SqlDb
 		return $this->createQuery()->execute($sql, $params);
 	}
 
+	public function query($sql, $params=[])
+	{
+		// Alias of executeSelectAll
+		return $this->executeSelectAll($sql, $params);
+	}
+	
+	public function executeSelect($sql, $params=[])
+	{
+		// Alias of executeSelectAll
+		return $this->executeSelectAll($sql, $params);
+	}
+
+	public function executeSelectAll($sql, $params=[])
+	{
+		return $this->createQuery()->executeSelectAll($sql, $params);
+	}
+
+	public function executeSelectOne($sql, $params=[])
+	{
+		return $this->createQuery()->executeSelectOne($sql, $params);
+	}
+
+	public function executeInsert($query, $params=[])
+	{
+		return $this->createQuery()->executeInsert($sql, $params);
+	}
+
+	public function executeUpdate($query, $params=[])
+	{
+		return $this->createQuery()->executeUpdate($sql, $params);
+	}
+	
+	public function executeDelete($query, $params=[])
+	{
+		return $this->createQuery()->executeDelete($sql, $params);
+	}
+
 
 	public function getInsertId()
 	{
@@ -121,7 +163,7 @@ class SqlDb
 
 	// DATABASE
 
-	public function use($database_name)
+	public function useDatabase($database_name)
 	{
 		return $this->schema->useDatabase($database_name);
 	}

+ 9 - 9
src/Database/Sql/SqlSchema.php

@@ -15,7 +15,7 @@ class SqlSchema
 
 	/* DATABASES */
 
-	public function useDatabase($database_name) : bool
+	public function useDatabase($database_name) /* : bool */
 	{
 		$sql = "use " . $database_name;
 		$ret = $this->db->createQuery()->execute($sql);
@@ -23,7 +23,7 @@ class SqlSchema
 	}
 
 
-	public function dropDatabase($database_name, $if_exists=false) : bool
+	public function dropDatabase($database_name, $if_exists=false) /* : bool */
 	{
 		if ($if_exists) {
 			$sql = "drop database if exists " . $database_name;
@@ -36,7 +36,7 @@ class SqlSchema
 	}
 
 
-	public function createDatabase($database_name, $if_not_exists=false) : bool
+	public function createDatabase($database_name, $if_not_exists=false) /* : bool */
 	{
 		if ($if_not_exists) {
 			$sql = "create database if not exists " . $database_name;
@@ -49,7 +49,7 @@ class SqlSchema
 	}
 
 
-	public function listDatabases($database=null) : array
+	public function listDatabases($database=null) /* : array */
 	{
 		$sql = "show databases";
 
@@ -68,7 +68,7 @@ class SqlSchema
 
 	/* TABLES */
 
-	public function createTable($table_name, array $columns, array $indexes=[], $if_not_exists=false) : bool
+	public function createTable($table_name, array $columns, array $indexes=[], $if_not_exists=false) /* : bool */
 	{
 		if ($if_not_exists) {
 			$sql = "create table if not exists " . $table_name . " (" . PHP_EOL;
@@ -99,7 +99,7 @@ class SqlSchema
 
 
 
-	public function dropTable($table_name, $if_exists=false) : bool
+	public function dropTable($table_name, $if_exists=false) /* : bool */
 	{
 		if ($if_exists) {
 			$sql = "drop table if exists " . $table_name;
@@ -112,7 +112,7 @@ class SqlSchema
 	}
 
 
-	public function listTables($table=null, $database=null) : array
+	public function listTables($table=null, $database=null) /* : array */
 	{
 		$sql = "show tables";
 
@@ -135,7 +135,7 @@ class SqlSchema
 
 	/* COLUMNS */
 
-	public function listTableColumns($table, $column=null) : array
+	public function listTableColumns($table, $column=null) /* : array */
 	{
 		$sql = "show columns from " . $table;
 
@@ -152,7 +152,7 @@ class SqlSchema
 	}
 
 
-	public function listTableIndexes($table) : array
+	public function listTableIndexes($table) /* : array */
 	{
 		$sql = "show indexes from " . $table;
 

+ 7 - 7
src/Database/Sql/SqlTable.php

@@ -29,7 +29,7 @@ class SqlTable
 	}
 
 
-	public function listColumns() : array
+	public function listColumns() /* : array */
 	{
 		$schema = new SqlSchema($this->db);
 		$this->columns = $schema->listTableColumns($table, $column=null);
@@ -37,7 +37,7 @@ class SqlTable
 	}
 
 
-	public function insert($values=[], $options=[]) : int
+	public function insert($values=[], $options=[]) /* : int */
 	{
 		$this->insertAll([$values], $options);
 		return $this->db->getInsertId();
@@ -73,7 +73,7 @@ class SqlTable
 	}
 
 
-	public function update(array $updates=[], array $where=[], $options=[]) : int
+	public function update(array $updates=[], array $where=[], $options=[]) /* : int */
 	{
 		$limit_sql = (isset($options['limit']) && ! is_null($options['limit'])) ? ("limit " . $options['limit']) : "";
 
@@ -85,7 +85,7 @@ class SqlTable
 	}
 
 
-	public function delete(array $where=[], $options=[]) : int
+	public function delete(array $where=[], $options=[]) /* : int */
 	{
 		$limit_sql = isset($options['limit']) ? ("limit " . $options['limit']) : "";
 
@@ -108,7 +108,7 @@ class SqlTable
 		return $this->getAll($where, $options);
 	}
 
-	public function getAll($where=null, $options=[]) : array
+	public function getAll($where=null, $options=[]) /* : array */
 	{
 		//return $this->db->createQuery()->tableSelect($this->table_name, $where, $options)->fetchAll();
 
@@ -123,14 +123,14 @@ class SqlTable
 		return $this->getOne($where, $options);
 	}
 
-	public function getOne($where=null, $options=[]) : array
+	public function getOne($where=null, $options=[]) /* : array */
 	{
 		$options['limit'] = 1;
 		return $this->getAll($where, $options)->fetchOne();
 	}
 
 
-	public function buildQuery($where=null, $options=[]) : string
+	public function buildQuery($where=null, $options=[]) /* : string */
 	{
 		$limit_sql = isset($options['limit']) ? ("limit " . $options['limit']) : "";
 		$order_by_sql = isset($options['order_by']) ? ("order by " . $options['order_by']) : "";

+ 2 - 2
src/Routing/Router.php

@@ -8,7 +8,7 @@ class Router
 	private static $routes = [];
 
 
-	// Add a route to the router
+	// Register a route in the router
 	public static function add($methods, $url_match, $callback=null, $type_match='exact', $regex_params=[])
 	{
 		$route = new Route();
@@ -68,7 +68,7 @@ class Router
 				$callback = $route->getCallback();
 				if (empty($callback)) {
 					// Do nothing
-					return false;
+					return 0;
 
 				} else if (is_callable($callback)) {
 					self::routeRun($route, $callback, $request_method, $request_uri, $match_params);

+ 1 - 1
src/Templates/Templater.php

@@ -39,7 +39,7 @@ class Templater
 		}
 
 		if (empty($tpl_dirs)) {
-			throw new \Exception("No Templates dir", 1);
+			throw new \Exception("No Templates dir. Please define TPL_DIR with a valid directory path.", 1);
 		}
 
 		$tpl_path = false;