Difference between revisions of "Dotenv Usage"

From OpenEMR Project Wiki
(Created page with "Starting with version 5.0.1 OpenEMR now supports "Dotenv" files to better enable development. A dotenv file is a file, excluded from version control, that allows each d...")
 
 
(3 intermediate revisions by 2 users not shown)
Line 5: Line 5:
Currently this only has one effect: ensure the frontend assets are refreshed on every request (It overrides the <code>$v_js_includes</code> variable with a random integer).
Currently this only has one effect: ensure the frontend assets are refreshed on every request (It overrides the <code>$v_js_includes</code> variable with a random integer).


Example .env file
 
== Example .env file ==


<pre>OPENEMR__ENVIRONMENT=dev</pre>
<pre>OPENEMR__ENVIRONMENT=dev</pre>
Future use could include special logging or profiling during development. However, please be wise on where to use this; if you're writing lots of functions that use this feature, you may need to reconsider ''how'' you're writing the functions.
Future use could include special logging or profiling during development. However, please be wise on where to use this; if you're writing lots of functions that use this feature, you may need to reconsider ''how'' you're writing the functions.


Example usage in code


<pre>use Dotenv\Dotenv;
== Example usage in code ==
$dotenv = new Dotenv(__DIR__); // Will search current directory for a file called .env
 
$dotenv-&gt;load();
<pre>// should only be used in globals.php, is referenced here just as an example
use Dotenv\Dotenv;
if (file_exists("{$webserver_root}/.env") {
    $dotenv = Dotenv::createImmutable($webserver_root);
    $dotenv-&gt;load();
}


// From above example
// From above example
if (getenv(&quot;OPENEMR__ENVIRONMENT&quot;) === &quot;dev&quot;) {
if ($_ENV[&quot;OPENEMR__ENVIRONMENT&quot;] === &quot;dev&quot;) {
     // do something specitifc for a dev site
     // do something specitifc for a dev site
} else {
} else {
Line 23: Line 28:
     // Or, if there's nothing special to happen, just omit the else clause
     // Or, if there's nothing special to happen, just omit the else clause
}</pre>
}</pre>
To learn more about Dotenv and some more usage, see the [https://github.com/vlucas/phpdotenv github repository]
To learn more about Dotenv and some more usage, see the [https://github.com/vlucas/phpdotenv github repository]

Latest revision as of 06:35, 8 September 2020

Starting with version 5.0.1 OpenEMR now supports "Dotenv" files to better enable development. A dotenv file is a file, excluded from version control, that allows each developer to set specific variables special to the development environment (Or for production testing). An example would be testing interaction with an online cloud service (such as Amazon AWS) where the dev has an access and secret key. Those keys should not be stored in git, code, or a database, they should instead be set on the server level. By utilizing a .env file, you can simulate that server variable without having to actually go in and mess around under the hood.

In the root directory copy .env.example to .env and set the OPENEMR__ENVIRONMENT variable to dev

Currently this only has one effect: ensure the frontend assets are refreshed on every request (It overrides the $v_js_includes variable with a random integer).


Example .env file

OPENEMR__ENVIRONMENT=dev

Future use could include special logging or profiling during development. However, please be wise on where to use this; if you're writing lots of functions that use this feature, you may need to reconsider how you're writing the functions.


Example usage in code

// should only be used in globals.php, is referenced here just as an example
use Dotenv\Dotenv;
if (file_exists("{$webserver_root}/.env") {
    $dotenv = Dotenv::createImmutable($webserver_root);
    $dotenv->load();
}

// From above example
if ($_ENV["OPENEMR__ENVIRONMENT"] === "dev") {
    // do something specitifc for a dev site
} else {
    // do something else for any other site (Right now just `prod`)
    // Or, if there's nothing special to happen, just omit the else clause
}


To learn more about Dotenv and some more usage, see the github repository