From dc078a59f89fa4e7196c6b25a5b2f043614f8974 Mon Sep 17 00:00:00 2001 From: fkeloks Date: Tue, 15 Aug 2017 12:48:45 +0200 Subject: [PATCH] Correctifs mineur sur l'ajout d'une page Database pour l'exemple --- app/controllers/DbController.php | 33 ++++++++++++++++++++ app/controllers/dbController.php | 27 ---------------- app/models/DbModel.php | 34 ++++++++++++++++++++ app/models/dbModel.php | 24 --------------- app/views/db.twig | 20 ++++++------ app/views/errors/error500.twig | 4 +-- config/config.php | 10 +++--- config/router.php | 8 ++--- core/Database/Database.php | 48 +++++++++++------------------ core/Database/DatabaseException.php | 5 +-- core/Functions/Functions.php | 2 +- public/index.php | 2 +- storage/databases/db_mysql.sql | 4 +++ storage/databases/db_postgresql.sql | 4 +++ 14 files changed, 117 insertions(+), 108 deletions(-) create mode 100644 app/controllers/DbController.php delete mode 100644 app/controllers/dbController.php create mode 100644 app/models/DbModel.php delete mode 100644 app/models/dbModel.php diff --git a/app/controllers/DbController.php b/app/controllers/DbController.php new file mode 100644 index 0000000..a4860d0 --- /dev/null +++ b/app/controllers/DbController.php @@ -0,0 +1,33 @@ +app = new \App\Models\DbModel(); + } + + /** + * @return void + */ + public function index() { + $this->render('db.twig', [ + 'bookmarks' => $this->app->bookmarkList() + ]); + } + +} \ No newline at end of file diff --git a/app/controllers/dbController.php b/app/controllers/dbController.php deleted file mode 100644 index d2e6f71..0000000 --- a/app/controllers/dbController.php +++ /dev/null @@ -1,27 +0,0 @@ -app = new \App\Models\dbModel(); - } - - - public function index() - { - $this->render('db.twig', ['bookmarks' => $this->app->bookmarkList()]); - } - -} \ No newline at end of file diff --git a/app/models/DbModel.php b/app/models/DbModel.php new file mode 100644 index 0000000..60e8fc5 --- /dev/null +++ b/app/models/DbModel.php @@ -0,0 +1,34 @@ +database = new \BDSCore\Database\Database(); + } + + /** + * @return array + */ + public function bookmarkList() { + $sql = 'SELECT id, url, nom FROM bookmark ORDER BY id ASC'; + $result = $this->database->fetchAll($sql); + + return $result; + } + +} \ No newline at end of file diff --git a/app/models/dbModel.php b/app/models/dbModel.php deleted file mode 100644 index f215eb5..0000000 --- a/app/models/dbModel.php +++ /dev/null @@ -1,24 +0,0 @@ -database = new BDSCore\Database\Database(); - } - - - public function bookmarkList() - { - $sql = 'SELECT id, url, nom FROM bookmark ORDER BY id ASC'; - - $result = $this->database->fetchAll($sql); - - return $result; - } - -} \ No newline at end of file diff --git a/app/views/db.twig b/app/views/db.twig index c65c2bf..4583ce9 100644 --- a/app/views/db.twig +++ b/app/views/db.twig @@ -4,17 +4,17 @@ {% block body %} -
-

Test database

-
+
+

Test database

+
-
-{% for key, bookmark in bookmarks %} +
+ {% for key, bookmark in bookmarks %} -#{{ bookmark.id }} {{ bookmark.nom }}
+ #{{ bookmark.id }} {{ bookmark.nom }} +
-{% endfor %} -
- -{% endblock %} + {% endfor %} +
+{% endblock %} \ No newline at end of file diff --git a/app/views/errors/error500.twig b/app/views/errors/error500.twig index 9b7e0ea..792570e 100644 --- a/app/views/errors/error500.twig +++ b/app/views/errors/error500.twig @@ -6,9 +6,9 @@ -
+

Error 500 : Internal server error

- + {% if(exception == false) %} Unexpected error...
An error occurred and your request couldn't be completed. Please try again. diff --git a/config/config.php b/config/config.php index 21b5246..44958b3 100644 --- a/config/config.php +++ b/config/config.php @@ -15,12 +15,12 @@ 'routerCache' => false, 'twigViews' => 'app/views', - 'twigCache' => false, //'cache/twig', + 'twigCache' => 'cache/twig', - 'db_driver' => 'postgresql', + 'db_driver' => 'mysql', 'db_host' => 'localhost', - 'db_name' => 'framework', - 'db_username' => 'stephane', - 'db_password' => 'stephane', + 'db_name' => 'BDS_Framework', + 'db_username' => 'root', + 'db_password' => '', ]; \ No newline at end of file diff --git a/config/router.php b/config/router.php index f02c52f..d1b2217 100644 --- a/config/router.php +++ b/config/router.php @@ -9,10 +9,10 @@ ], 'routes' => [ - 'homePage' => ['get', '/', 'homeController@index'], - 'helloPage' => ['get', '/hello/{name:\w+}', 'helloController@index'], - 'testPage' => ['get', '/test', 'testController@index'], - 'dbPage' => ['get', '/db', 'dbController@index'], + 'homePage' => ['get', '/', 'HomeController@index'], + 'helloPage' => ['get', '/hello/{name:\w+}', 'HelloController@index'], + 'testPage' => ['get', '/test', 'TestController@index'], + 'dbPage' => ['get', '/database', 'DbController@index'], ] ]; \ No newline at end of file diff --git a/core/Database/Database.php b/core/Database/Database.php index 7ce4be0..e99f9fe 100644 --- a/core/Database/Database.php +++ b/core/Database/Database.php @@ -2,6 +2,8 @@ namespace BDSCore\Database; +use BDSCore\Config\Config; + /** * Class Database * @package BDSCore\Database @@ -14,61 +16,54 @@ class Database */ private $pdo; - /** * Database constructor. * @param string|null $driver * @param string|null $databaseName * @throws DatabaseException */ - public function __construct() - { + public function __construct() { return $this->connect(); } - /** * @param $driver * @param $databaseName * @return \PDO * @throws DatabaseException */ - public function connect() - { + public function connect() { + $config = Config::getAllConfig(); $params = [ - 'driver' => \BDSCore\Config\Config::getConfig('db_driver'), - 'hostname' => \BDSCore\Config\Config::getConfig('db_host'), - 'database' => \BDSCore\Config\Config::getConfig('db_name'), - 'username' => \BDSCore\Config\Config::getConfig('db_username'), - 'password' => \BDSCore\Config\Config::getConfig('db_password') + 'driver' => $config['db_driver'], + 'hostname' => $config['db_host'], + 'database' => $config['db_name'], + 'username' => $config['db_username'], + 'password' => $config['db_password'] ]; - if ($params['driver'] == 'sqlite') - { + if ($params['driver'] == 'sqlite') { if ($params['database'] == null) { throw new DatabaseException('The name of the database must be specified'); } - $this->pdo = new \PDO("sqlite:./storage/databases/{$params['database']}.sqlite"); - } - elseif ($params['driver'] == 'mysql') - { + Config::getDirectoryRoot(); + $this->pdo = new \PDO( + 'sqlite:' . Config::getDirectoryRoot("/storage/databases/{$params['database']}.sqlite") + ); + } elseif ($params['driver'] == 'mysql') { try { $this->pdo = new \PDO("mysql:host={$params['hostname']};dbname={$params['database']};charset=UTF8", $params['username'], $params['password']); } catch (\Exception $e) { throw new DatabaseException($e->getMessage()); } - } - elseif ($params['driver'] == 'postgresql') - { + } elseif ($params['driver'] == 'postgresql') { try { $this->pdo = new \PDO("pgsql:dbname={$params['database']};host={$params['hostname']}", $params['username'], $params['password']); } catch (\DatabaseException $e) { throw new DatabaseException($e->getMessage()); } - } - else - { + } else { throw new DatabaseException('Use of an unknown driver name in the \BDSCore\Database() class.'); } @@ -78,7 +73,6 @@ public function connect() return $this->pdo; } - /** * @param string $query * @param array $params @@ -100,7 +94,6 @@ public function query(string $query, array $params = [], string $entity = null) return $query; } - /** * @param string $query * @param array $params @@ -111,7 +104,6 @@ public function fetch(string $query, array $params = [], string $entity = null) return $this->query($query, $params, $entity)->fetch(); } - /** * @param string $query * @param array $params @@ -122,7 +114,6 @@ public function fetchAll(string $query, array $params = [], string $entity = nul return $this->query($query, $params, $entity)->fetchAll(); } - /** * @param string $query * @param array $params @@ -132,7 +123,6 @@ public function fetchColumn(string $query, array $params = [], string $entity = return $this->query($query, $params, $entity)->fetchColumn(); } - /** * @return int */ @@ -140,7 +130,6 @@ public function lastInsertId(): int { return $this->pdo->lastInsertId(); } - /** * @param string|null $tableName * @param $collumns @@ -165,7 +154,6 @@ public function insert(string $tableName = null, $collumns, $values) { } } - /** * @param string|null $tableName * @param null $collums diff --git a/core/Database/DatabaseException.php b/core/Database/DatabaseException.php index bc3ebf2..4b2d316 100644 --- a/core/Database/DatabaseException.php +++ b/core/Database/DatabaseException.php @@ -8,8 +8,5 @@ */ class DatabaseException extends \Exception { - public function test() - { - return 'ERROR CONNEXIOn DATABASE'; - } + } \ No newline at end of file diff --git a/core/Functions/Functions.php b/core/Functions/Functions.php index 2820c45..dee3ca6 100644 --- a/core/Functions/Functions.php +++ b/core/Functions/Functions.php @@ -5,7 +5,7 @@ * @return bool */ function debug($item): bool { - \BDSCore\Debug\debugBar::pushElement('Debug#' . substr(uniqid(), 8), $item); + \BDSCore\Debug\DebugBar::pushElement('Debug#' . substr(uniqid(), 8), $item); return \BDSCore\Debug\Debugger::debug($item); } diff --git a/public/index.php b/public/index.php index f836307..84747a1 100644 --- a/public/index.php +++ b/public/index.php @@ -26,6 +26,6 @@ set_error_handler([$app, 'catchException']); set_exception_handler([$app, 'catchException']); -require('../core/functions/Functions.php'); +require('../core/Functions/Functions.php'); $app->run($request, $response, $timeStart); \ No newline at end of file diff --git a/storage/databases/db_mysql.sql b/storage/databases/db_mysql.sql index e903199..7bed2cb 100644 --- a/storage/databases/db_mysql.sql +++ b/storage/databases/db_mysql.sql @@ -1,4 +1,8 @@ +/* + BDS Framework, For example only +*/ + CREATE DATABASE framework; CREATE TABLE bookmark diff --git a/storage/databases/db_postgresql.sql b/storage/databases/db_postgresql.sql index 4b52b01..3211cc3 100644 --- a/storage/databases/db_postgresql.sql +++ b/storage/databases/db_postgresql.sql @@ -1,4 +1,8 @@ +/* + BDS Framework, For example only +*/ + CREATE DATABASE framework WITH OWNER = stephane ENCODING = 'UTF8'