Skip to main content
update wording
Source Link

This has worked well over the years, but moving to newer versions of PHP and trying to adhere to some best practices and standards I am considering migrating to a .env file, among other reasons, because that approach seems to be more standardstandardized/popular.

I find myself in a chicken-and-egg situation. I would like to be able to keep this configuration, since migrating everything in a big-bang would require revisiting all build jobs, a lot of classes, and many lines of PHP code referencingreferences.

This has worked well over the years, but moving to newer versions of PHP and trying to adhere to some best practices and standards I am considering migrating to a .env file, among other reasons, because that approach seems to be more standard.

I find myself in a chicken-and-egg situation. I would like to be able to keep this configuration, since migrating everything in a big-bang would require revisiting all build jobs, a lot of classes, and many lines of PHP code referencing.

This has worked well over the years, but moving to newer versions of PHP and trying to adhere to some best practices and standards I am considering migrating to a .env file, among other reasons, because that approach seems to be more standardized/popular.

I find myself in a chicken-and-egg situation. I would like to be able to keep this configuration, since migrating everything in a big-bang would require revisiting all build jobs, a lot of classes, and many lines of PHP code references.

update wording
Source Link

I have a legacy PHP project using a self-styled configuration file (named globals.inc.php) which is generated by build scripts based on a template included in the code called globals.template.inc.php:

I have a legacy PHP project using a self-styled configuration file (globals.inc.php) which is generated by build scripts based on a template included in the code called globals.template.inc.php:

I have a legacy PHP project using a self-styled configuration file (named globals.inc.php) which is generated by build scripts based on a template included in the code called globals.template.inc.php:

Source Link
JoSSte
  • 199
  • 1
  • 8

Migrating configuration method

I have a legacy PHP project using a self-styled configuration file (globals.inc.php) which is generated by build scripts based on a template included in the code called globals.template.inc.php:

<?php

use MyPropject\Core\DBConn;

require_once __DIR__ . "/vendor/autoload.php";

/**
 * GLOBAL variables
 *
 */
define("I18N_FILES",         __DIR__ . '/i18n/lang_{LANGUAGE}.json');
define("I18N_CACHE",         __DIR__ . '/langcache/');
define("SITE_TITLE",         "¤SITE_TITLE¤");
define("SITE_NAME",          "¤SITE_NAME¤"); 
define("SITE_FQDN",          "¤SITE_FQDN¤");
define("SESSION_DURATION",   1800);
define("DEFAULT_FROM_EMAIL", "¤DEFAULT_FROM_EMAIL¤");
define("SESSION_NAME",       "¤SESSION_NAME¤"); 
define("CERT_LOCATION",      "¤CERT_LOCATION¤");
define("MYSQL_USERNAME",     "¤MYSQL_USERNAME¤");
define("MYSQL_PASSWORD",     "¤MYSQL_PASSWORD¤");
define("MYSQLDATABASENAME",  "¤MYSQL_DB_NAME¤");
define("MYSQL_HOSTNAME",     "¤MYSQL_HOSTNAME¤");
define("MYSQL_PORT",         ¤MYSQL_PORT¤);
define("MYSQL_DSN",          "mysql:dbname=" . MYSQLDATABASENAME . ";host=" . MYSQL_HOSTNAME. ";port=" . MYSQL_PORT);
define("DEPLOY_DATE",        "¤DEPLOY_DATE¤");
define("GIT_TAG_BACKEND",    "¤GIT_TAG_BACKEND¤");
define("GIT_TAG_FRONTEND",   "¤GIT_TAG_FRONTEND¤");
if (isset($_SERVER["SERVER_NAME"])) {
    define("IS_PROD", $_SERVER["SERVER_NAME"] === "www.MyPropject.com");
}

if (substr(SITE_TITLE, -1) == "¤" || substr(SITE_TITLE, -1) == "\xa4") {
    exit("Globals not set. Please run deploy.sh");
}
date_default_timezone_set('Europe/Berlin');

$mysqlConn = new mysqli(MYSQL_HOSTNAME, MYSQL_USERNAME, MYSQL_PASSWORD, MYSQLDATABASENAME, 3306);
if ($mysqlConn->connect_errno) {
    echo "Failed to connect to MySQL: (" . $mysqlConn->connect_errno . ") " . $mysqlConn->connect_error;
}
$mysqlConn->query("SET NAMES 'utf8'");

This has worked well over the years, but moving to newer versions of PHP and trying to adhere to some best practices and standards I am considering migrating to a .env file, among other reasons, because that approach seems to be more standard.

I find myself in a chicken-and-egg situation. I would like to be able to keep this configuration, since migrating everything in a big-bang would require revisiting all build jobs, a lot of classes, and many lines of PHP code referencing.

My proposition as a half-measure-transition is to do the following:

  • Generate the .env file from the globals.inc.php by appending the below code to the file.
  • Coding all new classes to use a central configuration class, which reads the .env file (vlucas/phpdotenv maybe)
  • When all areas of the program use the .env file, rewrite the deploy scripts to generate the .env file
$envFileName = __DIR__ . "/.env";
function createEnvFile()
{
    global $envFileName;
    $contents = 
    "I18N_FILES=\"".I18N_FILES."\"\n".
    "I18N_CACHE=\"".I18N_CACHE."\"\n".
    "SITE_TITLE=\"".SITE_TITLE."\"\n".
    "SITE_NAME=\"".SITE_NAME."\"\n".
    "SITE_FQDN=\"".SITE_FQDN."\"\n".
    "SESSION_DURATION=\"".SESSION_DURATION."\"\n".
    "DEFAULT_FROM_EMAIL=\"".DEFAULT_FROM_EMAIL."\"\n".
    "SESSION_NAME=\"".SESSION_NAME."\"\n".
    "CERT_LOCATION=\"".CERT_LOCATION."\"\n".
    "MYSQL_USERNAME=\"".MYSQL_USERNAME."\"\n".
    "MYSQL_PASSWORD=\"".MYSQL_PASSWORD."\"\n".
    "MYSQLDATABASENAME=\"".MYSQLDATABASENAME."\"\n".
    "MYSQL_HOSTNAME=\"".MYSQL_HOSTNAME."\"\n".
    "MYSQL_PORT=\"".MYSQL_PORT."\"\n".
    "MYSQL_DSN=\"".MYSQL_DSN."\"\n".
    "DEPLOY_DATE=\"".DEPLOY_DATE."\"\n".
    "GIT_TAG_BACKEND=\"".GIT_TAG_BACKEND."\"\n".
    "GIT_TAG_FRONTEND=\"".GIT_TAG_FRONTEND."\"\n";
    file_put_contents($envFileName, $contents);

}

// check if .env file exists and has a timestamp similar to this file:
echo "checking for .env file in $envFileName";
if (file_exists($envFileName)) {
    // if file modification time is more than 10 minutes old, create a new one:
    if ( abs(filemtime(__FILE__) - filemtime($envFileName)) > 600 ){
        unlink($envFileName);
        createEnvFile();
    }
} else {
    createEnvFile();
}