NEW replace _ss_environment.php with .env and environment vars

This commit is contained in:
Daniel Hensby 2017-01-30 15:33:56 +00:00 committed by Daniel Hensby
parent a5da085cc7
commit 873fd8c5bc
No known key found for this signature in database
GPG Key ID: E38EC566FE29EB66
22 changed files with 161 additions and 192 deletions

View File

@ -82,23 +82,20 @@ $_SESSION = null;
// Connect to database
if(!isset($databaseConfig) || !isset($databaseConfig['database']) || !$databaseConfig['database']) {
echo "\nPlease configure your database connection details. You can do this by creating a file
called _ss_environment.php in either of the following locations:\n\n";
echo " - " . BASE_PATH . DIRECTORY_SEPARATOR . "_ss_environment.php\n - ";
echo dirname(BASE_PATH) . DIRECTORY_SEPARATOR . "_ss_environment.php\n\n";
called .env in " . BASE_PATH;
echo <<<ENVCONTENT
Put the following content into this file:
--------------------------------------------------
<?php
/* Change this from 'dev' to 'live' for a production environment. */
define('SS_ENVIRONMENT_TYPE', 'dev');
# Change this from 'dev' to 'live' for a production environment.
SS_ENVIRONMENT_TYPE="dev"
/* This defines a default database user */
define('SS_DATABASE_SERVER', 'localhost');
define('SS_DATABASE_USERNAME', '<user>');
define('SS_DATABASE_PASSWORD', '<password>');
define('SS_DATABASE_NAME', '<database>');
SS_DATABASE_SERVER="localhost"
SS_DATABASE_USERNAME="<user>"
SS_DATABASE_PASSWORD="<password>"
SS_DATABASE_NAME="<database>"
--------------------------------------------------
Once you have done that, run 'composer install' or './framework/sake dev/build' to create

View File

@ -24,7 +24,8 @@
"embed/embed": "^2.6",
"swiftmailer/swiftmailer": "~5.4",
"symfony/config": "^2.8|^3",
"symfony/translation": "^2.8|^3"
"symfony/translation": "^2.8|^3",
"vlucas/phpdotenv": "^2.4"
},
"require-dev": {
"phpunit/PHPUnit": "~4.8",

View File

@ -32,8 +32,7 @@ if (version_compare(phpversion(), '5.5.0', '<')) {
*
* The main.php does a number of set-up activities for the request.
*
* - Includes the first one of the following files that it finds: (root)/_ss_environment.php,
* (root)/../_ss_environment.php, or (root)/../../_ss_environment.php
* - Includes the .env file in your webroot
* - Gets an up-to-date manifest from {@link ManifestBuilder}
* - Sets up error handlers with {@link Debug::loadErrorHandlers()}
* - Calls {@link DB::connect()}, passing it the global variable $databaseConfig that should

View File

@ -35,13 +35,8 @@ class ProtectedAssetAdapter extends AssetAdapter implements ProtectedAdapter
return parent::findRoot($root);
}
// Use environment defined path
if (defined('SS_PROTECTED_ASSETS_PATH')) {
return SS_PROTECTED_ASSETS_PATH;
}
// Default location is under assets
return ASSETS_PATH . '/' . Config::inst()->get(get_class($this), 'secure_folder');
// Use environment defined path or default location is under assets
return getenv('SS_PROTECTED_ASSETS_PATH') ?: ASSETS_PATH . '/' . Config::inst()->get(get_class($this), 'secure_folder');
}
/**

View File

@ -607,7 +607,7 @@ class Director implements TemplateGlobalProvider
// See https://support.microsoft.com/en-us/kb/307347
$headerOverride = false;
if (TRUSTED_PROXY) {
$headers = (defined('SS_TRUSTED_PROXY_PROTOCOL_HEADER')) ? array(SS_TRUSTED_PROXY_PROTOCOL_HEADER) : null;
$headers = (getenv('SS_TRUSTED_PROXY_PROTOCOL_HEADER')) ? array(getenv('SS_TRUSTED_PROXY_PROTOCOL_HEADER')) : null;
if (!$headers) {
// Backwards compatible defaults
$headers = array('HTTP_X_FORWARDED_PROTO', 'HTTP_X_FORWARDED_PROTOCOL', 'HTTP_FRONT_END_HTTPS');

View File

@ -768,7 +768,7 @@ class HTTPRequest implements ArrayAccess
{
$headerOverrideIP = null;
if (TRUSTED_PROXY) {
$headers = (defined('SS_TRUSTED_PROXY_IP_HEADER')) ? array(SS_TRUSTED_PROXY_IP_HEADER) : null;
$headers = (getenv('SS_TRUSTED_PROXY_IP_HEADER')) ? array(getenv('SS_TRUSTED_PROXY_IP_HEADER')) : null;
if (!$headers) {
// Backwards compatible defaults
$headers = array('HTTP_CLIENT_IP', 'HTTP_X_FORWARDED_FOR');

View File

@ -3,7 +3,6 @@
* This file is the Framework constants bootstrap. It will prepare some basic common constants.
*
* It takes care of:
* - Including _ss_environment.php
* - Normalisation of $_SERVER values
* - Initialisation of necessary constants (mostly paths)
*
@ -30,48 +29,6 @@
///////////////////////////////////////////////////////////////////////////////
// ENVIRONMENT CONFIG
/**
* Include _ss_environment.php file
*/
//define the name of the environment file
$envFile = '_ss_environment.php';
//define the dirs to start scanning from (have to add the trailing slash)
// we're going to check the realpath AND the path as the script sees it
$dirsToCheck = array(
realpath('.'),
dirname($_SERVER['SCRIPT_FILENAME'])
);
//if they are the same, remove one of them
if ($dirsToCheck[0] == $dirsToCheck[1]) {
unset($dirsToCheck[1]);
}
foreach ($dirsToCheck as $dir) {
//check this dir and every parent dir (until we hit the base of the drive)
// or until we hit a dir we can't read
while (true) {
//if it's readable, go ahead
if (@is_readable($dir)) {
//if the file exists, then we include it, set relevant vars and break out
if (file_exists($dir . DIRECTORY_SEPARATOR . $envFile)) {
define('SS_ENVIRONMENT_FILE', $dir . DIRECTORY_SEPARATOR . $envFile);
include_once(SS_ENVIRONMENT_FILE);
//break out of BOTH loops because we found the $envFile
break(2);
}
} else {
//break out of the while loop, we can't read the dir
break;
}
if (dirname($dir) == $dir) {
// here we need to check that the path of the last dir and the next one are
// not the same, if they are, we have hit the root of the drive
break;
}
//go up a directory
$dir = dirname($dir);
}
}
/**
* Validate whether the request comes directly from a trusted server or not
* This is necessary to validate whether or not the values of X-Forwarded-
@ -82,18 +39,18 @@ if (!defined('TRUSTED_PROXY')) {
if (getenv('BlockUntrustedProxyHeaders') // Legacy setting (reverted from documentation)
|| getenv('BlockUntrustedIPs') // Documented setting
|| defined('SS_TRUSTED_PROXY_IPS')
|| getenv('SS_TRUSTED_PROXY_IPS')
) {
$trusted = false;
if (defined('SS_TRUSTED_PROXY_IPS') && SS_TRUSTED_PROXY_IPS !== 'none') {
if (SS_TRUSTED_PROXY_IPS === '*') {
if (getenv('SS_TRUSTED_PROXY_IPS') !== 'none') {
if (getenv('SS_TRUSTED_PROXY_IPS') === '*') {
$trusted = true;
} elseif (isset($_SERVER['REMOTE_ADDR'])) {
if (!class_exists('SilverStripe\\Control\\Util\\IPUtils')) {
require_once 'Control/IPUtils.php';
};
$trusted = SilverStripe\Control\Util\IPUtils::checkIP($_SERVER['REMOTE_ADDR'], explode(',', SS_TRUSTED_PROXY_IPS));
$trusted = SilverStripe\Control\Util\IPUtils::checkIP($_SERVER['REMOTE_ADDR'], explode(',', getenv('SS_TRUSTED_PROXY_IPS')));
}
}
}
@ -202,6 +159,23 @@ if (!defined('BASE_PATH')) {
}
define('BASE_PATH', $candidateBasePath);
}
// Allow a first class env var to be set that disables .env file loading
if (!getenv('SS_IGNORE_DOT_ENV')) {
foreach (array(
BASE_PATH,
dirname(BASE_PATH),
) as $path) {
try {
(new \Dotenv\Dotenv($path))->load();
} catch (\Dotenv\Exception\InvalidPathException $e) {
// no .env found - no big deal
continue;
}
break;
}
}
if (!defined('BASE_URL')) {
// Determine the base URL by comparing SCRIPT_NAME to SCRIPT_FILENAME and getting common elements
$path = realpath($_SERVER['SCRIPT_FILENAME']);

View File

@ -176,7 +176,7 @@ class ClassManifest
$this->base = $base;
$this->tests = $includeTests;
$cacheClass = defined('SS_MANIFESTCACHE') ? SS_MANIFESTCACHE : 'SilverStripe\\Core\\Manifest\\ManifestCache_File';
$cacheClass = getenv('SS_MANIFESTCACHE') ?: 'SilverStripe\\Core\\Manifest\\ManifestCache_File';
$this->cache = new $cacheClass('classmanifest'.($includeTests ? '_tests' : ''));
$this->cacheKey = 'manifest';

View File

@ -210,7 +210,7 @@ class ConfigManifest
if (isset($key['envvars'])) {
foreach ($key['envvars'] as $variable => $foo) {
$key['envvars'][$variable] = isset($_ENV[$variable]) ? $_ENV[$variable] : null;
$key['envvars'][$variable] = getenv($variable) ?: null;
}
}
@ -726,7 +726,7 @@ class ConfigManifest
break;
case 'envvarset':
$matches = $matches && isset($_ENV[$v]);
$matches = $matches && getenv($v);
break;
case 'constantdefined':
@ -735,7 +735,7 @@ class ConfigManifest
default:
$matches = $matches && (
(isset($_ENV[$k]) && $_ENV[$k] == $v) ||
getenv($k) == $v ||
(defined($k) && constant($k) == $v)
);
break;

View File

@ -179,7 +179,7 @@ class ParameterConfirmationToken
// See https://support.microsoft.com/en-us/kb/307347
$headerOverride = false;
if (TRUSTED_PROXY) {
$headers = (defined('SS_TRUSTED_PROXY_PROTOCOL_HEADER')) ? array(SS_TRUSTED_PROXY_PROTOCOL_HEADER) : null;
$headers = (getenv('SS_TRUSTED_PROXY_PROTOCOL_HEADER')) ? array(getenv('SS_TRUSTED_PROXY_PROTOCOL_HEADER')) : null;
if (!$headers) {
// Backwards compatible defaults
$headers = array('HTTP_X_FORWARDED_PROTO', 'HTTP_X_FORWARDED_PROTOCOL', 'HTTP_FRONT_END_HTTPS');

View File

@ -152,10 +152,7 @@ class Deprecation
if (isset(self::$enabled)) {
return self::$enabled;
}
if (defined('SS_DEPRECATION_ENABLED')) {
return SS_DEPRECATION_ENABLED;
}
return true;
return getenv('SS_DEPRECATION_ENABLED') ?: true;
}
/**

View File

@ -2,7 +2,7 @@ $(document).ready(function () {
/**
* Toggle field readonly modes, if check configuration comes from
* _ss_environment (values populated on reload).
* environment variables (values populated on reload).
*/
$('#use_environment').click(function (e) {
if (!$(this).is(':checked')) {

View File

@ -101,7 +101,7 @@
// All are disabled by default when environment is used
$disabled = 'disabled="disabled"';
// If SS_DATABASE_CLASS is specified, check the database in the list
if(defined('SS_DATABASE_CLASS') && SS_DATABASE_CLASS == $class) {
if(getenv('SS_DATABASE_CLASS') == $class) {
$checked = ' checked="checked"';
}
} else {
@ -135,8 +135,8 @@
$fieldType = ($fieldName == 'password') ? 'password' : 'text';
// values
$defaultValue = (isset($fieldSpec['default'])) ? $fieldSpec['default'] : null;
if($usingEnv && isset($fieldSpec['envVar']) && defined($fieldSpec['envVar'])) {
$value = constant($fieldSpec['envVar']);
if($usingEnv && isset($fieldSpec['envVar']) && $envVar = getenv($fieldSpec['envVar'])) {
$value = $envVar;
} else {
$value = (isset($databaseConfig[$fieldName]) && $databaseConfig['type'] == $class) ? $databaseConfig[$fieldName] : $defaultValue;
}
@ -149,7 +149,7 @@
'name' => "db[$class][$fieldName]",
'value' => $value,
);
if($usingEnv && isset($fieldSpec['envVar']) && defined($fieldSpec['envVar'])) {
if($usingEnv && isset($fieldSpec['envVar']) && getenv($fieldSpec['envVar'])) {
$attrs['disabled'] = 'disabled';
}
if(isset($fieldSpec['envVar'])) {
@ -177,7 +177,7 @@
<?php if($envFileExists) { ?>
<div id="use_environment_field" class="field">
<input id="use_environment" type="checkbox" name="useEnv" <?php if($usingEnv) echo "checked=\"checked\"" ?>>
<label for="use_environment">Use _ss_environment file for configuration (<a href="http://doc.silverstripe.org/framework/en/topics/environment-management" target="_blank">?</a>)</label>
<label for="use_environment">Use environment variables for configuration (<a href="http://doc.silverstripe.org/framework/en/topics/environment-management" target="_blank">?</a>)</label>
</div>
<?php } ?>
@ -202,14 +202,14 @@
<div class="field">
<label for="admin_username">Email:</label>
<span class="middleColumn">
<input type="text" class="text configured-by-env" name="admin[username]" id="admin_username" value="<?php echo htmlspecialchars($adminConfig['username'], ENT_QUOTES, 'UTF-8'); ?>" <?php if($usingEnv && defined('SS_DEFAULT_ADMIN_USERNAME')) echo 'disabled="disabled"' ?>>
<input type="text" class="text configured-by-env" name="admin[username]" id="admin_username" value="<?php echo htmlspecialchars($adminConfig['username'], ENT_QUOTES, 'UTF-8'); ?>" <?php if($usingEnv && getenv('SS_DEFAULT_ADMIN_USERNAME')) echo 'disabled="disabled"' ?>>
</span>
</div>
<div class="field">
<label for="admin_password">Password:</label>
<span class="middleColumn">
<input type="password" class="text configured-by-env" name="admin[password]" id="admin_password" value="<?php echo htmlspecialchars($adminConfig['password'], ENT_QUOTES, 'UTF-8'); ?>" <?php if($usingEnv && defined('SS_DEFAULT_ADMIN_PASSWORD')) echo 'disabled="disabled"' ?>>
<input type="password" class="text configured-by-env" name="admin[password]" id="admin_password" value="<?php echo htmlspecialchars($adminConfig['password'], ENT_QUOTES, 'UTF-8'); ?>" <?php if($usingEnv && getenv('SS_DEFAULT_ADMIN_PASSWORD')) echo 'disabled="disabled"' ?>>
</span>
</div>
</div>

View File

@ -60,8 +60,7 @@ if (!$included) {
exit(1);
}
$envFileExists = defined('SS_ENVIRONMENT_FILE');
$usingEnv = $envFileExists && !empty($_REQUEST['useEnv']);
$usingEnv = !empty($_REQUEST['useEnv']);
require_once __DIR__ . '/DatabaseConfigurationHelper.php';
require_once __DIR__ . '/DatabaseAdapterRegistry.php';
@ -142,8 +141,8 @@ if(isset($_REQUEST['db'])) {
if(isset($_REQUEST['db']['type'])) {
$type = $_REQUEST['db']['type'];
} else {
if( defined('SS_DATABASE_CLASS') ){
$type = $_REQUEST['db']['type'] = SS_DATABASE_CLASS;
if ($type = getenv('SS_DATABASE_CLASS')) {
$_REQUEST['db']['type'] = $type;
} elseif( $databaseClasses['MySQLPDODatabase']['supported'] ) {
$type = $_REQUEST['db']['type'] = 'MySQLPDODatabase';
} elseif( $databaseClasses['MySQLDatabase']['supported'] ) {
@ -156,10 +155,10 @@ if(isset($_REQUEST['db'])) {
// Disabled inputs don't submit anything - we need to use the environment (except the database name)
if($usingEnv) {
$_REQUEST['db'][$type] = $databaseConfig = array(
"type" => defined('SS_DATABASE_CLASS') ? SS_DATABASE_CLASS : $type,
"server" => defined('SS_DATABASE_SERVER') ? SS_DATABASE_SERVER : "localhost",
"username" => defined('SS_DATABASE_USERNAME') ? SS_DATABASE_USERNAME : "root",
"password" => defined('SS_DATABASE_PASSWORD') ? SS_DATABASE_PASSWORD : "",
"type" => getenv('SS_DATABASE_CLASS') ?: $type,
"server" => getenv('SS_DATABASE_SERVER') ?: "localhost",
"username" => getenv('SS_DATABASE_USERNAME') ?: "root",
"password" => getenv('SS_DATABASE_PASSWORD') ?: "",
"database" => $_REQUEST['db'][$type]['database'],
);
@ -169,8 +168,8 @@ if(isset($_REQUEST['db'])) {
$databaseConfig['type'] = $type;
}
} else {
if( defined('SS_DATABASE_CLASS') ){
$type = $_REQUEST['db']['type'] = SS_DATABASE_CLASS;
if($type = getenv('SS_DATABASE_CLASS')) {
$_REQUEST['db']['type'] = $type;
} elseif( $databaseClasses['MySQLPDODatabase']['supported'] ) {
$type = $_REQUEST['db']['type'] = 'MySQLPDODatabase';
} elseif( $databaseClasses['MySQLDatabase']['supported'] ) {
@ -180,9 +179,9 @@ if(isset($_REQUEST['db'])) {
}
$_REQUEST['db'][$type] = $databaseConfig = array(
"type" => $type,
"server" => defined('SS_DATABASE_SERVER') ? SS_DATABASE_SERVER : "localhost",
"username" => defined('SS_DATABASE_USERNAME') ? SS_DATABASE_USERNAME : "root",
"password" => defined('SS_DATABASE_PASSWORD') ? SS_DATABASE_PASSWORD : "",
"server" => getenv('SS_DATABASE_SERVER') ?: "localhost",
"username" => getenv('SS_DATABASE_USERNAME') ?: "root",
"password" => getenv('SS_DATABASE_PASSWORD') ?: "",
"database" => isset($_SERVER['argv'][2]) ? $_SERVER['argv'][2] : "SS_mysite",
);
}
@ -191,16 +190,16 @@ if(isset($_REQUEST['admin'])) {
// Disabled inputs don't submit anything - we need to use the environment (except the database name)
if($usingEnv) {
$_REQUEST['admin'] = $adminConfig = array(
'username' => defined('SS_DEFAULT_ADMIN_USERNAME') ? SS_DEFAULT_ADMIN_USERNAME : 'admin',
'password' => defined('SS_DEFAULT_ADMIN_PASSWORD') ? SS_DEFAULT_ADMIN_PASSWORD : '',
'username' => getenv('SS_DEFAULT_ADMIN_USERNAME') ?: 'admin',
'password' => getenv('SS_DEFAULT_ADMIN_PASSWORD') ?: '',
);
} else {
$adminConfig = $_REQUEST['admin'];
}
} else {
$_REQUEST['admin'] = $adminConfig = array(
'username' => defined('SS_DEFAULT_ADMIN_USERNAME') ? SS_DEFAULT_ADMIN_USERNAME : 'admin',
'password' => defined('SS_DEFAULT_ADMIN_PASSWORD') ? SS_DEFAULT_ADMIN_PASSWORD : '',
'username' => getenv('SS_DEFAULT_ADMIN_USERNAME') ?: 'admin',
'password' => getenv('SS_DEFAULT_ADMIN_PASSWORD') ?: '',
);
}
@ -1402,7 +1401,7 @@ class Installer extends InstallRequirements {
// Write the config file
global $usingEnv;
if($usingEnv) {
$this->statusMessage("Setting up 'mysite/_config.php' for use with _ss_environment.php...");
$this->statusMessage("Setting up 'mysite/_config.php' for use with environment variables...");
$this->writeToFile("mysite/_config.php", <<<PHP
<?php

View File

@ -1026,7 +1026,7 @@ class SapphireTest extends PHPUnit_Framework_TestCase
public static function using_temp_db()
{
$dbConn = DB::get_conn();
$prefix = defined('SS_DATABASE_PREFIX') ? SS_DATABASE_PREFIX : 'ss_';
$prefix = getenv('SS_DATABASE_PREFIX') ?: 'ss_';
return $dbConn && (substr($dbConn->getSelectedDatabase(), 0, strlen($prefix) + 5)
== strtolower(sprintf('%stmpdb', $prefix)));
}
@ -1083,7 +1083,7 @@ class SapphireTest extends PHPUnit_Framework_TestCase
$databaseConfig['timezone'] = '+0:00';
DB::connect($databaseConfig);
$dbConn = DB::get_conn();
$prefix = defined('SS_DATABASE_PREFIX') ? SS_DATABASE_PREFIX : 'ss_';
$prefix = getenv('SS_DATABASE_PREFIX') ?: 'ss_';
$dbname = strtolower(sprintf('%stmpdb', $prefix)) . rand(1000000, 9999999);
while (!$dbname || $dbConn->databaseExists($dbname)) {
$dbname = strtolower(sprintf('%stmpdb', $prefix)) . rand(1000000, 9999999);
@ -1103,7 +1103,7 @@ class SapphireTest extends PHPUnit_Framework_TestCase
public static function delete_all_temp_dbs()
{
$prefix = defined('SS_DATABASE_PREFIX') ? SS_DATABASE_PREFIX : 'ss_';
$prefix = getenv('SS_DATABASE_PREFIX') ?: 'ss_';
foreach (DB::get_schema()->databaseList() as $dbName) {
if (preg_match(sprintf('/^%stmpdb[0-9]+$/', $prefix), $dbName)) {
DB::get_schema()->dropDatabase($dbName);

View File

@ -241,7 +241,7 @@ class DB
return false;
}
$prefix = defined('SS_DATABASE_PREFIX') ? SS_DATABASE_PREFIX : 'ss_';
$prefix = getenv('SS_DATABASE_PREFIX') ?: 'ss_';
$pattern = strtolower(sprintf('/^%stmpdb\d{7}$/', $prefix));
return (bool)preg_match($pattern, $name);
}

View File

@ -134,9 +134,9 @@ class BasicAuth
* away from prying eyes, but still be able to test the regular log-in features of the site.
*
* If you are including conf/ConfigureFromEnv.php in your _config.php file, you can also enable
* this feature by adding this line to your _ss_environment.php:
* this feature by adding this line to your .env:
*
* define('SS_USE_BASIC_AUTH', true);
* SS_USE_BASIC_AUTH=1
*
* @param boolean $protect Set this to false to disable protection.
* @param string $code {@link Permission} code that is required from the user.

View File

@ -72,9 +72,8 @@ class ThemeManifest implements ThemeList
$this->project = $project;
$cacheClass = defined('SS_MANIFESTCACHE')
? SS_MANIFESTCACHE
: 'SilverStripe\\Core\\Manifest\\ManifestCache_File';
$cacheClass = getenv('SS_MANIFESTCACHE')
?: 'SilverStripe\\Core\\Manifest\\ManifestCache_File';
$this->cache = new $cacheClass('thememanifest'.($includeTests ? '_tests' : ''));
$this->cacheKey = $this->getCacheKey();

View File

@ -1,9 +1,9 @@
<?php
/**
* Configure SilverStripe from the _ss_environment.php file.
* Configure SilverStripe from the environment variables.
* Usage: Put "require_once('conf/ConfigureFromEnv.php');" into your _config.php file.
*
* If you include this file, you will be able to use the following defines in _ss_environment.php to control
* If you include this file, you will be able to use the following variables to control
* your site.
*
* Your database connection will be set up using these defines:
@ -55,49 +55,27 @@ use SilverStripe\Dev\Install\DatabaseAdapterRegistry;
use SilverStripe\Security\BasicAuth;
use SilverStripe\Security\Security;
/*
* _ss_environment.php handler
*/
if (defined('SS_ENVIRONMENT_FILE')) {
// Only perform validation if SS_ENVIRONMENT_FILE is actually set, which is to say, there is an
// _ss_environment.php file
foreach (array(
'SS_DATABASE_PASSWORD',
'SS_DATABASE_USERNAME',
'SS_ENVIRONMENT_TYPE',) as $reqDefine) {
if (!defined($reqDefine)) {
user_error(
"$reqDefine must be defined in your _ss_environment.php."
. "See http://doc.silverstripe.org/framework/en/topics/environment-management for more information",
E_USER_ERROR
);
}
}
}
if (defined('SS_ENVIRONMENT_TYPE')) {
Director::config()->environment_type = SS_ENVIRONMENT_TYPE;
if ($envType = getenv('SS_ENVIRONMENT_TYPE')) {
Director::config()->environment_type = $envType;
}
global $database;
// No database provided
if (!isset($database) || !$database) {
if (defined('SS_DATABASE_NAME')) {
$database = SS_DATABASE_NAME;
} elseif (defined('SS_DATABASE_CHOOSE_NAME') && SS_DATABASE_CHOOSE_NAME) {
$loopCount = (int)SS_DATABASE_CHOOSE_NAME;
if (!($database = getenv('SS_DATABASE_NAME')) && $chooseName = getenv('SS_DATABASE_CHOOSE_NAME')) {
$loopCount = (int)$chooseName;
$databaseDir = BASE_PATH;
for ($i=0; $i<$loopCount-1;
$i++) {
for ($i=0; $i<$loopCount-1; $i++) {
$databaseDir = dirname($databaseDir);
}
$database = "SS_" . basename($databaseDir);
$database = getenv('SS_DATABASE_PREFIX') ?: 'SS_';
$database .= basename($databaseDir);
$database = str_replace('.', '', $database);
}
}
if (defined('SS_DATABASE_USERNAME') && defined('SS_DATABASE_PASSWORD')) {
if ($dbUser = getenv('SS_DATABASE_USERNAME')) {
global $databaseConfig;
// Checks if the database global is defined (if present, wraps with prefix and suffix)
@ -105,69 +83,69 @@ if (defined('SS_DATABASE_USERNAME') && defined('SS_DATABASE_PASSWORD')) {
if (!$name) {
return '';
} else {
return (defined('SS_DATABASE_PREFIX') ? SS_DATABASE_PREFIX : '')
return (getenv('SS_DATABASE_PREFIX') ?: '')
. $name
. (defined('SS_DATABASE_SUFFIX') ? SS_DATABASE_SUFFIX : '');
. (getenv('SS_DATABASE_SUFFIX') ?: '');
}
};
/** @skipUpgrade */
$databaseConfig = array(
"type" => defined('SS_DATABASE_CLASS') ? SS_DATABASE_CLASS : 'MySQLDatabase',
"server" => defined('SS_DATABASE_SERVER') ? SS_DATABASE_SERVER : 'localhost',
"username" => SS_DATABASE_USERNAME,
"password" => SS_DATABASE_PASSWORD,
"type" => getenv('SS_DATABASE_CLASS') ?: 'MySQLDatabase',
"server" => getenv('SS_DATABASE_SERVER') ?: 'localhost',
"username" => $dbUser,
"password" => getenv('SS_DATABASE_PASSWORD'),
"database" => $databaseNameWrapper($database),
);
// Set the port if called for
if (defined('SS_DATABASE_PORT')) {
$databaseConfig['port'] = SS_DATABASE_PORT;
if ($dbPort = getenv('SS_DATABASE_PORT')) {
$databaseConfig['port'] = $dbPort;
}
// Set the timezone if called for
if (defined('SS_DATABASE_TIMEZONE')) {
$databaseConfig['timezone'] = SS_DATABASE_TIMEZONE;
if ($dbTZ = getenv('SS_DATABASE_TIMEZONE')) {
$databaseConfig['timezone'] = $dbTZ;
}
// For schema enabled drivers:
if (defined('SS_DATABASE_SCHEMA')) {
$databaseConfig["schema"] = SS_DATABASE_SCHEMA;
if ($dbSchema = getenv('SS_DATABASE_SCHEMA')) {
$databaseConfig["schema"] = $dbSchema;
}
// For SQlite3 memory databases (mainly for testing purposes)
if (defined('SS_DATABASE_MEMORY')) {
$databaseConfig["memory"] = SS_DATABASE_MEMORY;
if ($dbMemory = getenv('SS_DATABASE_MEMORY')) {
$databaseConfig["memory"] = $dbMemory;
}
}
if (defined('SS_SEND_ALL_EMAILS_TO')) {
Email::config()->send_all_emails_to = SS_SEND_ALL_EMAILS_TO;
if ($sendAllEmailsTo = getenv('SS_SEND_ALL_EMAILS_TO')) {
Email::config()->send_all_emails_to = $sendAllEmailsTo;
}
if (defined('SS_SEND_ALL_EMAILS_FROM')) {
Email::config()->send_all_emails_from = SS_SEND_ALL_EMAILS_FROM;
if ($sendAllEmailsFrom = getenv('SS_SEND_ALL_EMAILS_FROM')) {
Email::config()->send_all_emails_from = $sendAllEmailsFrom;
}
if (defined('SS_DEFAULT_ADMIN_USERNAME')) {
if (!defined('SS_DEFAULT_ADMIN_PASSWORD')) {
if ($defaultAdminUser = getenv('SS_DEFAULT_ADMIN_USERNAME')) {
if (!$defaultAdminPass = getenv('SS_DEFAULT_ADMIN_PASSWORD')) {
user_error(
"SS_DEFAULT_ADMIN_PASSWORD must be defined in your _ss_environment.php,"
"SS_DEFAULT_ADMIN_PASSWORD must be defined in your environment,"
. "if SS_DEFAULT_ADMIN_USERNAME is defined. See "
. "http://doc.silverstripe.org/framework/en/topics/environment-management for more information",
E_USER_ERROR
);
} else {
Security::setDefaultAdmin(SS_DEFAULT_ADMIN_USERNAME, SS_DEFAULT_ADMIN_PASSWORD);
Security::setDefaultAdmin($defaultAdminUser, $defaultAdminPass);
}
}
if (defined('SS_USE_BASIC_AUTH') && SS_USE_BASIC_AUTH) {
BasicAuth::config()->entire_site_protected = SS_USE_BASIC_AUTH;
if ($useBasicAuth = getenv('SS_USE_BASIC_AUTH')) {
BasicAuth::config()->entire_site_protected = $useBasicAuth;
}
if (defined('SS_ERROR_LOG')) {
if ($errorLog = getenv('SS_ERROR_LOG')) {
$logger = Injector::inst()->get('Logger');
if ($logger instanceof Logger) {
$logger->pushHandler(new StreamHandler(BASE_PATH . '/' . SS_ERROR_LOG, Logger::WARNING));
$logger->pushHandler(new StreamHandler(BASE_PATH . '/' . $errorLog, Logger::WARNING));
} else {
user_error("SS_ERROR_LOG setting only works with Monolog, you are using another logger", E_USER_WARNING);
}
@ -175,3 +153,24 @@ if (defined('SS_ERROR_LOG')) {
// Allow database adapters to handle their own configuration
DatabaseAdapterRegistry::autoconfigure();
unset(
$envType,
$chooseName,
$loopCount,
$databaseDir,
$i,
$databaseNameWrapper,
$dbUser,
$dbPort,
$dbTZ,
$dbSchema,
$dbMemory,
$sendAllEmailsTo,
$sendAllEmailsFrom,
$defaultAdminUser,
$defaultAdminPass,
$useBasicAuth,
$errorLog,
$logger
);

View File

@ -1,34 +1,37 @@
<?php
// Bootstrap _ss_environment.php
// Bootstrap environment variables
if (!defined('SS_ENVIRONMENT_TYPE')) {
define('SS_ENVIRONMENT_TYPE', 'dev');
use Dotenv\Loader;
$loader = new Loader(null);
if (!getenv('SS_ENVIRONMENT_TYPE')) {
$loader->setEnvironmentVariable('SS_ENVIRONMENT_TYPE', 'dev');
}
if (!defined('SS_DATABASE_CLASS') && !defined('SS_DATABASE_USERNAME')) {
if (!getenv('SS_DATABASE_CLASS') && !getenv('SS_DATABASE_USERNAME')) {
// The default settings let us define the database config via environment vars
// Database connection, including PDO and legacy ORM support
switch (getenv('DB')) {
case "PGSQL";
define('SS_DATABASE_CLASS', getenv('PDO') ? 'PostgrePDODatabase' : 'PostgreSQLDatabase');
define('SS_DATABASE_USERNAME', 'postgres');
define('SS_DATABASE_PASSWORD', '');
break;
$loader->setEnvironmentVariable('SS_DATABASE_CLASS', getenv('PDO') ? 'PostgrePDODatabase' : 'PostgreSQLDatabase');
$loader->setEnvironmentVariable('SS_DATABASE_USERNAME', 'postgres');
$loader->setEnvironmentVariable('SS_DATABASE_PASSWORD', '');
break;
case "SQLITE":
define('SS_DATABASE_CLASS', getenv('PDO') ? 'SQLite3PDODatabase' : 'SQLite3Database');
define('SS_DATABASE_USERNAME', 'root');
define('SS_DATABASE_PASSWORD', '');
define('SS_SQLITE_DATABASE_PATH', ':memory:');
$loader->setEnvironmentVariable('SS_DATABASE_CLASS', getenv('PDO') ? 'SQLite3PDODatabase' : 'SQLite3Database');
$loader->setEnvironmentVariable('SS_DATABASE_USERNAME', 'root');
$loader->setEnvironmentVariable('SS_DATABASE_PASSWORD', '');
$loader->setEnvironmentVariable('SS_SQLITE_DATABASE_PATH', ':memory:');
break;
default:
define('SS_DATABASE_CLASS', getenv('PDO') ? 'MySQLPDODatabase' : 'MySQLDatabase');
define('SS_DATABASE_USERNAME', 'root');
define('SS_DATABASE_PASSWORD', '');
$loader->setEnvironmentVariable('SS_DATABASE_CLASS', getenv('PDO') ? 'MySQLPDODatabase' : 'MySQLDatabase');
$loader->setEnvironmentVariable('SS_DATABASE_USERNAME', 'root');
$loader->setEnvironmentVariable('SS_DATABASE_PASSWORD', '');
}
define('SS_DATABASE_SERVER', '127.0.0.1');
define('SS_DATABASE_CHOOSE_NAME', true);
$loader->setEnvironmentVariable('SS_DATABASE_SERVER', '127.0.0.1');
$loader->setEnvironmentVariable('SS_DATABASE_CHOOSE_NAME', true);
}

View File

@ -427,7 +427,9 @@ class ConfigManifestTest extends SapphireTest
public function testEnvVarSetRules()
{
$_ENV['ENVVARSET_FOO'] = 1;
$loader = new \Dotenv\Loader(null);
$loader->setEnvironmentVariable('ENVVARSET_FOO', 1);
$config = $this->getConfigFixtureValue('EnvVarSet');
$this->assertEquals(
@ -463,7 +465,9 @@ class ConfigManifestTest extends SapphireTest
public function testEnvOrConstantMatchesValueRules()
{
$_ENV['ENVORCONSTANTMATCHESVALUE_FOO'] = 'Foo';
$loader = new \Dotenv\Loader(null);
$loader->setEnvironmentVariable('ENVORCONSTANTMATCHESVALUE_FOO', 'Foo');
define('ENVORCONSTANTMATCHESVALUE_BAR', 'Bar');
$config = $this->getConfigFixtureValue('EnvOrConstantMatchesValue');
@ -537,7 +541,9 @@ class ConfigManifestTest extends SapphireTest
public function testMultipleRules()
{
$_ENV['MULTIPLERULES_ENVVARIABLESET'] = 1;
$loader = new \Dotenv\Loader(null);
$loader->setEnvironmentVariable('MULTIPLERULES_ENVVARIABLESET', 1);
define('MULTIPLERULES_DEFINEDCONSTANT', 'defined');
$config = $this->getConfigFixtureValue('MultipleRules');

View File

@ -12,7 +12,7 @@ class DBTest extends SapphireTest
public function testValidAlternativeDatabaseName()
{
$prefix = defined('SS_DATABASE_PREFIX') ? SS_DATABASE_PREFIX : 'ss_';
$prefix = getenv('SS_DATABASE_PREFIX') ?: 'ss_';
Director::config()->update('environment_type', 'dev');
$this->assertTrue(DB::valid_alternative_database_name($prefix.'tmpdb1234567'));