From 6cd9baf19c9002103466261cf3c8de518e579497 Mon Sep 17 00:00:00 2001 From: xdrm-brackets Date: Fri, 24 Nov 2017 16:00:12 +0100 Subject: [PATCH] Barebone setup@5 > add: database.{core.Repo,core.Repo_i,repo.user} (Repo dispatcher + user.getAll,getById,getByMail) | dup:config.database-driver done | upd: view.homepage loads a repo (call example) --- build/database/core/DatabaseDriver.php | 5 ++ build/database/core/Repo.php | 79 ++++++++++++++++++++++++++ build/database/core/Repo_i.php | 15 +++++ build/database/repo/user.php | 51 +++++++++++++++++ config/database-driver.json | 8 +-- public_html/index.php | 11 ++++ public_html/view/homepage.php | 9 +++ 7 files changed, 174 insertions(+), 4 deletions(-) create mode 100644 build/database/core/Repo.php create mode 100644 build/database/core/Repo_i.php create mode 100644 build/database/repo/user.php diff --git a/build/database/core/DatabaseDriver.php b/build/database/core/DatabaseDriver.php index e8b94d3..8e9a13c 100755 --- a/build/database/core/DatabaseDriver.php +++ b/build/database/core/DatabaseDriver.php @@ -176,6 +176,11 @@ } + public function pdo(){ + return $this->pdo; + } + + public function getConfig(){ return [ 'host' => $this->host, diff --git a/build/database/core/Repo.php b/build/database/core/Repo.php new file mode 100644 index 0000000..f56b6a0 --- /dev/null +++ b/build/database/core/Repo.php @@ -0,0 +1,79 @@ +pdo()); + + + /* (3) Check if the method exists */ + if( !\method_exists($instance, $path[1]) ) + throw new \Exception("Repo '${path[0]}' has no public method '{$path[1]}'"); + + /* (4) Fetch response (send arguments as well) */ + $response = call_user_func_array([$instance, $path[1]], array_slice(func_get_args(), 1)); + + /* (5) Call post-script */ + $instance = null; + + /* (6) Dispatch response */ + return $response; + + } + + + + + } \ No newline at end of file diff --git a/build/database/core/Repo_i.php b/build/database/core/Repo_i.php new file mode 100644 index 0000000..465d1ea --- /dev/null +++ b/build/database/core/Repo_i.php @@ -0,0 +1,15 @@ +pdo = $pdo; + + } + + } \ No newline at end of file diff --git a/build/database/repo/user.php b/build/database/repo/user.php new file mode 100644 index 0000000..209cf43 --- /dev/null +++ b/build/database/repo/user.php @@ -0,0 +1,51 @@ +pdo->query("SELECT * FROM user ORDER BY username ASC"); + + /* (2) Fetched data */ + return $st->fetchAll(); + + } + + + public function getById(int $id_user){ + + /* (1) Prepare Statement */ + $pst = $this->pdo->prepare("SELECT * FROM user WHERE id_user = :id_user"); + + /* (2) Bind variables */ + $pst->bindParam(':id_user', $id_user, \PDO::PARAM_INT); + + /* (3) Execute */ + if( !$pst->execute() ) return false; // if error -> send FALSE + + /* (4) Fetched data */ + return $pst->fetchAll(); + + } + + + public function getByMail(String $mail){ + + /* (1) Prepare Statement */ + $pst = $this->pdo->prepare("SELECT * FROM user WHERE mail = :mail"); + + /* (2) Bind variables */ + $pst->bindParam(':mail', $mail, \PDO::PARAM_STR, 50); + + /* (3) Execute */ + if( !$pst->execute() ) return false; // if error -> send FALSE + + /* (4) Fetched data */ + return $pst->fetchAll(); + + } + } \ No newline at end of file diff --git a/config/database-driver.json b/config/database-driver.json index 4f51030..72519d5 100755 --- a/config/database-driver.json +++ b/config/database-driver.json @@ -1,10 +1,10 @@ { "default": { "local": { - "host" : "db_local_host", - "dbname" : "db_local_name", - "user" : "db_local_user", - "password" : "db_local_password" + "host" : "localhost", + "dbname" : "ndli1718", + "user" : "ndli1718-php", + "password" : "4JB1dtbrIC8pT935" }, "remote": { "host" : "db_remote_host", diff --git a/public_html/index.php b/public_html/index.php index 0f63ce9..9cc4679 100755 --- a/public_html/index.php +++ b/public_html/index.php @@ -1,8 +1,19 @@ "; $MainRouter = Router::launch($_GET['url']); diff --git a/public_html/view/homepage.php b/public_html/view/homepage.php index 6bf6a65..f4d0a9e 100644 --- a/public_html/view/homepage.php +++ b/public_html/view/homepage.php @@ -16,6 +16,15 @@ + +
+ +