Skip to content

Instantly share code, notes, and snippets.

@rafaelcavalcanti
Last active April 20, 2020 19:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rafaelcavalcanti/ccb01bf77834efd6edf24e92901028cd to your computer and use it in GitHub Desktop.
Save rafaelcavalcanti/ccb01bf77834efd6edf24e92901028cd to your computer and use it in GitHub Desktop.
Using specific .env file according which your environment

The issue

Imagine if you have several different environments and the .env settings need to be dynamic when the project pass through each..

This small script was made to solve it.

But, i strong recommend to take a look on https://github.com/vlucas/phpdotenv for better soluctions.

The flow

The custumer use their projects with the follow flow:

local (windows) -> dev (Linux) -> Stage (Linux) -> Prod (Linux)

So, i've organized the .env name file according the table below:

Environment S.O. .env
Local Windows .env.local
Dev Linux .env.dev
Stage Linux .env.stage
Production Linux .env.production
  • PS: You can use it wich how many files who goes to need, just adjust the DetectEnvironment->whichIs() method it.
  • PS2: It was used at an Laravel project, i believe it can be implemented for other needs.

*Please, let me know if you have any suggestion.

<?php
namespace App\Helpers;
/**
* Detect enviroment via URL or CLI according with some unique caracteristic of the server
*/
class DetectEnvironment
{
/**
* Verify the which environment
*
* @return string name of environment
*/
public function whichIs()
{
// Default is production
$envName = 'production';
// ---------------------------------------------------
// If it was requested throught CLI (command line)
// ----------------------------------------------------
if (php_sapi_name() === 'cli') {
// Locally is using Microsoft Windows
if (stripos(PHP_OS, 'WIN') === 0) {
$envName = 'local';
}
// Dev stack
elseif (strpos(php_uname('a'), 'lampdev.YOUR_URL_NAME.com') !== false) {
$envName = 'dev';
}
// Stage
elseif (strpos(gethostname(), 'stage') !== false) {
$envName = 'stage';
}
return $envName;
}
// ----------------
// Web request
// ----------------
$httpHost = $_SERVER['HTTP_HOST'];
// Locally we use the vhost dev.URL_EXAMPLE.com
if (strpos($httpHost, 'dev.') === 0) {
$envName = 'local';
}
// Lampdev stack
elseif (strpos($httpHost, 'lampdev.URL_EXAMPLE.com') !== false) {
$envName = 'dev';
}
// Stage stack
elseif (strpos($httpHost, 'stage.URL_EXAMPLE.com') !== false) {
$envName = 'stage';
}
return $envName;
}
/**
* Get .ENV filename according with environment
*
* @param string|null $filename If you want to force a specific filename, let it blank for auto-detect
* @return string '.env.[enviroment]'
*/
public function getEnvFilename($filename = null)
{
$suffix = ($filename ?: $this->whichIs());
$newEnvFilename = '.env';
$newEnvFilename .= ".{$suffix}";
return $newEnvFilename;
}
}
<?php
// ...all the code that already exists...
// --------------------------------------------------
// Identify Environment....
// PS: Insert it before the 'return $app' line
// ---------------------------------------------------
$detectEnvironment = new App\Helpers\DetectEnvironment;
$env = $app->detectEnvironment(function() use ($detectEnvironment) {
return $detectEnvironment->whichIs();
});
$app->loadEnvironmentFrom($detectEnvironment->getEnvFilename($env));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment