Skip to content

Commit 1a0b5e2

Browse files
build: added dotenv for environment configuration
1 parent 610f75d commit 1a0b5e2

File tree

6 files changed

+29
-29
lines changed

6 files changed

+29
-29
lines changed

.env-simple

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Database Variables
2+
HOST_NAME = ''
3+
USER_NAME = ''
4+
PASSWORD = ''
5+
DATABASE_NAME = ''
6+
ACCESS_TOKEN = ''

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@ composer.lock
99
*.pem
1010

1111
# local env files
12-
env.php
12+
.env

env-simple.php

Lines changed: 0 additions & 10 deletions
This file was deleted.

src/rest.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
namespace RestJS\PhpRestApi;
55

66
/* Use External Class */
7+
use Dotenv\Dotenv;
78
use RestJS\PhpRestApi\Controller\File;
89
use RestJS\PhpRestApi\Controller\Method;
910
use RestJS\PhpRestApi\View\Auth;
@@ -14,14 +15,15 @@ class Rest {
1415
public static function execute($dir) {
1516

1617
/* Include Varibles File */
17-
include $dir . '/env.php';
18+
$dotenv = Dotenv::createImmutable($dir);
19+
$dotenv->load();
1820

1921
/* Include View Function */
2022
Auth::accessToken();
2123
$table = Table::checkTable();
2224

2325
/* Check ID */
24-
if (!isset($_GET['id'])) { $id = null; }
26+
if (!isset($_GET['id'])) { $id = null; }
2527
else { $id = $_GET['id']; }
2628

2729
/* Check Upload File */
@@ -34,4 +36,4 @@ public static function execute($dir) {
3436
Method::methodController($_SERVER['REQUEST_METHOD'], $table, $id);
3537

3638
}
37-
}
39+
}

src/view/auth.php

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,37 +3,39 @@
33
/* Define Namespace */
44
namespace RestJS\PhpRestApi\View;
55

6+
/* Head Allows Section */
7+
header("Access-Control-Allow-Origin: *");
8+
header("Content-Type: application/json; charset=UTF-8");
9+
header("Access-Control-Allow-Methods: POST, GET, PUT, DELETE");
10+
header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");
11+
612
class Auth {
713

814
public static function accessToken() {
915

10-
/* Head Allows Section */
11-
header("Access-Control-Allow-Origin: *");
12-
header("Content-Type: application/json; charset=UTF-8");
13-
header("Access-Control-Allow-Methods: POST, GET, PUT, DELETE");
14-
header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");
16+
/* Get Headers */
1517
$headers = getallheaders();
1618

1719
/* Check Access Token */
1820
if (isset($_GET['access_token'])) {
1921

20-
if ($_GET['access_token'] != ACCESS_TOKEN) {
21-
echo json_encode(array('status' => 'Fail', 'error' => 'Please provide valid access token'));
22+
if ($_GET['access_token'] != $_ENV['ACCESS_TOKEN']) {
23+
echo json_encode(['status' => 'Fail', 'error' => 'Please provide valid access token']);
2224
die();
2325
}
2426
}
2527

26-
/* Check Authorization */
27-
else if (isset($headers['Authorization'])) {
28+
/* Check Authorization */
29+
elseif (isset($headers['Authorization'])) {
2830

29-
if ($headers['Authorization'] != "Bearer " . ACCESS_TOKEN) {
30-
echo json_encode(array('status' => 'Fail', 'error' => 'Please provide valid access token'));
31+
if ($headers['Authorization'] != "Bearer " . $_ENV['ACCESS_TOKEN']) {
32+
echo json_encode(['status' => 'Fail', 'error' => 'Please provide valid access token']);
3133
die();
3234
}
33-
}
35+
}
3436
else {
35-
echo json_encode(array('status' => 'Fail', 'error' => 'Please provide access token'));
37+
echo json_encode(['status' => 'Fail', 'error' => 'Please provide access token']);
3638
die();
3739
}
3840
}
39-
}
41+
}

src/view/database.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class Database {
1313
public static function connection() {
1414

1515
/* Create connection */
16-
$con = new mysqli(SERVER_NAME, USER_NAME, PASSWORD, DATABASE_NAME);
16+
$con = new mysqli($_ENV['HOST_NAME'], $_ENV['USER_NAME'], $_ENV['PASSWORD'], $_ENV['DATABASE_NAME']);
1717

1818
/* Check connection */
1919
if ($con->connect_error) {

0 commit comments

Comments
 (0)