Skip to content

Commit

Permalink
feat(App): Ability to load environment variables with .env files.
Browse files Browse the repository at this point in the history
- Added .env.example as guide when deploying app
- Added security rules to .htaccess
- Updated .gitignore to ignore .env files
  • Loading branch information
CarlosEGuerraSilva committed Jul 24, 2024
1 parent 3d3db29 commit 9b15159
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
GUSI_FRAMEWORK_ENCRYPTION_KEY=YourEncryptionKey
GUSI_FRAMEWORK_DB_USER=YourDatabaseUser
GUSI_FRAMEWORK_DB_PASSWORD=YourDatabasePassword
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# Ignore .env files
.env

# Ignore .log debug files in App/Logs/Debug
App/Logs/Debug/*.log

Expand Down
5 changes: 5 additions & 0 deletions .htaccess
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@
AddOutputFilterByType DEFLATE text/xml
</IfModule>

<FilesMatch "^\.env$">
Order allow,deny
Deny from all
</FilesMatch>

Options -Indexes
RewriteEngine On

Expand Down
23 changes: 23 additions & 0 deletions App/Core/Application/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,32 @@ public function __construct()
private function init()
{
$this->loadModules();
$this->loadEnvironmentVariables();
Router::getInstance()->handleRequest();
}

/**
* Loads environment variables from the .env file.
*/
private function loadEnvironmentVariables(){
if (file_exists('.env')) {
$envData = file_get_contents('.env');
$envLines = explode("\n", $envData);
foreach ($envLines as $line) {
$line = trim($line);
if (empty($line) || strpos($line, '#') === 0) {
continue;
}
$parts = explode('=', $line, 2);
if (count($parts) === 2) {
$key = trim($parts[0]);
$value = trim($parts[1]);
putenv("$key=$value");
}
}
}
}

/**
* Loads modules from the App/Modules directory.
*/
Expand Down

0 comments on commit 9b15159

Please sign in to comment.