From 0e9045361a3ef273a678ae3ce3b2ac2f89e2e1bf Mon Sep 17 00:00:00 2001 From: Raeen Date: Tue, 16 Mar 2021 17:41:14 +0330 Subject: [PATCH] apikey --- .env | 12 +++++++----- core/Application.php | 2 +- core/Controller.php | 4 ++++ core/Response.php | 11 +++++++++++ 4 files changed, 23 insertions(+), 6 deletions(-) diff --git a/.env b/.env index 2835e03..efdfcde 100644 --- a/.env +++ b/.env @@ -1,5 +1,7 @@ -SERVERNAME = localhost -PORT = 3306 -USERNAME = root -PASSWORD = -DATABASENAME = api \ No newline at end of file +# Mysql Config + +DB_SERVER = localhost +DB_PORT = 3306 +DB_USERNAME = root +DB_PASSWORD = +DB_NAME = api \ No newline at end of file diff --git a/core/Application.php b/core/Application.php index bde1e51..0e35bb7 100644 --- a/core/Application.php +++ b/core/Application.php @@ -23,7 +23,7 @@ public function __construct($rootPath) $this->response = new Response(); $this->router = new Router(); $this->file = new File(); - $this->db = new DB($_ENV['SERVERNAME'] . ":" . $_ENV['PORT'], $_ENV['USERNAME'], $_ENV['PASSWORD'], $_ENV['DATABASENAME']); + $this->db = new DB($_ENV['DB_SERVER'] . ":" . $_ENV['DB_PORT'], $_ENV['DB_USERNAME'], $_ENV['DB_PASSWORD'], $_ENV['DB_NAME']); } public function run() { diff --git a/core/Controller.php b/core/Controller.php index 585ee8d..7bcf68d 100644 --- a/core/Controller.php +++ b/core/Controller.php @@ -34,4 +34,8 @@ protected function sendFile($filename) { return Application::$app->file->sendFile($filename); } + protected function apikey($length = 20) + { + return Application::$app->response->apikey($length); + } } diff --git a/core/Response.php b/core/Response.php index 7e19a0e..edf8b77 100644 --- a/core/Response.php +++ b/core/Response.php @@ -111,4 +111,15 @@ public function render(int $code, array|object $data = null, $status = null, $me } die(json_encode($temp)); } + public function apikey($length = 20) + { + //gen api key + $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; + $charactersLength = strlen($characters); + $randomString = ''; + for ($i = 0; $i < $length; $i++) { + $randomString .= $characters[rand(0, $charactersLength - 1)]; + } + return $randomString; + } }