From 972574d2934dac848076790e68f176494719fb4c Mon Sep 17 00:00:00 2001 From: William Date: Thu, 20 Jan 2022 22:23:21 +0100 Subject: [PATCH] Commit --- app/core/App.php | 14 ++++++++++++-- app/core/DBHandle.php | 31 ------------------------------- app/core/Database.php | 36 ++++++++++++++++++++++++++++++++++++ app/core/Session.php | 2 +- 4 files changed, 49 insertions(+), 34 deletions(-) delete mode 100644 app/core/DBHandle.php create mode 100644 app/core/Database.php diff --git a/app/core/App.php b/app/core/App.php index de92a1e..d28aa32 100644 --- a/app/core/App.php +++ b/app/core/App.php @@ -2,10 +2,20 @@ class App { + public array $config; + public object $database; + public object $session; + public function __construct() { - $config = require __DIR__ . '/../config.php'; - $this->config = $config; + // Require configuration file + $this->config = require __DIR__ . '/../config.php'; + + // Connect to database + $this->database = new Database($this->config['database']); + + // Instantiate new session + $this->session = new Session; } // Grab model diff --git a/app/core/DBHandle.php b/app/core/DBHandle.php deleted file mode 100644 index cba05b5..0000000 --- a/app/core/DBHandle.php +++ /dev/null @@ -1,31 +0,0 @@ -dbh = $this->connectWithMySQL(); - } catch (PDOException $e) { - throw new PDOException($e->getMessage(), (int)$e->getCode()); - } - } - - private function connectWithMySQL(): object - { - $dsn = "mysql:host=$this->host;dbname=$this->db;charset=$this->charset"; - $options = [ - PDO::ATTR_PERSISTENT => true, - ]; - return new PDO($dsn, $this->user, $this->pass, $options); - } -} \ No newline at end of file diff --git a/app/core/Database.php b/app/core/Database.php new file mode 100644 index 0000000..d3508c5 --- /dev/null +++ b/app/core/Database.php @@ -0,0 +1,36 @@ +conn = $this->connectWithMySQL($config['args']); + } catch (PDOException $e) { + throw new PDOException("Database error: " . $e->getMessage()); + } + } + + private function connectWithMySQL(array $args): object + { + $host = $args['host']; + $database = $args['database']; + $charset = $args['charset']; + $user = $args['user']; + $password = $args['password']; + + $dsn = "mysql:host=$host;dbname=$database;charset=$charset"; + $options = [ + PDO::ATTR_PERSISTENT => true, + ]; + return new PDO($dsn, $user, $password, $options); + } +} \ No newline at end of file diff --git a/app/core/Session.php b/app/core/Session.php index d3351a4..4a9a17f 100644 --- a/app/core/Session.php +++ b/app/core/Session.php @@ -3,9 +3,9 @@ // Handles anything to do with sessions class Session { - // TODO: ... public function __construct() { + // Start new session if there is none if (session_status() === PHP_SESSION_NONE) { session_start();